//HELPER FUNCTIONS
//strips a pixel value to return an integer
var stripPx = function(pixels) {
	var num = parseInt(pixels.replace(/px/, ""));
	return num;
}

//GLOBAL FUNCTIONS
//AddThis Popup box
function addThis() {
	addthis.button(".addthis");
}
if (navigator.userAgent.indexOf("Firefox")!=-1) {
	var addthis_config = {
		ui_offset_top: -13
	//	ui_click: true,
	//	services_compact: "facebook, twitter, email, delicious, digg, more"
	};
}

//Hover Over Effects for Main and Footer Nav
function rollOver(element) {
	var oldImg = $(element).find("img").attr("src");
	var newImg = oldImg.replace(/_off/, "_over");
	$(element).find("img").attr("src", newImg);
	if($(element).attr("class") != "menuless" && $(element).parents("div").attr("id") == "header") {
		$(element).addClass("hover");
	}
}
function rollOut(element) {
	var oldImg = $(element).find("img").attr("src");
	var newImg = oldImg.replace(/_over/, "_off");
	$(element).find("img").attr("src", newImg);
	if($(element).attr("class") != "menuless" && $(element).parents("div").attr("id") == "header") {
		$(element).removeClass();
	}
}
function navHover() {
	$("#header ul li").hover(function() {
		rollOver(this);
		$(this).find("dl").show();
	}, function() {
		rollOut(this);
		$(this).find("dl").hide();
	});
	$("#footer ul li").hover(function() {
		rollOver(this);
	}, function() {
		rollOut(this);
	});
}

//Form Text Input Focus and Blur Effects
function formFocusBlur(element, inputVal) {
	$(element).focus(function() {
		if(this.value == inputVal) {
			this.value = "";
		}	
	});
	$(element).blur(function() {
		if(this.value == "") {
			this.value = inputVal;
		}
		if(inputVal == "Search Here") {
			setTimeout(function() {$("#store_search form ul").hide()}, 150);
		}
	});
}

//Testimonial and Teacher Tips Quote Rotator
var currentQuote = 0;
function quoteRotator() {
	$("#quotes li:eq(0)").show();
	$("#quotes_paging a.prev").click( function() {
		$("#quotes p span").hide();
		$("#quotes li:eq(" + currentQuote + ")").hide();
		if (currentQuote == 0) {
			currentQuote = numQuotes - 1;
		} else {
			currentQuote -= 1;
		}
		$("#quotes li:eq(" + currentQuote + ")").show();
		return false;
	});

	$("#quotes_paging a.next").click( function() {
		$("#quotes p span").hide();
		$("#quotes li:eq(" + currentQuote + ")").hide();
		if (currentQuote == numQuotes - 1) {
			currentQuote = 0;
		} else {
			currentQuote += 1;
		}
		$("#quotes li:eq(" + currentQuote + ")").show();
		return false;
	});	
}
function quoteExpand() {
	$("#quotes blockquote a").toggle(function() {
		$(this).html("Read less");
		$("#quotes p span").show();
		return false;
	}, function() {
		$(this).html("Read more");
		$("#quotes p span").hide();
		return false;
	});
}

//HOMEPAGE
//Main Slideshow
var newNum = 0;
var totalNum = 0;
var rotate;
function slideShowOut(num) {
	$("#home_slideshow ul li").not("li:eq("+num+")").hide();
}
function slideShowSwitch(element) {
	newNum = $("#nums a").index(element);
	$("#home_slideshow ul li").css("z-index", "0");
	$("#home_slideshow ul li").eq(newNum).css("z-index", "1");
	$("#home_slideshow ul li").eq(newNum).fadeIn(1000);
	var out = setTimeout("slideShowOut(\""+newNum+"\")", 1000);
	$("#nums a").not("a.pause").removeClass();
	$("#nums a").eq(newNum).addClass("on");
	slideShowAnimate();
}
function slideShowRotate() {
	if(newNum < totalNum) {
		newNum++;
		$("#home_slideshow ul li").css("z-index", "auto");
	} else {
		newNum = 0;
		$("#home_slideshow ul li").eq(newNum).css("z-index", "900");
		$("#home_slideshow ul li").not("li:eq("+newNum+")").css("z-index", "899");
	}
	slideShowSwitch($("#nums a").eq(newNum));
}
function slideShowAnimate() {
	rotate = setTimeout("slideShowRotate()", 5000);
}
function slideShow() {
	totalNum = $("#home_slideshow ul li").length - 1;
	slideShowAnimate();
	$("#nums a").not("#nums a.pause").click(function (){
		clearTimeout(rotate);
		slideShowSwitch(this);
		return false;
	});
	$("#nums a.pause").toggle(function() {
		clearTimeout(rotate);
		var newBtn = $(this).find("img").attr("src").replace(/_pause/, "_play");
		$(this).find("img").attr("src", newBtn);
		return false;
	}, function() {
		slideShowRotate();
		var newBtn = $(this).find("img").attr("src").replace(/_play/, "_pause");
		$(this).find("img").attr("src", newBtn);
		return false;
	});
	$("#controls a.replay").click(function() {
		clearTimeout(rotate);
		$("#home_slideshow").prepend("<div id=\"home_intro\"></div>");
		loadIntro();
		return false;
	});
}
function loadSlideshow() {
	$("#home_slideshow").show();
	$("#home_slideshow ul li").eq(0).show();
}
function loadIntro() {
	//Embeds homepage intro video
	var flashvars = { currentVideo: "/flv/SPI-home-slideshow.flv" };
	var params = { wmode: "transparent" };
	var attributes = {};
	swfobject.embedSWF("/swf/intro.swf", "home_intro", "630", "383", "9.0.0", "/swf/expressInstall.swf", flashvars, params, attributes, loadSlideshow);
}
//This function is called in the AS at the end of the intro video
function showSlideshow() {
	//starts slideshow animation, removes swf, and re-adds the container for intro swf
	slideShow();
	swfobject.removeSWF("home_intro");
	$("#home_banner").prepend("<div id=\"home_intro\"></div>");
}
//For users with cookie set, skip intro and start slideshow.
function skipIntro() {
	$("#home_intro").remove();
	loadSlideshow();
	slideShow();
}
//END HOMEPAGE

