﻿function setLoginCookie(c_name, value, expiredays, domain) {
	var exdate = new Date();
	domain = domain.substring(domain.indexOf("."),domain.length)
	exdate.setDate(exdate.getDate() + expiredays);
	document.cookie = c_name + "=" + escape(value) + ((expiredays == null) ? "" : ";expires=" + exdate.toGMTString()) + ";path=/" + ";domain=" + domain;
}

function setCookie(c_name, value, expiredays) {
	var exdate = new Date();
	exdate.setDate(exdate.getDate() + expiredays);
	document.cookie = c_name + "=" + escape(value) + ((expiredays == null) ? "" : ";expires=" + exdate.toGMTString()) + ";path=/";
}

function getCookie(c_name) {
	if (document.cookie.length > 0) {
		c_start = document.cookie.indexOf(c_name + "=");
		if (c_start != -1) {
			c_start = c_start + c_name.length + 1;
			c_end = document.cookie.indexOf(";", c_start);
			if (c_end == -1) {
				c_end = document.cookie.length;
			}
			return unescape(document.cookie.substring(c_start, c_end));
		}
	}
	return "";
}

if (getCookie("ct") == "") {
	if (window.location.pathname != GLOBALOBJ.contextPath + "/mc/content/error/cookieDisabled.jsf") {
		setCookie("ct", "true", 1);
		if (getCookie("ct") == "") {
			window.location = GLOBALOBJ.contextPath + "/mc/content/error/cookieDisabled.jsf";
		}
	}
}

function resetFeaturedItems() {
	$("#verticalTabsOptions").removeAttr("style");
	if ($("#verticalTabsOptions").height() < $("#verticalTabsContent").height()) {
			$("#verticalTabsOptions").css("height", $("#verticalTabsContent").height());
	}
}

function swapSubmit() {
	var inputArr = document.getElementsByTagName("input");
	for (var i = 0; i < inputArr.length; i++) {
		if ((document.styleSheets.length) && (inputArr[i].type == "submit") && (inputArr[i].src)) {
			// change type from submit to image, in order to display graphical submit button
			var inputSubmit = inputArr[i];

			// copy previous submit button attributes to new submit, and replace element					
			var imageSubmit = document.createElement("input");
			imageSubmit.type = "image";
			if (inputSubmit.size) {
				imageSubmit.size = inputSubmit.size;
			}
			if (inputSubmit.value) {
				imageSubmit.value = inputSubmit.value;
			}
			if (inputSubmit.name) {
				imageSubmit.name = inputSubmit.name;
			}
			if (inputSubmit.id) {
				imageSubmit.id = inputSubmit.id;
			}
			if (inputSubmit.className) {
				imageSubmit.className = inputSubmit.className;
			}
			if (inputSubmit.src) {
				imageSubmit.src = inputSubmit.src;
			}
			inputSubmit.parentNode.replaceChild(imageSubmit, inputSubmit);
		}
	}
}

function addEvent(obj, evType, fn){
	if (obj.addEventListener){ 
		obj.addEventListener(evType, fn, false); 
		return true; 
	} else if (obj.attachEvent){ 
		var r = obj.attachEvent("on"+evType, fn); 
		return r; 
	} else { 
		return false; 
	} 
}

function openNav(id) {
	var navList = $("#navigation li a");
	var contentList = $("#layout .content");
	navList.removeClass("active");
	contentList.hide();
	$(navList[id - 1]).addClass("active");
	$(contentList[id - 1]).show();
}

