// JavaScript Document
var navID = 0;
var selectedItem = "p1";
var intIndex = -1;

$(document).ready(function() { 
	// Reset the screen to (0,0)
	if($.cookie("pdfLink") && unescape($.cookie("pdfLink")) != "" && unescape($.cookie("pdfLink")) != "launched"){
		window.open(unescape($.cookie("pdfLink")), "PDF", "toolbar=yes");
		$.cookie("pdfLink", "launched", options);
	}
	backToTop();
	buildNav();
	//pdfLaunchPopup();
	if(unescape($.cookie("pdfLink")) != "launched" && location.href.toLowerCase().split("dynamicsmea.com").length == 2){
		if($.cookie("timeout")){
			initPopup("launched");
		}else{ 
			setTimeout(initPopup, 8000, "launched");
		}		
	}
	
}); 

function setMainBG(bColumn){
	// show or hide the column border
	if(bColumn){
		$("div#mainBG").css('background', 'url(images/css/bgMainCol.gif) repeat-y');
		$("div#mainContainer").css('background', 'url(images/css/bgMainTextCol.jpg) no-repeat');
		$("div#mainContent").css('width', '426px');
	}else{
		$("div#mainBG").css('background', 'url(images/css/bgMain.gif) repeat-y');
		$("div#mainContainer").css('background', 'url(images/css/bgMainText.jpg) no-repeat');
		$("div#mainContent").css('width', '660px');
	}
}

function setScrollers(){
	$("a.scroller").each(function(){
		$(this).attr("href","#p"+(navID+1)).click(function(){
			var tg = "#" + $(this).attr("title");
			$.scrollTo(tg, {speed:800,ease:"easeInOut",axis:'y'} );
		});	
	});
	$("a.backToTop").each(function(){
		$(this).attr("href","#p"+(navID+1)).click(backToTop);							   
	});
}

function buildNav(){
	selectedItem = window.location.hash; // get item from URL (ex: #p2)
	selectedItem = selectedItem.substr(1, selectedItem.length); // item name (ex: p2)
	navID = Number(selectedItem.substr(1, selectedItem.length))-1; // item ID (ex: 2);
	if(navID == -1 || isNaN(navID)){ // no item selected -> select first
		navID = 0;
		selectedItem = "p1";
	}
	loadPage(navID);
	
	$("ul.navigation a").css("behavior", "url(iepngfix.htc)").css("cursor", "pointer"); // IE6: apply iepngfix and hand cursor on all items
	var pos = "-638px -" + Number(9+(30*navID)) + "px"; 
	$("a."+selectedItem).css("background-position",pos);// set 'selected' css background position
	
	// item functionality
	$("ul.navigation a").each(function(intIndex){
		$(this).mouseover(function(){
			if(intIndex != navID){
				//if not the selected item, apply 'over' background
				var pos = "-319px -" + Number(9+(30*intIndex)) + "px";
				$(this).css("background-position",pos);
			}
		});
		$(this).mouseout(function(){
			if(intIndex != navID){
				//if not the selected item, apply 'normal' background
				var pos = "0px -" + Number(9+(30*intIndex)) + "px";
				$(this).css("background-position",pos);
			}
		});	
		$(this).click(function(){
			if(intIndex != navID){
				loadPage(intIndex); // load page
			}
		});	
	});	
	$("a.contactlink").click(function(){
		//loadPage(13);
		$("ul.navigation a.p13").click();
	});
}

function loadPage(pageID){
	
		pageID = (pageID==2)? 3: pageID;
				
		var navItem = "ul.navigation a.p"+(pageID+1);
		var linkText = $(navItem).text();
		document.title = "Microsoft Dynamics Vertical Solutions Middle East & Africa - " + linkText;
	
		// CSS
		var pos = "-638px -" + Number(9+(30*pageID)) + "px";
		$(navItem).css("background-position",pos); // set 'selected' css background position
		
		// ACTION
		linkText = linkText.split(" ").join("_"); // remove spaces
		linkText = linkText.split(".").join("_"); // remove dots
		
		$("div#footer").hide();
		$("div#mainBG").css("height", "720px");
		$("div#mainContainer").fadeOut("fast", function(){
			$(this).load("pages/"+linkText+".htm", null, function(){ // load content
				$(this).fadeIn("fast");
				$("div#mainBG").css("height", "auto");
				if(navID == 0 || navID == 1 || navID == 12 || navID == 13) setMainBG(false);
				else setMainBG(true);
				setScrollers();
				pdfLaunchPopup();
				$("div#footer").show();
			});										
		});			

		/*$("div#footer").fadeOut("fast", function(){
			$(this).fadeIn("fast");									 
		});*/
		
		navID = pageID; // pageID becomes the selected navigation ID
		$("ul.navigation a").trigger('mouseout'); // trigger mouseout on all items
}

function backToTop(){
	$.scrollTo(0, {speed:800,ease:"easeInOut",axis:'y'} );
}

function pdfLaunchPopup(){
    $("a[href^='http']:not(a.facebooklink, a#popupContactClose, a#backgroundPopup, a.nopopup), a[href*='.pdf']:not(a.contactPDF, a#popupContactClose, a#backgroundPopup, a.nopopup)").each(function(){
		if(!$.cookie(COOKIE_NAME)){
			$(this).attr("name", $(this).attr("href"));
			//console.log($(this).attr("name"));
			$(this).removeAttr("href").css("cursor","pointer").click(function(){
				initPopup($(this).attr("name"));
			});	
		}																																																		
	});
}

function initPopup(sNewWin){
	if(popupStatus == 0){
		$.cookie("timeout", true, options);
		//centering with css
		centerPopup();
		//load popup
		loadPopup();
		initForm(sNewWin);
	}
}

function initForm(sNewWin){
	// focus first input field:
	$('input.inputfield:first').focus();

	// input select:
	
	$('#countrydropdown').change(function() {
		
		if (this.value == ""){
			$('#countrydropdown_label').html("-- Select --");
		}else{
			$('#countrydropdown_label').html(this.value);
		}
	
	});
	$('#countrydropdown_container').mouseover(function() {
		$('#countrydropdown_container').addClass('dropdown_over');
	
	});
	
	$('#countrydropdown_container').mouseout(function() {
		$('#countrydropdown_container').removeClass('dropdown_over');
	
	});

	var container = $('div.errorWrap');
	// validate the form when it is submitted
	var validator = $("#registerForm").validate({
		errorContainer: container,
		errorLabelContainer: $("ol", container),
		wrapper: 'li',
		meta: "validate",
		submitHandler: function() {
			// add pdf or link to the form
			$("#registerForm").append('<input type="hidden" name="pdfLink" value="'+sNewWin+'" />');
			// submit the form
		 	registerForm.submit();
		 }
	});
  
	// focus and blur: change background:
	$('input').focus(function() {	
		$(this).addClass("active");
	});
	$('input').blur(function() {
		$(this).removeClass("active")
	});	
	$("input#formLocation").attr("value", window.location.hash);
	$.get("formcheck/token.asp",function(txt){
  		$("#registerForm").append('<input type="hidden" name="ts" value="'+txt+'" />');
	});
}