$(document).ready(function() {
						   
	$("a[href$=.jpg],a[href$=.png],a[href$=.gif]").fancybox({
		'titleFormat': formatTitle	
	}); 
	
	
	$("a.video-fancybox").fancybox({
			'padding': 0,
			'autoScale': false,
			'titleShow': false,
			'width': 507,
			'height': 320,
			'swf': {
				'allowfullscreen': 'true',
				'wmode': 'transparent'
			}						   
	});
		
	if($("a.video-fancybox").length){
		setVideoOverlays();
	}
	
	if ($('div#xml-news').length) {
		$xmlNews = $('div#xml-news').html();
		parseNewsXML($xmlNews);
	}
    
    // Load the people finder carousel
	if($('div.people-finder-carousel div').length){
		$numberPeople = $('div.people-finder-carousel div ul li').length;
		if($numberPeople < 6){
			$('div.people-finder-carousel span.prev-people').css('display', 'none');
			$('div.people-finder-carousel span.next-people').css('display', 'none');
			return false;			
		}
		$('div.people-finder-carousel div').jCarouselLite({
			visible: 6,
			timeout: 2800,
            auto: false,
            circular: true,
			btnNext: ".next-people",
			btnPrev: ".prev-people"
		});	
	}
    
	// Set tabindex on all form elements and links
	var tabindex = 1;
	$('input,select,a').each(function() {
		if (this.type != "hidden") {
			var $input = $(this);
			$input.attr("tabindex", tabindex);
			tabindex++;
		}
	});
	
	$('div.hidden-project-container div.hidden-project').each(function(index, element){
		$(element).attr('id', 'hidden-project' + index)
	});
	
	$('div.content-home-banner img').css('display', 'none');
	
	if($('#project-team').length){
		setprojectTeam();
	}

});

function formatTitle(title, currentArray, currentIndex, currentOpts) {
    return '<div id=\'fancybox-title-main\'><p>' + (title && title.length ? '' + title + '' : '' ) + '</p></div><div class=\'image-number\'>Image ' + (currentIndex + 1) + ' / ' + currentArray.length + '</div>';
}


var xmlhttp;
var xmlPath = '++resource++d2012.theme/common/xml/deloitte2012.xml';
var activeProject = 0;

function setprojectTeam(){

	$('#project-team li a').each(function(index, element){
		var teamMemberName = $(element).find('span').eq(0).html();
		var teamMemberPosition = $(element).find('span').eq(1).html();
		var teamMemberLink = $(element).find('span').eq(2).html();
		$(element).removeClass('active');
		
		$(element).hover(function() {
			$('#project-team li a').removeClass('active');
			$(element).addClass('active');
			$('#project-team-details').html('<h4>' + teamMemberName + '</h4><p>' + teamMemberPosition + '</p><ul class=\'links\'><li><a href=\'' + teamMemberLink + '\' title=\'View profile page\'><span>VIEW PROFILE PAGE</span></a></li></ul>');
		}) ;
		
		if(index == '0'){
			$(element).addClass('active');
			$('#project-team-details').html('<h4>' + teamMemberName + '</h4><p>' + teamMemberPosition + '</p><ul class=\'links\'><li><a href=\'' + teamMemberLink + '\' title=\'View profile page\'><span>VIEW PROFILE PAGE</span></a></li></ul>');
		}
		
	});
	/*var teamMemberDetails = document.getElementById("project-team-details");
	var teamMemberList = document.getElementById("project-team").getElementsByTagName("li");
	
	for(i=0; i<teamMemberList.length; i++){
		
		var teamMemberName = teamMemberList[i].childNodes[0].childNodes[0].innerHTML;
		var teamMemberPosition = teamMemberList[i].childNodes[0].childNodes[1].innerHTML;
		var teamMemberLink = teamMemberList[i].childNodes[0].childNodes[2].innerHTML;

		

		//teamMemberList[i].childNodes[0].href = "javascript:showText(this);";
		//teamMemberList[i].childNodes[0].onmouseover = "showText(this, \""+ brandTitle + "\" , \""+ brandDetails + "\")" ;
		teamMemberList[i].childNodes[0].onmouseover = function() {   showText(this, teamMemberLink)   } ;
		if (i==0)
		{
		
			teamMemberDetails.innerHTML = "<h4>" + teamMemberName + "</h4> <p>" + teamMemberPosition + "</p> <ul class=\"links\"><li><a href=\"" + teamMemberLink + "\" title=\"View profile page\"><span>VIEW PROFILE PAGE</span></a></li></ul>";
		
		}
	}*/
}
function showText(param, teamMemberHREF){
	var teamMemberName = param.parentNode.childNodes[0].childNodes[0].innerHTML;
	var teamMemberPosition = param.parentNode.childNodes[0].childNodes[1].innerHTML;
	var teamMemberLink = param.parentNode.childNodes[0].href;
	
	
	
	var teamMemberDetails = document.getElementById("project-team-details");
	var teamMemberList = document.getElementById("project-team").getElementsByTagName("li");
	
	for(i=0; i<teamMemberList.length; i++){
		teamMemberList[i].firstChild.className = "";
		//alert(teamMemberHREF);
	}
	param.className = "active";
	
	teamMemberDetails.innerHTML = "<h4>" + teamMemberName + "</h4> <p>" + teamMemberPosition + "</p> <ul class=\"links\"><li><a href=\"" + teamMemberLink + "\" title=\"View profile page\"><span>VIEW PROFILE PAGE</span></a></li></ul>";

}


