
// Supress BOM Script Errors	

function errorsuppressor(){

	return true;

}

if(location.href.indexOf("localhost") == -1) {

	window.onerror = errorsuppressor;

}

/* ====== jQuery functions ====== */
var $j = jQuery.noConflict();
if($j.cookie("DCAREERS_TXT")) { // For Text Resizing

	$j("#content").addClass($j.cookie("DCAREERS_TXT"));
	
}

$j(function() {

	/* ====== Text Resize ====== */

	// Get saved text size

	if($j.cookie("DCAREERS_TXT")) {

		$j("#content").addClass($j.cookie("DCAREERS_TXT"));
		$j("body").addClass($j.cookie("DCAREERS_TXT"));
	
	}

	// Dynamically insert text resize button

	$j("#t4").after('<li id="text-size"><a title="Change Text Size">Change Text Size</a></li>');

	// Set new text size

	resizeme = function(textSize) {

		var newSize = textSize;

		// We need to make sure we can movie the size to next in line depending on what cookie value is set

		var currentSize = $("#content").attr("class");

		if (currentSize == "medium") {

			newSize = "large";

		} else if (currentSize == "large") {

			newSize = "small";

		} else if (currentSize == "small") {

			newSize = "medium";

		}

		$j("#content").removeClass().addClass(newSize);
		$j("body").removeClass(currentSize).addClass(newSize);
		$j.cookie("DCAREERS_TXT",newSize, { path: "/", expires: 10000 });

		return false;

	};	

	// Toggle through each side

	$j("#text-size a").toggle (

		function() {

			resizeme("medium");

		}, function() {

			resizeme("large");

		}, function() {

			resizeme("small");

		}
	);

	if (document.getElementById("gateway")) { // Begin: If is gateway page

		/* ====== "Learn more" link on gateway page ====== */

		$j("#about").append('<span></span>');

		var aboutbox = $j("#about").height() + 5;

		$j("#save a").click(function() {

			$j("#about").appendTo("body");
			$j("#about").toggle().css({

				top: $(this).offset().top - aboutbox, 
				left: $(this).offset().left - 30

			});

			return false;

		});

		$j("#about a.close").click(function() {

			$j(this).parent().hide();
			return false;

		});

	} else {
	
		if (!$j("body#session_error").length) //Exit
		if (!$j(".antisocial").length) // Exit

			// Facebook Like Button

			if ($j("body.site-careers").length) { // RULE: Careers Template Social - Only show social buttons on careers site

				$j("#aspnetForm").prepend("<div id=\"fb-root\"></div>"); // Add necessary div for FB to sniff for

				if ($j(".pagecallout").length) {

					$j(".pagecallout").before("<fb:like id=\"like-button-heading\" ref=\"top_heading\" send=\"true\" show_faces=\"false\"></fb:like>");

				}

				$j("#secondarycontent").append("<fb:like id=\"like-button-content\" ref=\"bot_left\" send=\"true\" show_faces=\"false\"></fb:like>"); // Create like button and append to secondarycontent
				$j("#ctl00_tertiarynav").prepend("<fb:like id=\"like-button-tnav\" layout=\"button_count\" ref=\"top_right\" show_faces=\"false\"></fb:like>"); // Create like button and prepend to tertiarynav

				

				var shortTitle = $j("#metatitle").attr("content").substring(0, 100); // Grab title from og:title meta, shorten to 100 characters (so it can be retweeted)
				var tweetbtn = "<a href=\"http://twitter.com/share\" class=\"twitter-share-button\" data-text=\"" + shortTitle + "\" data-count=\"horizontal\" data-via=\"deloitte\" data-lang=\"en\">Tweet</a>";

				$j("#secondarycontent").append(tweetbtn);
				$j("#ctl00_tertiarynav").prepend(tweetbtn);

				if ($j(".pagecallout").length) {

					$j("#like-button-heading").after(tweetbtn + "<br/><br/>");

				}

				

				window.fbAsyncInit = function() {

					FB.init({appId: "134262293278270", status: true, cookie: true, xfbml: true}); // Deloitte Careers under Hugo Plasternaff

				};

					(function() {

						var e = document.createElement("script"); 
						e.async = true;
						e.src = document.location.protocol + "//connect.facebook.net/en_US/all.js";
						document.getElementById("fb-root").appendChild(e);

					}()

				);

			} // End: Careers Template Social

		/* ====== Widgets ====== */

		// HACK: Hijacking YouTubes UI a bit because it's C-R-A-Z-Y

		resetYouTubeVideo = function() {

			if (document.getElementById("videobar")) {

				$j("#videobar").scrollTop(0);
				// $j(".resultDiv_gsvb").removeAttr("title");
				$j(".playing_gsvb").removeAttr("style").removeClass("playing_gsvb").addClass("idle_gsvb");
				$j(".player_gsvb").remove();

			}

		}

		/* ====== Social Buttons (Hide/Show) ====== */

		// Can't set top position of widget in CSS because amount of widget li's is dynamic so we have to force it in JS instead - what a pain

		var iconHeight = $j("#social-network li > a").height();

		$j("#social-network li").each(function() {

			$j(this).children(".widget").css("top", "-" + iconHeight * ($j(this).index() + 1) + "px"); // Height of <a> tag multiplied by number in list

		});

		$j("#social-network ul li a.button").click(function() {

			$j("#settings-toolbar").fadeOut("fast");
			$j(this).next().animate({"right" : "50px"}, "medium"); // and show only this widget
			$j("#social-network").animate({"right" : "-50px"}, "fast");

			return false;

		});

		// Close Button

		$j(".widget").append('<p class="close"><a href="#">Close</a></p>');

		$j(".widget p.close a").click(function () {

			resetYouTubeVideo();
			$j(this).parent().parent().animate({"right":"-285px"});
			$j("#social-network").animate({"right":"0"});

			return false;

		});

		$j("#settings-toolbar").append('<p class="close"><a href="#">Close</a></p>');

		$j("#change-location > a").click(function() {

			resetYouTubeVideo();
			$j(".widget").animate({"right":"-285px"});
			$j("#social-network").animate({"right":"0"});
			$j("#settings-toolbar").fadeIn("fast");
		
			return false;

		});

		$j("#settings-toolbar p.close a").click(function(){

			$j(this).parent().parent().fadeOut("fast");
			return false;

		});

	}; // End: If is gateway page

});


