
// -----------------------------------------------------------------------------------
//
//	BING CREATIVE - Custom JavaScripts
//	Part 1: Main Menu JavaScript Controls
//
// -----------------------------------------------------------------------------------

if(!Array.indexOf){
	Array.prototype.indexOf = function(obj){
		for(var i=0; i<this.length; i++){
			if(this[i]==obj){
				return i;
			}
		}
		return -1;
	}
}

function newRollOver(targetMenu){
	if (bingMenu[1][bingMenu[0].indexOf(targetMenu)] != 'open') {
		document.getElementById(targetMenu).style.width = '135px';
	};
}

function newRollOut(targetMenu){
	if (bingMenu[1][bingMenu[0].indexOf(targetMenu)] != 'open') {
		document.getElementById(targetMenu).style.width = '125px';
	};
}

var bingMenu=new Array(new Array('newDesignMenu','newCultureMenu','newDialogueMenu'),new Array('closed','closed','closed'));

function buttonActivate(targetMenu){
		document.getElementById(targetMenu).style.width = '955px';
		bingMenu[1][bingMenu[0].indexOf(targetMenu)] = 'open';
}


// -----------------------------------------------------------------------------------
//
//	BING CREATIVE - Custom JavaScripts
//	Part 2: Portfolio SubMenu
//
// -----------------------------------------------------------------------------------

function SDMenu(id) {
	if (!document.getElementById || !document.getElementsByTagName)
		return false;
	this.menu = document.getElementById(id);
	this.submenus = this.menu.getElementsByTagName("div");
	this.remember = true;
	this.speed = 3;
	this.markCurrent = true;
	this.oneSmOnly = true;
}
SDMenu.prototype.init = function() {
	var mainInstance = this;
	for (var i = 0; i < this.submenus.length; i++)
		this.submenus[i].getElementsByTagName("span")[0].onclick = function() {
			mainInstance.toggleMenu(this.parentNode);
		};
	if (this.markCurrent) {
		var links = this.menu.getElementsByTagName("a");
		for (var i = 0; i < links.length; i++)
			if (links[i].href == document.location.href) {
				links[i].className = "current";
				break;
			}
	}
	if (this.remember) {
		var regex = new RegExp("sdmenu_" + encodeURIComponent(this.menu.id) + "=([01]+)");
		var match = regex.exec(document.cookie);
		if (match) {
			var states = match[1].split("");
			for (var i = 0; i < states.length; i++)
				this.submenus[i].className = (states[i] == 0 ? "collapsed" : "");
		}
	}
};
SDMenu.prototype.toggleMenu = function(submenu) {
	if (submenu.className == "collapsed")
		this.expandMenu(submenu);
	else
		this.collapseMenu(submenu);
};
SDMenu.prototype.expandMenu = function(submenu) {
	var fullHeight = submenu.getElementsByTagName("span")[0].offsetHeight;
	var links = submenu.getElementsByTagName("a");
	for (var i = 0; i < links.length; i++)
		fullHeight += links[i].offsetHeight;
	var moveBy = Math.round(this.speed * links.length);
	
	var mainInstance = this;
	var intId = setInterval(function() {
		var curHeight = submenu.offsetHeight;
		var newHeight = curHeight + moveBy;
		if (newHeight < fullHeight)
			submenu.style.height = newHeight + "px";
		else {
			clearInterval(intId);
			submenu.style.height = "";
			submenu.className = "";
			mainInstance.memorize();
		}
	}, 30);
	this.collapseOthers(submenu);
};
SDMenu.prototype.collapseMenu = function(submenu) {
	var minHeight = submenu.getElementsByTagName("span")[0].offsetHeight;
	var moveBy = Math.round(this.speed * submenu.getElementsByTagName("a").length);
	var mainInstance = this;
	var intId = setInterval(function() {
		var curHeight = submenu.offsetHeight;
		var newHeight = curHeight - moveBy;
		if (newHeight > minHeight)
			submenu.style.height = newHeight + "px";
		else {
			clearInterval(intId);
			submenu.style.height = "";
			submenu.className = "collapsed";
			mainInstance.memorize();
		}
	}, 30);
};
SDMenu.prototype.collapseOthers = function(submenu) {
	if (this.oneSmOnly) {
		for (var i = 0; i < this.submenus.length; i++)
			if (this.submenus[i] != submenu && this.submenus[i].className != "collapsed")
				this.collapseMenu(this.submenus[i]);
	}
};
SDMenu.prototype.expandAll = function() {
	var oldOneSmOnly = this.oneSmOnly;
	this.oneSmOnly = false;
	for (var i = 0; i < this.submenus.length; i++)
		if (this.submenus[i].className == "collapsed")
			this.expandMenu(this.submenus[i]);
	this.oneSmOnly = oldOneSmOnly;
};
SDMenu.prototype.collapseAll = function() {
	for (var i = 0; i < this.submenus.length; i++)
		if (this.submenus[i].className != "collapsed")
			this.collapseMenu(this.submenus[i]);
};
SDMenu.prototype.memorize = function() {
	if (this.remember) {
		var states = new Array();
		for (var i = 0; i < this.submenus.length; i++)
			states.push(this.submenus[i].className == "collapsed" ? 0 : 1);
		var d = new Date();
		d.setTime(d.getTime() + (30 * 24 * 60 * 60 * 1000));
		document.cookie = "sdmenu_" + encodeURIComponent(this.menu.id) + "=" + states.join("") + "; expires=" + d.toGMTString() + "; path=/";
	}
};