function autoClearInputs(){
	var Inputs = document.getElementsByTagName('input'); 
	for(i=0;i<Inputs.length;i++){ 
		if (Inputs[i].value.length > 0 && Inputs[i].type == "text")
		{
			if(navigator.appName == "Microsoft Internet Explorer"){
				//IS doesnt like to set attributes in the normal way
				Inputs[i].setAttribute('rev',Inputs[i].value);
				Inputs[i].setAttribute('onfocus',function(){clearInput(this)});
				Inputs[i].setAttribute('onblur',function(){resetInput(this)});
			}else{
				Inputs[i].setAttribute("onfocus","javascript:if(this.value=='"+ Inputs[i].value +"')this.value='';")
				Inputs[i].setAttribute("onblur","javascript:if(this.value=='')this.value='" + Inputs[i].value + "';")
			}
		}
	}
}


function clearInput(ele){
	if (ele.value == ele.rev)
	{
		ele.value = "";
	}
}

function resetInput(ele){
	if (ele.value == "")
	{
		ele.value = ele.rev;
	}
}

window.onload = function(){
	initXML(null);
	// autoClearInputs();
	// setDefaultFontSize();
	try {
		//buildFlashBanner("content-home-banner", "/++resource++d2012.theme/common/swf/wide-banner.swf", "mainBanner", "100%", "100%", xmlPath);
		setBanner();
	} catch(err) {
		//assume this means "content-home-banner" was not found and we're not on the homepage
	}
}

function fixPNG(myImage){
	
    if ((version >= 5.5) && (version < 7) && (document.body.filters)) 
    {
		
       var imgID = (myImage.id) ? "id='" + myImage.id + "' " : ""
	   var imgClass = (myImage.className) ? "class='" + myImage.className + "' " : ""
	   var imgTitle = (myImage.title) ? 
		             "title=\"" + myImage.title  + "\" " : "title=\"" + myImage.alt + "\" "
	   var imgStyle = "display:block; margin-top:0px;" + myImage.style.cssText
	   var myParent = myImage.parentNode

		if (myParent.nodeName == "A"){
			var aHref=myParent.href;
			var aTarget=""
			var aStyle="style=\" display:block; width:" + myImage.width + "px; height:" + myImage.height + "px;\" "
			if (myParent.target !=="") {
				aTarget = "target=\""+ myParent.target + "\"";
			}
			var strNewHTML = "<span " + imgID + imgClass
				+ imgTitle + " style=\"" + "width:" + myImage.width 
				+ "px; height:" + myImage.height 
				+ "px;" + imgStyle + ";"
				+ "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader"
				+ "(src=\'" + myImage.src + "\', sizingMethod='image');\">"
				+ "<a " + aStyle +  "href=\""+aHref + "\" target=\""+ aTarget + "\" ><!-- --></a> </span>"
			myParent.outerHTML = strNewHTML
		}
	   else{
			var strNewHTML = "<span " + imgID + imgClass + imgTitle
				+ " style=\"" + "width:" + myImage.width 
				+ "px; height:" + myImage.height 
				+ "px;" + imgStyle + ";"
				+ "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader"
				+ "(src=\'" + myImage.src + "\', sizingMethod='image');\"></span>"
			myImage.outerHTML = strNewHTML
	   }
	   	  
    }
}