// Multiple Onload Utility

function addLoadEvent(func) {

	var oldonload = window.onload;

	if (typeof window.onload != 'function') {

		window.onload = func;

	} else {

		window.onload = function() {

			oldonload();
			func();

		}

	}

}

// Insert-after Function

function insertAfter(newElement, targetElement) {

	var parent = targetElement.parentNode;

	if (parent.lastChild == targetElement) {

		parent.appendChild(newElement);

	} else {

		parent.insertBefore(newElement, targetElement.nextSibling);

	}

}

// Frame Buster

if (window.name == 'careersdeloitte_frameMain') {

		window.top.location.href = window.location.href;

}

// Back to top

function scrollToTop() {

	scroll(0, 0);

}

// Prep link and toggle displays

function prepElements(page, parent, child) {

	if (!document.getElementById(page)) return false;

	var linkid = document.getElementById(parent);

	document.getElementById(child).style.display = "none";

	linkid.onclick = function() {

		toggleElement(child, this);
		return false;

	}

}

// Prep transcript in medialibrary

function prepMediaLibrary() {

	prepElements("ctl00_primarycontent_accessibility", "ctl00_primarycontent_accessibility", "ctl00_primarycontent_transcriptcontent");

}

addLoadEvent(prepMediaLibrary);

// Check for children tables in datagrids

function prepMultipleGrids() {

	var get_grid = document.getElementsByTagName("table");

	for (var i=0; i < get_grid.length; i++) {

		if (get_grid[i].className == "datagridchild") {

			get_grid[i].style.display = "none"

		}

	}

}

// Toggle, Show/Hide Element(s)

function toggleElement(val, obj){

	if(document.getElementById(val).style.display == "none") { 

		obj.className = "expandon";
		document.getElementById(val).style.display = "block";

	} else {

		obj.className = "expand";
		document.getElementById(val).style.display = "none";
	}

}

// Global New-windows (parse through DOM, if viewpage class exists, append event)

function newWinLinks() {

	for (var i=0; i < document.links.length; i++) {

		if (document.links[i].className == "viewpage") {

			document.links[i].onclick = displayPage;

		}

		if (document.links[i].className == "viewmedia") {

			document.links[i].onclick = displayMedia;

		}

		if (document.links[i].className == "viewfeature") {

			document.links[i].onclick = displayFeature;

		}

		if (document.links[i].className == "newwindow") {

			document.links[i].onclick = displayNewWindow;

		}

	}

}

addLoadEvent(newWinLinks);

function displayPage() {

	var pageWindow = window.open(this.href,"win", 'toolbar=1,location=0,directories=0,status=0,menubar=1,scrollbars=yes,resizable=1,top=100,left=200,width=820,height=500');
	pageWindow.focus();
	return false;

}

function displayMedia() {

	var mediaWindow = window.open(this.href,"win", 'toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=none,resizable=1,top=300,left=475,width=363,height=180');
	mediaWindow.focus();
	return false;

}