//ABOUT US PAGE
//Secondary navigation
function subnavClick() {
	$("#sub_nav li a").click(function() {
		//Change on state
		$("#sub_nav li a").removeClass("on");
		$(this).addClass("on");
		
		//Show the right content
		var newDiv = $(this).attr("href").replace(/#/, "");
		$("div.who_we_are").hide();
		$("#" + newDiv).show();
		
		return false;
	});
}

function showCategory(catNum) {
	// catNum 0 - Our Team, catNum 1- SPI Fellows, catNum 2 - Collaborators, catNum 3 - Past Fellows
	$("#sub_nav li a:not(" + catNum + ")").removeClass("on");
	$("#sub_nav li a:eq(" + catNum + ")").addClass("on");
	$("div.who_we_are:not(" + catNum + ")").hide();
	$("div.who_we_are:eq(" + catNum + ")").show();
}
//END ABOUT US

//ACCORDION STYLE EFFECTS
function doAccordion(element, reverse, flipVal) {
	if(reverse == true) {
		if(flipVal % 2 == 1) {
			closeAccordion(element);
		} else if(flipVal % 2 == 0) {
			openAccordion(element);
		}
	} else {
		if(flipVal % 2 == 1) {
			openAccordion(element);
		} else if(flipVal % 2 == 0) {
			closeAccordion(element);
		}
	}
}
function closeAccordion(element) {
	$(element).removeClass("on");
	$(element).next().slideUp();
}
function openAccordion(element) {
	$(element).addClass("on");
	$(element).next().slideDown();
}
function setAccordion() {
	var flip0 = 0;
	var flip1 = 0;
	var flip2 = 0;
	var flip3 = 0;
	var flip4 = 0;
	var flip5 = 0;
	var flip6 = 0;
	$("#toggler dl").find("dt.on").next().slideDown();
	$("#toggler dl dt").click(function() {
		var index = $("#toggler dl dt").index(this);
		switch(index) {
			case 0:
				flip0++;
				doAccordion(this, true, flip0);
				break;
			case 1:
				flip1++;
				doAccordion(this, false, flip1);
				break;
			case 2:
				flip2++;
				doAccordion(this, false, flip2);
				break;
			case 3:
				flip3++;
				doAccordion(this, false, flip3);
				break;
			case 4:
				flip4++;
				doAccordion(this, false, flip4);
				break;
			case 5:
				flip5++;
				doAccordion(this, false, flip5);
				break;
			case 6:
				flip6++;
				doAccordion(this, false, flip6);
				break;
			default:
				break;
		}
		return false;
	});
}
//END ACCORDION

//VIDEO PLAYER POPUPS
function closePopup() {
	$('.ui-dialog-overlay').click(function() {
		$("#dialog").dialog('close');
	});
}
function videoPopup(vTitle) {
	$("#dialog").dialog({
		modal: true,
		width: 720,
		height: 484,
		resizable: false,
		open: function() {
			$("#header ul li").css("z-index", "0");
		},
		close: function() {
			$("#dialog").dialog( 'destroy' );
			$("#header ul li").css("z-index", "1000");
		},
		title: vTitle
	});
	closePopup();
}
function loadVideo(swf) {
	var flashvars = { currentVideo: swf };
	var params = { };
	var attributes = { };
	swfobject.embedSWF("/swf/videoPlayer.swf", "video_player", "720", "444", "9.0.0","/swf/expressInstall.swf", flashvars, params, attributes);
}
function openVideo(group) {
	$("#" + group + " ul a").not("#" + group + " ul p a").click(function() {
		var swf = $(this).attr("href");
		var vTitle = $(this).attr("title");
		loadVideo(swf);
		videoPopup(vTitle);
		return false;
	});
}
//END VIDEO PLAYER

$(document).ready(function() {
	//Functions called on all pages
	addThis();
	navHover();
	formFocusBlur("#search_form input.input", "Search");
	formFocusBlur("#crumb_share input.input", "Sign up for newsletter");
	formFocusBlur("#signup input.input", "Email Address");
	quoteRotator();
	quoteExpand();
	if($("#home_slideshow").length) {
		//loadIntro();
		//skipIntro();
	//	openVideo("home_videos");
	}
	if($("#sub_nav").length) {
		subnavClick();
	}
	if($("#toggler").length) {
		setAccordion();
	}
	if($("#videos").length) {
		openVideo("videos");
	}
	//Load slideshow on press pages
	if($("#press_banner").length) {
		loadSlideshow();
		slideShow();
	}
});