function buildFlash (divId, swfLoc, swfName, swfHeight, swfWidth, swfVar, swfVar2, swfVar3){
		var flashVersion = 8;
		
		// old school, not the proper way. swfobject was conflicting with the rotator when using so.addVariable
		if (swfVar){
			swfLoc = swfLoc + '?videoPath=' + swfVar + '&posterPath=' + swfVar2 + '&autoPlay=' + swfVar3;
		}
		
		var so = new SWFObject(swfLoc, swfName, swfWidth, swfHeight, flashVersion, "#FFFFFF");
		so.addParam("wmode", "transparent");
		so.addParam("scale", "noscale");
		so.write(divId);
}

function buildFlashBanner (divId, swfLoc, swfName, swfHeight, swfWidth, swfVar){
		var flashVersion = 8;

		// old school, not the proper way. swfobject was conflicting with the rotator when using so.addVariable
		if (swfVar){
			swfLoc = swfLoc + '?xmlPath=' +swfVar;
		}

		var so = new SWFObject(swfLoc, swfName, swfWidth, swfHeight, flashVersion, "#FFFFFF");
		so.addParam("wmode", "transparent");
		so.addParam("scale", "noscale");

		so.write(divId);
}

var browser = navigator.appName;
var ver = navigator.appVersion;
var thestart = parseFloat(ver.indexOf("MSIE"))+1;
var brow_ver = parseFloat(ver.substring(thestart+4,thestart+7));
var lightboxKiller;

if ((browser=="Microsoft Internet Explorer") && (brow_ver < 6))
{
	lightboxKiller = true;
}else{
	lightboxKiller = false;
}

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());
}

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 "";
}

// FONT SIZE CHANGER - COOKIE BASED
function changeFont(fontClass){
	var HTMLBody = document.getElementsByTagName("body")[0];     
	HTMLBody.className = fontClass;
	setCookie("DELOITTEFontSize", fontClass, 30);
}		

function setDefaultFontSize(){
	var fontSize = getCookie("DELOITTEFontSize")
	if(fontSize){
		var HTMLBody = document.getElementsByTagName("body")[0];    
		HTMLBody.className = fontSize;
	}
}

/* End Page.js */

function initXML(projectNumber){
	xmlhttp=null;
	if (window.ActiveXObject){// code for IE5 and IE6
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	}else 	if (window.XMLHttpRequest){// code for all new browsers
		xmlhttp=new XMLHttpRequest();
	
	}if (xmlhttp!=null){
		xmlhttp.onreadystatechange=state_Change; //calls function below 
		xmlhttp.open("GET",xmlPath,true);
		xmlhttp.send(null);
	}else{
		alert("Your browser does not support XMLHTTP.");
	}
}

function state_Change(){
	if (xmlhttp.readyState==4) {// 4 = "loaded"
		if (xmlhttp.status==200){// 200 = OK
			initCarousel(xmlhttp);
			if ( document.getElementById("content-home-banner")){
				loadAJAXContent(0)
			}
		}else{
			//alert("Problem retrieving XML data");
			return false;
		}
	}
}

function initCarousel(xmlhttp){

	// Set active state of first carousel item
	$('div.carousel div ul li:first-child').attr('class', 'active');
	
	
	$('div.carousel div ul li').each(function(index, element){
		$(element).attr('id', 'carousel-link' + index)
	});
	
	
	if($('div.carousel div').length){
		$('div.carousel div').jCarouselLite({
			visible: 6,
			timeout: 2800,
		    circular: true,
			btnNext: ".next",
			btnPrev: ".prev"
		});	
		$('div.carousel div ul li').each(function(index, element){
			$(element).hover(function(){
				$('div.carousel div ul li').removeClass('active');
				$(element).addClass('active');
				$projectID = $(this).attr('id').substring(13);
				changeProject($projectID);
			});
		});
	}
	
}