function displayFeature() {

	modalWindow.windowId = "viewfitfactor";
	modalWindow.width = 785;
	modalWindow.height = 499;
	modalWindow.content = "<iframe width=\"785\" height=\"499\" frameborder=\"0\" scrolling=\"no\" allowtransparency=\"true\" src=\"" + this.href + "\">&lt/iframe>";
	modalWindow.open();
	return false;

}

function displayNewWindow() {

	var featureWindow = window.open(this.href,"win", 'toolbar=1,location=1,directories=1,status=1,menubar=1,scrollbars=yes,resizable=1,top=100,left=100,width=800,height=600');
	featureWindow.focus();
	return false;

}

function displayContent() {

	var temp = new Array();
	temp = this.coords.split(',');

	var regExp = /\s+/g;

	w = temp[0].replace(regExp,'');
	h = temp[1].replace(regExp,'');

	modalWindow.windowId = "viewcontent";
	modalWindow.width = w;
	modalWindow.height = h;
	modalWindow.content = "<iframe width=\"" + w + "\" height=\"" + h + "\" frameborder=\"0\" scrolling=\"no\" allowtransparency=\"true\" src=\"" + this.href + "\">&lt/iframe>";

	modalWindow.open();
	return false;

}

function showModal(link, width, height, button) {

	url = link.href;

	modalWindow.windowId = "client-content";
	modalWindow.width = width;
	modalWindow.height = height;
	modalWindow.content = "<iframe width=\"" + width + "\" height=\"" + height + "\" frameborder=\"0\" scrolling=\"no\" allowtransparency=\"true\" src=\"" + url + "\">&lt/iframe>";

	modalWindow.open();

	if (!button) {

		document.getElementById("close-iframe").style.display = "none";

	}

}

// Brings Reset Buttons back to top of page

function resetPage() {

	var inputelemnt = document.getElementsByTagName("input");

	for (var i=0; i < inputelemnt.length; i++) {

		if (inputelemnt[i].getAttribute("type") == "reset") {

			inputelemnt[i].onclick = function() {

				window.location = window.top.location;

			}

		}

	}

}

addLoadEvent(resetPage);

// Unobtrusive Print

function printDoc() {

	if (!document.getElementById("t4")) return false;

	if (document.getElementById("ctl00_t1")) {	

		var t1 = document.getElementById("ctl00_t1");
		t1.style.right = "177px";

	}

	var t2 = document.getElementById("t2");
	t2.style.right = "141px";

	var item = document.createElement("li");
	var anchor = document.createElement("a");
	var strtxt = document.createTextNode("Print Page");

	item.setAttribute("id", "t3");
	anchor.setAttribute("title", "Print Page");

	item.appendChild(anchor);
	anchor.appendChild(strtxt);

	anchor.onclick = function() {

		print();
		dcsMultiTrack("DCS.dcssip", location.hostname,"DCS.dcsuri",location.href + "/PrintPage","WT.ti","PrintPage"); //WebTrends Print Tracking
		return false;

	}

	var lastnode = document.getElementById("t4");
	var parentnode = lastnode.parentNode;

	parentnode.insertBefore(item, lastnode);

}

addLoadEvent(printDoc);

// Deatil Page Print

function printDetails() {

	if (!document.getElementById("getnews")) return false;

	var para = document.createElement("li");
	var anchor = document.createElement("a");
	var strtxt = document.createTextNode("Print Page");

	para.setAttribute("id", "pageprint");

	para.appendChild(anchor);

	anchor.appendChild(strtxt);

	anchor.onclick = function() {

		print();
		dcsMultiTrack("DCS.dcssip", location.hostname,"DCS.dcsuri",location.href + "/PrintPageDetail","WT.ti","PrintPageDetail"); //WebTrends Print Tracking
		return false;

	}

	var lastnode = document.getElementById("getnews");
	var parentnode = lastnode.parentNode;

	parentnode.insertBefore(para, lastnode);

}

addLoadEvent(printDetails);

// Close Window

function closeWindow() {

	if (!document.getElementById("subpage")) return false;

	var para = document.createElement("p");
	var link = document.createElement("a");
	var text = document.createTextNode("Close Window");

	para.setAttribute("id", "closewindow");
	para.appendChild(link);
	link.appendChild(text);

	link.onclick = function() {

			window.close();
			return false;

	}

	var container = document.getElementById("subpage");
	container.appendChild(para);

}

addLoadEvent(closeWindow);

// History Links

function pageJump(pageto) {

	window.history.go(pageto);
	return false;

}

// Candidate Enhancements (Going Bye-bye the second the ELOAS issue is cleared up)

