//function to fix the IE rollover 
function fixIE6flicker(fix) {
	try {
		document.execCommand("BackgroundImageCache", false, fix);
	}
	catch(err) { }
}

function fadeHomePhoto() {
//	$(".splashpage .photo img:eq(0)").css('display','block');
	$(".splashpage .photo").fadeIn(1000);
	$(".splashpage .photo").cycle({ 
		delay:2000,
		timeout:3000, 
		speed:1000, 
		sync:1
	});
}

function displayPortfolio() {
	$('#flashportfolio').flash({
		swf: '/includes/flash/portfolio.swf',
		height:192,
		width:810
	});
	
	if($('.frame').length >= 1 ){
		$('.frame:first').css('display','block');
		$('.frame:first').addClass('active');
	}
	var pContent=$('.frame');
	var pContentHeight=0;
	for(var i=0;i<pContent.length;i++){
		if(pContent.eq(i).height()>pContentHeight){
			pContentHeight=pContent.eq(i).height();
		}
	}
	pContentHeight+="px";
	$("#portfoliocontent").height(pContentHeight);
	
	for(var i=0;i<pContent.length;i++){
		pContent.eq(i).height(pContentHeight);
		pContent.eq(i).css('background','#fff');
	}
}

//Attempt to get this darn portfolio working as expected
var curZIndex = 20; //Increment z-index to roll in-out
function showPortfolio(newFrame){
	var pContent = $('.frame');
	var activeFrame = -1;
	
	var i = 0;
	while ((activeFrame == -1) && (i < pContent.length)){
		if(pContent.eq(i).hasClass('active')){
			pContent.eq(i).removeClass('active');
			pContent.eq(newFrame).addClass('active');
			activeFrame = i;
		}
		i++;
	}
	
	//scroll out content from left to right
	if(newFrame<activeFrame){
		pContent.eq(newFrame).css('display','block');
		pContent.eq(activeFrame).css('z-index', curZIndex);
		pContent.eq(newFrame).css('z-index', curZIndex - 1);
		pContent.eq(newFrame).css('left','0px');
		pContent.eq(activeFrame).animate({left:'710px'},1000);
	}
	//scroll in content from right to left
	if(newFrame>activeFrame){
		pContent.eq(newFrame).css('display','block');
		pContent.eq(activeFrame).css('z-index', curZIndex++);
		pContent.eq(newFrame).css('z-index', ++curZIndex);
		pContent.eq(newFrame).css('left','710px');
		pContent.eq(newFrame).animate({left:'0'},1000);
	}
	
}

function centerAbout() {
	var photoH = $(".about:eq(1) .photo").height();
	var img01H = $(".about:eq(1) img").height();
	var img02H = $(".about:eq(2) img").height();
	var img01Pad = Math.round((photoH-img01H) / 2);
	var img02Pad = Math.round((photoH-img02H) / 2);
	$(".about:eq(1) img").css("margin-top",img01Pad);
	$(".about:eq(2) img").css("margin-top",img02Pad);
}

function displayBanner() {
	if($("#body-class").text().length > 0){
		$('body').addClass($("#body-class").text());
	}
	else{
		if($("#flashportfolio").length == 0){
			$('body').addClass("banner_face01");
		}
	}
}

/* Lastchild functionality - for IE */
function addLast() {
	$("#footer .nav a:last-child").addClass("last");
	$("#footer .vcard:last-child").addClass("last");
	$(".audiences p.audiencetype:last-child").addClass("last");
//	$(".about:last-child").addClass("last");
//	$(".threecol:last-child").addClass("last");
}

/* Focus for tabbing nav - for IE */
function addFocus() {
	$("#pnav li a").focus(function() {
		$(this).addClass("focus");
	});
	$("#pnav li a").blur(function() {
		$(this).removeClass("focus");
	});
}

