//DOM Core
function getTopOffset(object) {
	var total = object.offsetTop;
	var parent = object.offsetParent;
	while (parent != null) {
		total += parent.offsetTop;
		parent = parent.offsetParent;
	}
	return (total);
}
function getLeftOffset(object) {
	var total = object.offsetLeft;
	var parent = object.offsetParent;
	while (parent != null) {
		total += parent.offsetLeft;
		parent = parent.offsetParent;
	}
	return (total);
}

//Port PHP functions
function strrchr(haystack, needle) {
	if (typeof needle !== "string") {
		needle = String.fromCharCode(parseInt(needle, 10));
	}
	needle = needle.charAt(0);
	pos = haystack.lastIndexOf(needle);
	if (pos === -1) {
		return false;
	} 
	return haystack.substr(pos);
}

//Events
function ignoreEnter(e) {
	e = (e ? e : window.event);
	var k = (e.keyCode ? e.keyCode : (e.which ? e.which : null));
	if (k == 13) {
		if (e.preventDefault) {
			e.preventDefault();
			return false;
		} else {
			return true;
		}
	}
}

//Preload images
var preload_images = new Array("/images/home_window.png","/images/nav_recipes_active.png","/images/nav_products_active.png","/images/nav_cooking_active.png","/images/nav_believe_active.png","/images/nav_community_active.png","/images/nav_story_active.png","/images/bottom_bar_btn_1_on.png","/images/bottom_bar_btn_2_on.png","/images/bottom_bar_btn_3_on.png");
for (i in preload_images) {
	var img = new Image();
	img.src = preload_images[i];
}

//Other Perdue Sites
var otherSitesExpanded = false;

function toggleOtherSites(object) {
	if (otherSitesExpanded) {
		hideOtherSites();
	} else {
		var target = $("#other_sites");
		target.css("top",(getTopOffset(object) + 22) + "px");
		target.css("left",(getLeftOffset(object) - 540) + "px");
		//target.css("border","1px solid #CCCCCC");
		target.animate({height:"350px"},500);
		otherSitesExpanded = true;
	}
}
function hideOtherSites() {
	//$("#other_sites").css("border","none");
	$("#other_sites").animate({height:"0px"},500);
	otherSitesExpanded = false;
}

//Sliders
function hideSliderHints() {
	if (document.all) {
		$(".slider_hint").remove();
	} else {
		$(".slider_hint").fadeOut(500,function(){
			$(".slider_hint").remove();
		});
	}
}

//Clocks
function clockSelected(object,id) {
	var checkbox = document.getElementById("clock_"+id);
	if (checkbox.checked) {
		//Make clock inactive
		object.src = "/images/clock_inactive.png";
		checkbox.checked = false;
	} else {
		//Make clock active
		object.src = "/images/clock_active.png";
		checkbox.checked = true;
	}
}

//Social Feeds
function switchFeed(object,to) {
	if (isIE7) {
		width = -381;
	} else if (isIE8) {
		width = -385;
	} else {
		width = -380;
	}
	var newLeft = (to - 1) * width + "px";
	$("#connect_with_us_content").animate({left:newLeft},500);
	
	var controls = object.parentNode.getElementsByTagName("img");
	controls[0].src = "/images/icon_fb_large" + (to != 1 ? "_faded" : "") + ".png";
	controls[1].src = "/images/icon_tw_large" + (to != 2 ? "_faded" : "") + ".png";
	controls[2].src = "/images/icon_yt_large" + (to != 3 ? "_faded" : "") + ".png";
}

//Site Search
function toggleSearch(object) {
	var arrow = object.getElementsByTagName("img")[0];
	var results = object.parentNode.parentNode.getElementsByTagName("div")[1];
	
	if (strrchr(arrow.getAttribute("src"),"/") == "/arrow_side.png") {
		arrow.setAttribute("src","/images/arrow_down.png");
		$(results).animate({height:"150px"},500);
		
		//Re-initialize scrollers for IE 7 here since it SUCKS and can't seem to do it the first time
		if (isIE7) {
			initializeRecipeScroller();
			initializeProductScroller();
			//initializeCMSScroller();
		}
	} else {
		arrow.setAttribute("src","/images/arrow_side.png");
		$(results).animate({height:"0px"},500);
	}
}

//Custom scrollbars
function initializeRecipeScroller() {
	var recipesHeight = $("#recipe_results").innerHeight();
	if (recipesHeight > visibleHeight) {
		$("#search_recipe_scroller").slider({animate:true,min:0,max:100,value:100,orientation:"vertical",
		slide:function(event, ui){
			var newTop = "-" + ((100 - ui.value) / 100 * (recipesHeight - visibleHeight)) + "px";
			$("#recipe_results").css("top",newTop);
		}});
	}
}
function initializeProductScroller() {
	var productsHeight = $("#product_results").innerHeight();
	if (productsHeight > visibleHeight) {
		$("#search_product_scroller").slider({animate:true,min:0,max:100,value:100,orientation:"vertical",
		slide:function(event, ui){
			var newTop = "-" + ((100 - ui.value) / 100 * (productsHeight - visibleHeight)) + "px";
			$("#product_results").css("top",newTop);
		}});
	}
}
function initializeCMSScroller() {
	var cmsHeight = $("#cms_results").innerHeight();
	if (cmsHeight > visibleHeight) {
		$("#search_cms_scroller").slider({animate:true,min:0,max:100,value:100,orientation:"vertical",
		slide:function(event, ui){
			var newTop = "-" + ((100 - ui.value) / 100 * (cmsHeight - visibleHeight)) + "px";
			$("#cms_results").css("top",newTop);
		}});
	}
}
function initializeNewsScroller() {
	var newHeight = $("#news_results").innerHeight();
	if (newHeight > visibleHeight) {
		$("#news_scroller").slider({animate:true,min:0,max:100,value:100,orientation:"vertical",
		slide:function(event, ui){
			var newTop = "-" + ((100 - ui.value) / 100 * (newHeight - visibleHeight)) + "px";
			$("#news_results").css("top",newTop);
		}});
	}
}
function initializeStatementScroller() {
	var statHeight = $("#stat_results").innerHeight();
	if (statHeight > visibleHeight) {
		$("#stat_scroller").slider({animate:true,min:0,max:100,value:100,orientation:"vertical",
		slide:function(event, ui){
			var newTop = "-" + ((100 - ui.value) / 100 * (statHeight - visibleHeight)) + "px";
			$("#stat_results").css("top",newTop);
		}});
	}
}
function initializeThreegenScroller() {
	var newHeight = $("#threegen_results").innerHeight();
	if (newHeight > visibleHeight) {
		$("#threegen_scroller").slider({animate:true,min:0,max:100,value:100,orientation:"vertical",
		slide:function(event, ui){
			var newTop = "-" + ((100 - ui.value) / 100 * (newHeight - visibleHeight)) + "px";
			$("#threegen_results").css("top",newTop);
		}});
	}
}
function initializeThreegentwoScroller() {
	var newHeight = $("#threegen_results2").innerHeight();
	if (newHeight > visibleHeight) {
		$("#threegen_scroller2").slider({animate:true,min:0,max:100,value:100,orientation:"vertical",
		slide:function(event, ui){
			var newTop = "-" + ((100 - ui.value) / 100 * (newHeight - visibleHeight)) + "px";
			$("#threegen_results2").css("top",newTop);
		}});
	}
}