function displaySWFContent() {

	if (!document.getElementById) return false;
	if (!document.getElementById("objectcontentactive")) return false;

	var container = document.getElementById("objectcontentactive");
	var containerid = container.getAttribute("id");
	var body = document.body;
	var bodyid = body.getAttribute("id");
	var bodyclass = body.className;

	if (!container) return false;

	if (bodyclass == "global en global-en global-en-university site-careers") {

		var filepath = "/global/_flash/home/students/new-default.swf";
		var filewidth = "606";
		var fileheight = "220";
		var bgcolor = "#000000";
		var videofile = "";
		var aspect = "exactfit"
		var basepath = "/global/_flash/home/students";
		var elementid = containerid;
		var showflash = true;

	} else if (bodyid == "meetus" && bodyclass == "global en global-en global-en-experienced x-page sub site-careers" || bodyclass == "global en global-en global-en-university x-page sub site-careers") {

		var filepath = "/global/_flash/meetus/mainmovie.swf";
		var filewidth = "650";
		var fileheight = "198";
		var bgcolor = "#ffffff";
		var basepath = "/global/_flash/meetus";
		var csspath = "../../_templates/default/css/flash.css";
		var elementid = containerid;

		

		var xmlpath = "../../_xml/meetus-flash-experienced.xml";

		

		var showflash = true;

	} else if (bodyid == "meetus" && bodyclass == "mx es mx-es mx-es-experienced x-page sub" || bodyclass == "mx es mx-es mx-es-university x-page sub") {

		var filepath = "/content/MX/meetus/mainmovie.swf"; // Nice Flash - IT DOES NOTHING!
		var filewidth = "750";
		var fileheight = "300";
		var bgcolor = "#ffffff";
		var elementid = containerid;
	 	var showflash = true;

	} else if (bodyid == "experienced") {

		var filepath = "/global/_flash/home/experienced/new-default.swf";
		var filewidth = "606";
		var fileheight = "220";
		var bgcolor = "#000000";
		var videofile = "";
		var aspect = "exactfit"
		var basepath = "/global/_flash/home/experienced";
		var elementid = containerid;

		//Hack....done because there is no country called "Global"

		if (bodyclass == "global en global-en global-en-experienced site-careers") {

			var showflash = true;

		}
	}

	var so = new SWFObject(filepath, "app_movie", filewidth, fileheight, "6", bgcolor);
	so.useExpressInstall('/global/_flash/flash_expressinstall.swf');
	so.addParam("menu", "false");
	so.addParam("base", basepath);
	so.addParam("wmode", "opaque");
	so.addParam("scale", aspect);
	so.addParam("allowScriptAccess", "sameDomain");
	so.addVariable("css_url", csspath);
	so.addVariable("xmlfilepath", xmlpath);
	so.addVariable("filepath", videofile);
	so.addVariable("loop", "1");
	so.setAttribute("style", ";");

	if (showflash == true) {

		so.write(elementid);

	}

}

// Just appends "addthis" to desired element (global)

function appendAddThis() {

	if (!document.getElementById("t4")) return false;

	var list_addthis = document.getElementById("t4").getElementsByTagName("a")[0];

	list_addthis.onmouseover = function() {

		return addthis_open(this, '', '[URL]', '[TITLE]');

	}

	list_addthis.onmouseout = function() {

		addthis_close();

	}

	list_addthis.onclick = function() {

		return addthis_sendto();
		return false;

	}

}

addLoadEvent(appendAddThis);

// Addthis variables (global)

var addthis_pub = "deloittejobs";
var addthis_brand = "careers.deloitte.com";
var addthis_options = "delicious, digg, email, favorites, facebook, fark, furl, google, live, myweb, newsvine, reddit, stumbleupon, technorati, twitter, more";
var addthis_offset_top = -6;
var addthis_offset_left = -208;

// Add title attribute to all select boxes for usability (global)

function addOptionTitle() { 

	if (!document.getElementsByTagName("select")) return false;

	var list = document.getElementsByTagName("select");

	for(var i = 0; i < list.length; i++) {

		var option = list[i].getElementsByTagName("option")

		for(var j = 0; j < option.length; j++) {

			option[j].setAttribute("title", option[j].text);

		}
	}
}

addLoadEvent(addOptionTitle);

// Load Location Map (locationfinder.aspx)

function loadLocationMap() {

	if (!document.getElementById("locationfinder")) return false;

	var so = new SWFObject("file_download.aspx?FileName=world_map.swf&FileSource=AF&FileID=swf", "mapview", "600", "300", "8", "#ffffff");
	so.useExpressInstall('/global/_flash/flash_expressinstall.swf');
	so.addParam("menu", "false");
	so.addParam("wmode", "opaque");
	so.setAttribute("style", ";");
	so.write("mapview");

}

addLoadEvent(loadLocationMap);