function loadAJAXContent(projectNumber)
{
	// Inserts Content Into Homepage Content Box
	
	var heading_main = $('div.hidden-project-container #hidden-project' + projectNumber).find('div.project-heading').html();
	var heading_sub = $('div.hidden-project-container #hidden-project' + projectNumber).find('div.project-subheading').html();
	var text = $('div.hidden-project-container #hidden-project' + projectNumber).find('div.project-text').html();
	var link_url = $('div.hidden-project-container #hidden-project' + projectNumber).find('div.project-url').html();
	var link_text = $('div.hidden-project-container #hidden-project' + projectNumber).find('div.project-url-text').html();
	
	
	$('#ajaxMainHeading').html('<h1 class="quote">' + heading_main + '</h1>');
	$('#ajaxSubHeading').html('<h2 class="sub-heading">' + heading_sub + '</h2>');
	$('#ajaxText').html('<p>' + text + '</p>');
	$('#ajaxLink').html('<ul class="links"><li><a href=\'' + link_url + '\' title=\'' + heading_main + '\'><span>' + link_text + '</span></a><li><ul>');
		
	//re-activate sifr
	sIFR.replace(times, {
		selector: 'h1',
		css: '.sIFR-root {  color: #193076; font-size:44px; leading:-4;}',
		wmode: 'transparent'
	});	

	sIFR.replace(times, {
		selector: 'h2.sub-heading',
		css: '.sIFR-root {  color: #9AC32F; font-size:44px; leading:-4;}',
		wmode: 'transparent'
	});
	
	changeBanner(projectNumber)

}


function changeProject(projectNumber){
	if (activeProject != projectNumber)
	{
		activeProject = projectNumber;
		if ( document.getElementById("content-home-banner")){
			loadAJAXContent(projectNumber)
		}
	}
}

function getFlashMovie(movieName) {
	var isIE = navigator.appName.indexOf("Microsoft") != -1;
	return (isIE) ? window[movieName] : document[movieName];
}

function sendToFlash(imageNumber) {
	getFlashMovie("mainBanner").toFlash(imageNumber);
}

/* News Tout Functions */

function parseNewsXML(xml){
	$('div.news-tout-container').html('');

	$ajaxTout = $(xml).find('div.tout');//Tony Rapa

	$ajaxTout.each(function(index, element){

		$ajaxHeading = $(this).find('div.heading').text();//Tony Rapa
		$ajaxIcon = $(this).find('div.icon').text();//Tony Rapa
		$ajaxDate = $(this).find('div.date').text();//Tony Rapa
		$ajaxParagraph = $(this).find('div.paragraph').text();//Tony Rapa
		if($ajaxParagraph.length > 80){
			$ajaxParagraph = $ajaxParagraph.substring(0, 80) + '...';	
		}
		else{
			 $ajaxParagraph = $ajaxParagraph.substring(0, 80);
		}
		$ajaxLink = $(this).find('div.linktext').text();//Tony Rapa
		$ajaxURL = $(this).find('div.linkurl').text();//Tony Rapa
		
		$ajaxHTML = '<div class="tout" id="ajax-tout' + index + '"><h3>' + $ajaxHeading + '</h3><img src="' + $ajaxIcon + '"><h4>' + 
					$ajaxDate + '</h4><p>' + $ajaxParagraph + '</p><ul class=\'links\'><li><a href="' + $ajaxURL + '"><span>' + $ajaxLink + '</span></a></li></ul></div>';
		
		$('div.news-tout-container').append($ajaxHTML);
		$('div.news-tout-container #ajax-tout0').addClass('active');

	});
	
	$('div.news-tout-container div.tout').css('opacity', 0.0);
	$('div.news-tout-container div.tout.active').css('opacity', 1.0);

	$toutFadeInterval = setInterval( 'ajaxToutSwitch()', 5000 );

	$('div.news-tout-container div.tout').mouseover(function(e){
		clearInterval($toutFadeInterval);
	});
	
	$('div.news-tout-container div.tout').mouseout(function(){
		$toutFadeInterval = setInterval( 'ajaxToutSwitch()', 5000 );
	});
}