function writeBingPortfolioMenu(){
	
	document.write("<div style=\"float: left\" id=\"my_menu\" class=\"sideMenu\">");
document.write("<ul>              ");
document.write("<li><a href=\"design_what_we_do_branding.html\" id=\"Brand\" class=\"\" >Brand Identity<\/a><\/li>");
document.write("<li><a href=\"design_what_we_do_interactive.html\" id=\"Interactive\" >Interactive<\/a><\/li>");
document.write("<li><a href=\"design_what_we_do_print.html\" id=\"Print\" >Print and Promotion<\/a><\/li>");
document.write("<li><a href=\"design_what_we_do_exhibits.html\" id=\"Exhibits\" >Exhibits and Events<\/a><\/li>");
document.write("<li><a href=\"design_what_we_do_motion.html\" id=\"Motion\">Motion Graphics<\/a><\/li>");
document.write("<li><a href=\"design_what_we_do_social.html\" id=\"Social\" >Social Media<\/a><\/li>");
document.write("<\/ul> ");

document.write("   <\/div>");
document.write(" <div class=\"lookBookbtn\"><a href=\"downloads\/Bing_Creative_Look_Book_Fall_2011_Online.pdf\" onClick=\"_gaq.push(['_trackEvent', 'Downloads', 'PDF', '\/downloads\/Bing_Creative_Look_Book_Fall_2011_Online.pdf']);\" class=\"lookBookbtn\" target=\"_blank\"><h5>Download our Lookbook<\/h5><\/a><\/div>");


}


function writeBingWomenMenu(){
document.write("<div style=\"float: left\" id=\"my_menu\" class=\"sideMenu\">");
document.write("<ul>");
document.write("<li class=\"\">Case Studies<\/li>");
document.write("<li class=\"menuItem\"><a id=\"BLOOMING-BETTY\" href=\"cs_bloomingbetty.html\">Blooming Betty<\/a><\/li>");
document.write("<li class=\"menuItem\"><a id=\"CSS-GNI\" href=\"cs_ccs.html\">Canadian Cancer Society: Girls Night In<\/a><\/li>");
document.write("<li class=\"menuItem\"><a id=\"DFC-MP\" href=\"cs_dfc_mp.html\">Dairy Farmers of Canada: Menu Planner<\/a><\/li>");
document.write("<li class=\"menuItem\"><a id=\"DOVE-SSR\" href=\"cs_dove_summit.html\">Dove Summit Showcase Reel<\/a><\/li>");
document.write("<li class=\"menuItem\"><a id=\"MAYTAG-100\" href=\"cs_maytag_100years.html\">Maytag: Celebrating 100 Years Exhibit<\/a><\/li>");
document.write("<li class=\"menuItem\"><a id=\"NUTELLAL-TBT\" href=\"cs_nutella_tbt.html\">Nutella: The Breakfast Table<\/a> <\/li>");
document.write("<li class=\"menuItem\"><a id=\"VASELINE-STE\" href=\"cs_vaseline.html\">Vaseline Science of Touch Exhibit<\/a> <\/li>");
document.write("<li class=\"menuItem\"><a id=\"WPL-GGC\" href=\"cs_whirlpool_ggc.html\">Whirlpool<sup>®<\/sup>: Great Green Challenge<\/a><\/li>");
document.write("<li class=\"menuItem\"><a id=\"WPL-NRL\" href=\"cs_whirlpool.html\">Whirlpool<sup>®<\/sup>: New Rules of Laundry<\/a> <\/li>");

document.write("<\/ul>");

document.write("<\/div>");


}


// display sliding menu


