﻿var totaltabs  = 5;
var campCookie = getCookie("PINSCAMPTAB");
var wolscript = "hide";

window.onload = function(){
    if (wolscript == "show") {
        setTabs(4);
    }
//    else if(campCookie == null) {
//        getCampagneID();
//    } else {
        setTabs(1);
//    }
};

$(document).ready(function() {

    // set mainmenu actions
    $("#Wuc_header1_HL_products").mouseover(function() {
        showMenu(this,"products_flyout");
    });

    $("#Wuc_header1_HL_solutions").mouseover(function() {
        showMenu(this,"solutions_flyout");
    });
    
    $("#Wuc_header1_HL_infrastructuur").mouseover(function() {
        showMenu(this,"infrastructuur_flyout");
    });
    
    $("#Wuc_header1_HL_partners").mouseover(function() {
        showMenu(this,"partners_flyout");
    });
    
    $("#Wuc_header1_HL_support").mouseover(function() {
        showMenu(this,"support_flyout");
    });
    
    $("#Wuc_header1_HL_contact_overview").mouseover(function() {
        showMenu(this,"contact_flyout");
    });
    
    $("#Wuc_header1_HL_pins").mouseover(function() {
        showMenu(this,"pins_flyout");
    });
    
    /* set clearing spaces */
    $("#logo_pins").mouseover(function() { clearSelected(); });
    $("#logo_text").mouseover(function() { clearSelected(); });
    $("#menumain_home").mouseover(function() { clearSelected(); });
    $("#mainlinks_right").mouseover(function() { clearSelected(); });
    $("#contents_home").mouseover(function() { clearSelected(); });
    
    // set tab actions
	$("#tab1").click(function(){
	    setTabsOnReady(1);
	});	
	
	$("#tab2").click(function(){
	    setTabsOnReady(2);
	});	
	
	$("#tab3").click(function(){
	    setTabsOnReady(3);
	});	
	
	$("#tab4").click(function(){
	    setTabsOnReady(4);
	    wolMenuSelect(1);
	});	
	
	$("#tab5").click(function(){
	    setTabsOnReady(5);
	});
	
	// set button actions
	$("#buttons_starter").hide();
    $("#buttons_business").hide();
    $("#buttons_e_commerce").hide();
    
	$("#order_items_starter").click(function(){
	    $("#buttons_starter").show();
    });	
    $("#order_items_business").click(function(){
    	$("#buttons_business").show();
    });	
    $("#order_items_e_commerce").click(function(){
    	$("#buttons_e_commerce").show();
    });

});


function clearSelected() {
    $("ul.main_menu").find("a").removeClass("main_menu_hover");
    $("div.main_menu_flyout").hide();
}

function showMenu(el, sub) {
    //cleanup
    clearSelected();
    //add placeholder class
    $(el).addClass("main_menu_hover");
    //get the position of the placeholder element
    var pos = $(el).offset();  
    //show the menu on the placeholder position
    $("#"+sub).css( { "left":pos.left + "px", "top":(pos.top+20) + "px" } );
    $("#"+sub).show();
}
    
// get campagne tabID from xml file, when not is 0 then set cookie
// and set the first tab to show, else show random tabs
function getCampagneID() {
	var xmlDoc = null;
	// try to create xml object
	try { 
		xmlDoc = new XMLHttpRequest(); // Firefox, Opera 8.0+, Safari
	}
	catch (e) { 
		try { 
			xmlDoc = new ActiveXObject("Msxml2.XMLHTTP"); // Internet Explorer 6.0+
		}
		catch (e) { 
			try {
				xmlDoc = new ActiveXObject("Microsoft.XMLHTTP"); // Internet Explorer 5.5+
			}
			catch (e) {}
		}
	}
	
	if (xmlDoc) { // object exist, get data from xml
		xmlDoc.open("GET" ,"index_campagne_ini.xml", true);
		
		xmlDoc.onreadystatechange = function() {
			if(xmlDoc.readyState == 4 && xmlDoc.status == 200) {
				var xDc = xmlDoc.responseXML;
				var tEl = xDc.getElementsByTagName("tabNummer");
				var tID = tEl[0].firstChild.data;
				if(tID == 0) { // no campagne tab is set
				    setTabs(0);
				} else if (tID > 0 && tID <= totaltabs) { // campagne tab is set
				    // write firsttime cookie
				    var eD = setExpDate("D",1);
				    setCookie("PINSCAMPTAB",tID,eD,"","","");
				    setTabs(tID);
				} else {
				    setTabs(0);
				}
			}
		}
		
		xmlDoc.send(null);
	} else { // can not create xml object
	    setTabs(0);
	}
}

// set the tabs on the page by default or random
function setTabs(campTabID) {
    var firsttab;
    if(campTabID == 0) {
        firsttab = Math.floor(Math.random() * totaltabs) + 1;
    } else {
        firsttab = campTabID;
    }
    for (var i = 1; i <= totaltabs; i++) {
		if (i == firsttab) {
		    $("#tab"+i).hide();
	        $("#tab_hover"+i).show();
	        $("#tab"+i+"content").show();
	        $("#banner_"+i).show();
	        if (firsttab == 4) wolMenuSelect(1);
		} else {
		    $("#tab"+i).show();
	        $("#tab_hover"+i).hide();
	        $("#tab"+i+"content").hide();
	        $("#banner_"+i).hide();
		}
	}
}

// set the tabs on the page after document.ready state
function setTabsOnReady(TabID) {
    $("#banner_wol").hide();
    $("#banner_home").show();
    for (var i = 1; i <= totaltabs; i++) {
		if (i == TabID) {
		    $("#tab"+i).hide();
	        $("#tab_hover"+i).show();
			$("#tab"+i+"content").show();
	        $("#banner_"+i).show();
		} else {
		    $("#tab"+i).show();
	        $("#tab_hover"+i).hide();
			$("#tab"+i+"content").hide();
	        $("#banner_"+i).hide();
		}
	}
}

// set Werk.Online menu items
function wolMenuSelect(itemID) {
    $("#banner_home").hide();
    $("#banner_wol").show().find("div").hide();
    $("#banner_wol"+itemID).show();
    $("div.wolcontent").hide();
    $("#content_wol"+itemID).show();
    $("ul.wol_menu").find("a").removeClass("wol_menu_hover");
    $("#wol_link"+itemID).addClass("wol_menu_hover");
}