function ajaxToutSwitch() {
    var $active = $('div.news-tout-container div.active');

    //if ( $active.length == 0 ) $active = $('div.news-tout-container div:last');
	if ( $active.length == 0 ) $active = $('div.news-tout-container #ajax-tout' + ($ajaxTout.length-1));

    // use this to pull the images in the order they appear in the markup
    var $next =  $active.next().length ? $active.next()
        //: $('div.news-tout-container div:first');
		: $('div.news-tout-container #ajax-tout0');

    // uncomment the 3 lines below to pull the images in random order
    
    var $sibs  = $active.siblings();
    var rndNum = Math.floor(Math.random() * $sibs.length );
    var $next  = $( $sibs[ rndNum ] );


    $active.addClass('last-active');

    $next.css({opacity: 0.0})
        .addClass('active')
        .animate({opacity: 1.0}, 1000, function() {
            $active.removeClass('active last-active');
    });
}

function setBanner(){
	
	$('#content-home-banner').html('<ul></ul>');
	
	//$('#content-home-banner ul').css('position', 'relative');
	
	$('div.hidden-project-container div.hidden-project').each(function(index, element){
		$bannerImage = $(element).find('div.project-image').html();
		$('#content-home-banner ul').append('<li style="background-image:url(\'' + $bannerImage  + '\'); background-repeat:no-repeat; background-position:top center;"></li>')
		
		if(index != '0'){
			$('#content-home-banner').find('li').eq(index).css('display', 'none');
		}
	});
	
	
	
	$('#content-home-banner').find('li').css('position', 'absolute');
	$('#content-home-banner').find('li').css('height', '365px');
	$bannerWidth = $(window).width();
	$leftOffset = ($bannerWidth - 1000) / 2;
	$('#content-home-banner').find('li').css('width', $bannerWidth + 'px');
	$('#content-home-banner').find('li').css('left', '0px');
	
}

function changeBanner(projectNumber){
	$('#content-home-banner').find('li').each(function(index, element){
		if(index != projectNumber){
			$(element).fadeOut(1000, 'easeInQuad');
		}
		else if(index == projectNumber){
			$(element).fadeIn(1000, 'easeInQuad');
		}
	});
}

function setVideoOverlays(){
	$("div.page-text a.video-fancybox").append('<span></span>')
	
	$("div.page-text a.video-fancybox span").each(function(index, element){
		$imageHeight = $(element).parent().find('img').height();
		$imageWidth = $(element).parent().find('img').width();
		
		//alert('Dimensions' + $imageHeight + ' - ' + $imageWidth)
		$("div.page-text a.video-fancybox span").css('height', $imageHeight + 'px');
		$("div.page-text a.video-fancybox span").css('width', $imageWidth + 'px');
		$("div.page-text a.video-fancybox span").css('left', - $imageWidth + 'px');
		
	});

	if($("div.page-text-error a.video-fancybox").length){

		$("div.page-text-error a.video-fancybox").append('<span></span>')
		
		$("div.page-text-error a.video-fancybox span").each(function(index, element){
			$imageHeight = $(element).parent().find('img').height();
			$imageWidth = $(element).parent().find('img').width();
			
			$("div.page-text-error a.video-fancybox span").css('height', $imageHeight + 'px');
			$("div.page-text-error a.video-fancybox span").css('width', $imageWidth + 'px');
			//$("div.page-text-error a.video-fancybox span").css('left', - $imageWidth + 'px');

			if($.browser.msie) {
			return false;
		   }
		   else{
			$("div.page-text-error a.video-fancybox span").css('top', - $imageHeight + 'px');
		   }			
		});
	}
	
}


$(window).resize(function() {
	if($('#content-home-banner').length){
		$bannerWidth = $(window).width();
		$('#content-home-banner').find('li').css('width', $bannerWidth + 'px');
		$('#content-home-banner').find('li').css('left', '0px');
	}
});