function writeNav(){
document.write(" <div id=\"newDesignBtn\" class=\"newButtonClass\"><a href=\"design_what_we_do.html\" onMouseOver=\"newRollOver('newDesignMenu');\" onMouseOut=\"newRollOut('newDesignMenu');\">DESIGN<\/a><\/div>");
document.write("    <div id=\"newDesignMenu\" class=\"mainMenuLinks pinkMenu\">");
document.write("        <ul>");
document.write("            <li class=\"first\"><a href=\"design_what_we_do.html\">What We Do<\/a><\/li>");
document.write("            <li><a href=\"design_design4women.html\">Design For Women<\/a><\/li>");
document.write("            <li><a href=\"design_beyond_the_logo.html\">Beyond the Logo&trade;<\/a><\/li>");
document.write("        <\/ul>");
document.write("    <\/div>");
document.write("    <div id=\"newCultureBtn\" class=\"newButtonClass\"><a href=\"culture_about_bing.html\" onMouseOver=\"newRollOver('newCultureMenu');\" onMouseOut=\"newRollOut('newCultureMenu');\">CULTURE<\/a><\/div>");
document.write("    <div id=\"newCultureMenu\" class=\"mainMenuLinks purpleMenu\">");
document.write("        <ul>");
document.write("            <li class=\"first\"><a href=\"culture_about_bing.html\">About Bing Creative<\/a><\/li>");
document.write("            <li><a href=\"culture_bing_process.html\">The Bing Creative Design Process<\/a><\/li>");
document.write("        <\/ul>");
document.write("    <\/div>");
document.write("    <div id=\"newDialogueBtn\" class=\"newButtonClass\"><a href=\"dialogue.html\" onMouseOver=\"newRollOver('newDialogueMenu');\" onMouseOut=\"newRollOut('newDialogueMenu');\">DIALOGUE<\/a><\/div>");
document.write("    <div id=\"newDialogueMenu\" class=\"mainMenuLinks blueMenu\">");
document.write("        <ul>");
document.write("            <li class=\"first\"><a href=\"dialogue.html\">Contact<\/a><\/li>");
document.write("        <\/ul>");
document.write("    <\/div>");
}




// -----------------------------------------------------------------------------------
//
//	BING CREATIVE - Custom JavaScripts
//	Part 3: Custom Background
//
// -----------------------------------------------------------------------------------


function setCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);

	}
	return null;
}

function deleteCookie(name) {
	createCookie(name,"",-1);
}


function selectBackground() {

		var randomnumber=Math.floor(Math.random()*4);
		var cookieEnabled=(navigator.cookieEnabled)? true : false;
		
		//if navigator,cookieEnabled is not supported
		if (typeof navigator.cookieEnabled=="undefined" && !cookieEnabled){ 
			document.cookie="testcookie";
			cookieEnabled=(document.cookie.indexOf("testcookie")!=-1)? true : false;
		}
	

		if (cookieEnabled)
		{
			if (randomnumber==1){
				var target = document.body;
				target.setAttribute("class", "bodyBG01");
				target.setAttribute("className", "bodyBG01"); // for IE which does not recognize "class"
				setCookie ( 'bgimage', 'one', 1 );
				return;
			}
		
			else if (randomnumber==2){
				var target = document.body;
				target.setAttribute("class", "bodyBG02");
				target.setAttribute("className", "bodyBG02"); // for IE which does not recognize "class"
				setCookie ( 'bgimage', 'two', 1 );
				return;
			}
			
			else{
				var target = document.body;
				target.setAttribute("class", "bodyBG03");
				target.setAttribute("className", "bodyBG03"); // for IE which does not recognize "class"
				setCookie ( 'bgimage', 'three', 1 );
				return;
			}
		}
		else{
			var target = document.body;
			target.setAttribute("class", "bodyBG03");
			target.setAttribute("className", "bodyBG03"); // for IE which does not recognize "class"
			return;
		}


		
}

function setBackground() {

	var x = readCookie ( "bgimage" );
	var cookieEnabled=(navigator.cookieEnabled)? true : false;
	
	//if navigator,cookieEnabled is not supported
	if (typeof navigator.cookieEnabled=="undefined" && !cookieEnabled){ 
		document.cookie="testcookie";
		cookieEnabled=(document.cookie.indexOf("testcookie")!=-1)? true : false;
	}
	
	if (cookieEnabled) {
		if (x=='one'){
			var target = document.body;
			target.setAttribute("class", "bodyBG01");
			target.setAttribute("className", "bodyBG01"); // for IE which does not recognize "class"
			return;
		} else if (x=='two'){
			var target = document.body;
			target.setAttribute("class", "bodyBG02");
			target.setAttribute("className", "bodyBG02"); // for IE which does not recognize "class"
			return;
		} else {
			var target = document.body;
			target.setAttribute("class", "bodyBG03");
			target.setAttribute("className", "bodyBG03"); // for IE which does not recognize "class"
			return;
		}
	} else {
			var target = document.body;
			target.setAttribute("class", "bodyBG03");
			target.setAttribute("className", "bodyBG03"); // for IE which does not recognize "class"
			return;
	}
	
}


// -----------------------------------------------------------------------------------
//
//	BING CREATIVE - Custom JavaScripts
//	Part 4: Portfolio Thumbnail Highlighting
//
// -----------------------------------------------------------------------------------

function highlightThumbnails() {
	$(document).ready(function(){
	
		$(function(){
			$(".thumbnailLink").tipTip({maxWidth: "160px", defaultPosition: "top", delay: 0, edgeOffset:-3});
		});
		$(function(){
			$(".descriptionLink").tipTip({maxWidth: "250px", defaultPosition: "right", delay: 0, edgeOffset:-6});
		});
		$(function(){
			$(".thumbnailLinkImage").tipTip({maxWidth: "130px", defaultPosition: "top", delay: 0, edgeOffset:-3});
		});
		
		$(".thumbnail").hover(
		  function () {
			$(this).children('a').addClass('thumbnailLinkActive');
		  },
		  function () {
			$(this).children('a').removeClass('thumbnailLinkActive');
		  }
		);
					 
	});	
}