/* Button for Login - for IE */
function hoverSubmit() {
	$("#login span.submit").hover(
		function() {
			$("#login").addClass("hover");
		},
		function() {
			$("#login").removeClass("hover");
		}
	)
	$("#registration span.submit").hover(
		function() {
			$("#registration").addClass("hover");
		},
		function() {
			$("#registration").removeClass("hover");
		}
	)
};

/* Button for Webinars and Whitepapers - for IE */
function hoverViewRead() {
	$(".item .link a .viewbutton").hover(
		function() {
			$(this).addClass("hover");
		},
		function() {
			$(this).removeClass("hover");
		}
	)
	$(".item .link a .readbutton").hover(
		function() {
			$(this).addClass("hover");
		},
		function() {
			$(this).removeClass("hover");
		}
	)


};

/* Open lightbox for unsubscribe confirmation */
function unsubscribe() {
	var unsubscribe = false;
	if($('body.splashpage').length){
		if($.jqURL.get('unsubscribe')){
			tb_show('', '#TB_inline?height=300&width=400&inlineId=unsubscribe', null)
			var lightboxModal = $(".modal");
			var h = lightboxModal.parent().css('height');
			$(".modal").css("height",h);
		}
	}
};

/* Remove default field value when element is focused*/
function toggleFieldDefaults() {
	$("#login .text").toggleVal();
}

function sizeModal() {
	$('.thickbox').click(function(){
		var lightboxModal = $(".modal");
		var h = lightboxModal.parent().css('height');
		$(".modal").css("height",h);
	})
}

function showAddress() {
	$("#footer .organization-unit a").click(function(e){
		e.preventDefault();
		var vcard = $(this).parents(".vcard");
		var vcard_collection = $(".vcard");
		for(var i=0;i<vcard_collection.length;i++){
			vcard_collection.eq(i).removeClass("active");
		}
		vcard.addClass("active");
	})
}

function setPrimarynav() {
	var currentPage = $.jqURL.url();
	var siteSections = new Array("/our-work/","/case-studies/","/resources/","/about-us/");
	var nav = $("#pnav").children();
	for(i=0;i<nav.length;i++){
		if(currentPage.indexOf(siteSections[i])>=0){
			nav.eq(i).addClass("active");
		}
	}
}

function setSubnav() {
	var currentPage = $.jqURL.url();
	var nav = $("#pnav").children();
	var subnav = $("#snav01,#snav02,#snav03,#snav04");
	var ourwork = ["/our-work/highlights.asp","/our-work/audiences.asp","/our-work/services.asp"];
	var casestudies = ["/case-studies/fertility-journey.asp","/case-studies/oncologystat.asp","/case-studies/bayer-diagnostics.asp","/case-studies/nuvaring.asp","/case-studies/sympatico-msn.asp"];
	var resources = ["/resources/whitepapers.asp","resources/vote_thankyou.asp"];
	var about = ["/about-us/index.asp","/about-us/contact-us.asp"];
	var subnavArray = [ourwork,casestudies,resources,about];
// show appropriate subnav
	for(i=0;i<nav.length;i++){
		if(nav.eq(i).hasClass("active")){
			subnav.eq(i).addClass("enabled");
			for(j=0;j<subnavArray[i].length;j++){
				if(currentPage.indexOf(subnavArray[i][j])>=0){
					var subnavList = subnav.eq(i).children();
					subnavList.eq(j).addClass("active");
					//edge cases
					//page that should fall under an existing nav:
					//	webinar vote's thank you page
					
					if((i==2) && (j==2)){
						subnavList.eq(0).addClass("active");
					}
					
				}
			}
		}
	}
}

$(document).ready(function(){
	fixIE6flicker(true);
	fadeHomePhoto();
	displayPortfolio();
	displayBanner();
	centerAbout();
	addLast();
	addFocus();
	hoverSubmit();
	hoverViewRead();
	sizeModal();
	showAddress();
	setPrimarynav();
	setSubnav();
});