// if ie6, then add mouse over events to mimic :hover if newer browsers 
if (navigator.appName == "Microsoft Internet Explorer" && navigator.userAgent.indexOf("MSIE 6") != -1) {
	dashHover = function() {
		var dashTabs = $('#dashboardTabs > ul li');
		if(dashTabs.length) {
			for (var i=0; i<dashTabs.length; i++) {
				dashTabs[i].onmouseover=function() {
					$(this).addClass('tabHover');
				}
				dashTabs[i].onmouseout=function() {
					$(this).removeClass('tabHover');
				}
			};
		}                                                              
	}
 
	headerNavHover = function() {
		var headerNav = $('#headerMainNav ul');
		if(headerNav.length) {
			for (var p=0; p<headerNav.length; p++) {
				if (headerNav[p].className == 'subNav') {
					headerSubNav = headerNav[p].getElementsByTagName("li");
					for (var i=0; i<headerSubNav.length; i++) {
						headerSubNav[i].onmouseover=function() {
							$(this).addClass('headerNavHover');
						}
						headerSubNav[i].onmouseout=function() {
							$(this).removeClass('headerNavHover');
						}
					}                                                                              
				}
			};
		}                                                              
	}              
	
	findAnotherHover = function() {
		var findHover = $('#findAnother li');
		if(findHover.length) {
			for (var g=0; g<2; g++) {
				findHover[g].onmouseover=function() {
					$(this).addClass('fHover');
				}
                
				findHover[g].onmouseout=function() {
					$(this).removeClass('fHover');
				}
			};
		}
	}
	
	if (window.attachEvent && headerNavHover) window.attachEvent("onload", headerNavHover);            
	if (window.attachEvent && dashHover) window.attachEvent("onload", dashHover);      
	if (window.attachEvent && findAnotherHover) window.attachEvent("onload", findAnotherHover);                                        

}	

function showContent(content) {
	$('#hiddenContent').children().hide();
	$('#' + content).show();
}

function popitup(url) {
	newwindow=window.open(url,'name','top=150,left=200,height=660,width=670,scrollbars=1');
	if (window.focus) {newwindow.focus()}
}

function popituptandc(url) {
	newwindow=window.open(url,'name','top=150,left=200,height=470,width=670,scrollbars=1');
	if (window.focus) {newwindow.focus()}
}

function popitupsized(url,h,w) {
    newwindow=window.open(url,'name','top=150,left=200,height='+h+',width='+w+',scrollbars=1');
    if (window.focus) {newwindow.focus()}
}

function resetReg3(){
	// if region3 exists, do this, otherwise ignore
	if($("#region3").length > 0) {
		$("#region3:not([class='noBg'])").removeAttr("style");
		var reg3Height = $("#region3:not([class='noBg'])").height();
		if (reg3Height != null && reg3Height < $("#main").height()) {
				$("#region3").css("height", $("#main").height());
		}
	}
}

$.fn.setAllToMaxHeight = function(){
	return this.height( Math.max.apply(this, $.map( this , function(e){ return $(e).height() }) ) )
}

$.fn.checknAdjustClass = function(chkcl, action){
	if(action == 0) {
		if($(this).hasClass(chkcl)) {
			$(this).removeClass("active actv");
		} else {
			$(this).removeClass("active");
		}
	} else if (action == 1) {
		if($(this).hasClass(chkcl) && action == 1) {
			$(this).addClass("active actv");
		} else {
			$(this).addClass("active");
		}               
	}
	return this;
}

jQuery.preloadImages = function()
{
  for(var i = 0; i<arguments.length; i++)
  {
    jQuery("<img>").attr("src", arguments[i]);
  }
}

$(document).ready(function () {
	swapSubmit();
		
	if(navigator.appName == 'Microsoft Internet Explorer' && navigator.userAgent.indexOf("MSIE 6") != -1) {
		$("#headerMainNav ul.subNav, #headerMainNav ul.secondNav").bgiframe();
	} 
	
	$('#header ul.globalNav li:first-child').bind("mouseenter", function(){
        $(this).children("ul.subNav:hidden").stop().css({'height' : 'auto', 'overflow' : 'visible'}).slideDown(500);
    });    
                         
	// header drop down menu fade out 
    $('#header ul.globalNav li:first-child').bind("mouseleave", function(){
        $(this).children("ul.subNav").stop().slideUp(1);
    });
	
	$(".popUpContainer").appendTo("#main");
	
	// move spotlightitem highlight into region2 for standard mode only
	$("#spotLightItem.r2").prependTo("#region2 #content").removeClass("r2");
	
	// Check to see if a user has already selected a segment
	dmTabSegment=getCookie("dmTabSegment");
	if(dmTabSegment){
		//Only override the tab if the user is in the DM section of the website.
		if(uriDirectories.match("/cpo/mc/business/productsservices/marketing/") || uriDirectories.match("/cpo/mc/business/productsservices/atoz/")){
			var n = dmTabSegment;
			if (-1 != n) {
				$(".tabs div.tabContent").filter(":visible").hide().end().filter(
				function(index){
					return index==n;
				}
			).show();
				$(".tabs ul.topMenu li").filter(":visible").removeClass("active").filter(
				function(index){
					return index==n;
				}
			).addClass("active");
			}
		}
	}
	
	$(".tabs ul.topMenu li.tabVariable a").bind("click", function (e) {
		if( e )
			e.preventDefault();
		if(this.hash == location.hash)
			return;
		location = this.href;
		var n = $(".tabs ul.topMenu li.tabVariable a").index(this);
			setCookie("dmTabSegment", n, 365);
			
			if(uriDirectories.match("/cpo/mc/business/productsservices/marketing/") || uriDirectories.match("/cpo/mc/business/productsservices/atoz/")){
				trackDMSegmentSwitch(n);	//Call to Omniture to track change in DM Segment.
			}
			
		$(".tabs div.tabContent").filter(":visible").hide().end().filter(
			function(index){
				return index==n;
			}
		).show();
		$(".tabs ul.topMenu li").filter(":visible").removeClass("active").filter(
			function(index){
				return index==n;
			}
		).addClass("active");
	});
	

	if(location.hash){
		var n = $(".tabs .tabContent").index($(location.hash));
		if (-1 != n) {
			$(".tabs div.tabContent").filter(":visible").hide().end().filter(
			function(index){
				return index==n;
			}
		).show();
			$(".tabs ul.topMenu li").filter(":visible").removeClass("active").filter(
			function(index){
				return index==n;
			}
		).addClass("active");
		}
	}

	
	$("div.tabContent").each(function(n){
			var item = $(this);
			item.removeAttr("id");
	});
	
	
	
	// global title variable for tooltips
	var saveTitle;
	
	$("a.doubleDashed").hover(
		function () {
			saveTitle = $(this).attr("title");
			$(this).attr("title", "");
		},
		function () {
			$(this).attr("title", saveTitle);
		}
	).bind("click", function (e) {
		// hide any tooltip that may be open, only one tip should show per click
		$(".toolTip").hide();
		$(this).attr("title", saveTitle);				
		
		var e = (!e) ? window.event : e;

		if (e.pageX) {
			yCord = e.pageY;
		} else if (e.clientX) {
			yCord = e.clientY + document.body.scrollTop	+ document.documentElement.scrollTop;
		}
		
		// test if new dynamic tip or old style static tip, if old tip, move to region3Content, display and return // otherwise if already in region3Content, display
		if($("#" + this.getAttribute("title")).length > 0 && $("#region3Content #" + this.getAttribute("title")).length == 0) {
			$("#" + this.getAttribute("title")).prependTo("#region3Content").css("top", (yCord - 200)).show();	

			$(".toolTip span.closeIcon").bind("click", function () {
				$(this).parent().parent().hide();
			});	
				
			return false;
		} else if ($("#" + this.getAttribute("title")).length > 0) {
			$("#" + this.getAttribute("title")).css("top", (yCord - 200)).show();	

			$(".toolTip span.closeIcon").bind("click", function () {
				$(this).parent().parent().hide();
			});				
			return false;
		}
	
		// if new style tip, since first instanc of tooltip, dynamically create tooltip div
		if($("#dynaTip").length == 0) {
			$('<div id="dynaTip" class="toolTip"><div class="closeTip"><span class="closeIcon"></span></div><div class="toolTipContent"><h3></h3><p></p></div></div>').prependTo("#region3Content");
			
			// bind onclick close tooltip function
			$(".toolTip span.closeIcon").bind("click", function () {
				$(this).parent().parent().hide();
			});						
		}
		
		// if :: seperator variable included in title, then split into proper segments for h3 title and tip content
		if($(this).attr('title').indexOf("=@@=") > 0) {
			$("#dynaTip .toolTipContent h3").html($(this).attr('title').split("=@@=")[0]);						
			$("#dynaTip .toolTipContent p").html($(this).attr('title').split("=@@=")[1]); 
		} else { 
			$("#dynaTip .toolTipContent p").html($(this).attr('title')); 
		}
		
		$('#dynaTip').css('top', (yCord - 200)).show()
		;	
		return false;
	});	
	
	
/* OLD TOOL TIP CODE	
	$("#content a.doubleDashed").bind("click", function (e) {
		$("#region3Content .toolTip").hide();
		var e = (!e) ? window.event : e;
		
		if (e.pageX) {
			yCord = e.pageY;
		} else {
			if (e.clientX) {
				yCord = e.clientY + document.body.scrollTop + document.documentElement.scrollTop;
			}
		}
		$("#" + this.getAttribute("title")).appendTo("#region3Content").show().css("top", (yCord - 200));
		return false;
	});
	
	$(".toolTip span.closeIcon").bind("click", function () {
		$(this).parent().parent().hide();
		return false;
	}); */
	
	$("#libraryMenu h3").click(function () {
		$(this).toggleClass("active").next().toggle();
		return false;
	});
	
	$(".toastHolder").bind("mouseleave", function () {
		var cssObj = {"visibility":"hidden"};
		$(this).children(".toastBackground").fadeOut("fast");
	});
	
	$(".toastHolder").bind("mouseover", function () {
		var cssObj = {"visibility":"visible", "top":-$(this).children(".toastBackground").height() - 20};
		$(this).children(".toastBackground").fadeIn("slow").css(cssObj);
	});
	
	// makes the text caption follow the image's width + 5px for breathing room  
	$('.imageCaption').each(function(){
			var item = $(this);
			item.css('width', $('img', this).eq(0).attr('width'));
	});
	
	$("#verticalTabsOptions:not([class='defProp']) li:has(a)").bind("click", function () {
		var n = $("#verticalTabsOptions li:has(a)").index(this);
		$("#verticalTabsContent div.tabContent").filter(":visible").removeClass("active").end().filter(":eq(" + n + ")").addClass("active");
		$("#verticalTabsOptions li").filter(":visible").checknAdjustClass("double-line", 0).end().filter(":eq(" + n + ")").checknAdjustClass("double-line", 1);
		resetFeaturedItems();
		resetReg3();
		return false;
	});
	
	$("#verticalTabsOptions ul li").each(function() {
		if($(this).height() > parseInt($(this).css("line-height"))) {
			$(this).addClass("double-line");
		}
	});
	
	$("#header a[href*='#']").bind("click", function() {
		var urlTest = $(this).attr("href").split("#")[0];
		if (location.href.match(urlTest) && $(".tabs").length) {
			window.location.reload();}
	});
	$(".dialog").bgiframe();
	resetReg3();
	
});

$(window).load(function () {
	//fixes container height to cater to content
	$(".miniSpotLight h2").setAllToMaxHeight();
	$(".miniSpotLight .miniSpotImg").setAllToMaxHeight();
	$(".miniSpotLight").setAllToMaxHeight();
	$(".marqueeItem .sectionTitle").setAllToMaxHeight();
	$(".marqueeItem .sectionBorder").setAllToMaxHeight();
	
	if (typeof $("img").noContext == "function")
	{
		$("img").noContext();
	}
	resetReg3();
}); 

function doSearch(){
	var searchterm = document.searchForm.query.value.toLowerCase();
	
	if (searchterm == "" || searchterm == "search" || searchterm == "recherche"){
		return false;
	}
	if (searchterm != 'search' && searchterm != 'recherche' && searchterm != ''){
		document.searchForm.submit();
	}
}/*
function doSearchOnPage(){
	var searchterm = document.searchFormOnPage.QueryText.value.toLowerCase();
	var encodedQuery = "";
	if (searchterm == "" || searchterm == "search" || searchterm == "recherche"){
		return false;
	}
	if (searchterm != 'search' && searchterm != 'recherche' && searchterm != ''){
		document.searchFormOnPage.Query.value = searchterm;
		document.searchForm.QueryText.value = searchterm;
		document.searchFormOnPage.submit();
	}
}*/
function doSearchHeader(){
	var searchterm = document.searchFormHeader.query.value.toLowerCase();
	
	if (searchterm == "" || searchterm == "search" || searchterm == "recherche"){ 
		return false;
	}
	if (searchterm != 'search' && searchterm != 'recherche' && searchterm != ''){
		document.searchFormHeader.submit();
	}
}



