featperpage = 4;
flipperpage = 4;

$(document).ready(function(){
	
	$(".issub").removeClass("issub");
	$("#menu li ul").parent().addClass("submenu").hover(function(){
		$(this).children("ul").show();
	},function(){
		$(this).children("ul").hide();
	});
	
	$("#searchbox").focus(function(){
		if ($(this).attr("value") == "Search...") {
			$(this).attr("value","");
		}
	});
	$("#searchbox").blur(function(){
		if ($(this).attr("value") == "") {
			$(this).attr("value","Search...");
		}
	});
	
	if ($("body").attr("id") == "homepage") {
		featsetup();
		
		$("#featprev").click(function(){
			prodscroll("prev");
			$(this).blur();
			return false;
		});
		$("#featnext").click(function(){
			prodscroll("next");
			$(this).blur();
			return false;
		});
		
		flipsetup();
		
		$(".flipbox_entry").click(function(){
			if (!($(this).hasClass("selected"))) {
				homeflip(this);
				return false;
			}
		});
		
		$("#flipbox_arrow_left").click(function(){
			homeflippage("prev");
			$(this).blur();
			return false;
		});
		$("#flipbox_arrow_right").click(function(){
			homeflippage("next");
			$(this).blur();
			return false;
		});
	}
	
	$(".opthighlight").hover(function(){
		showopthover(this);
	}, function(){
		hideopthover(this);
	}).click(function(){
		showoptdescrip(this);
	}).animate({"opacity" : 0}, 2000);
	
	$(".featurelist li").click(function(){
		$(".featurelist li.selected").removeAttr("class");
		$(this).addClass("selected");
		showoptdescrip(this);
		$(this).blur();
		$(this).children("a").blur();
		return false;
	});/*.hover(function(){
		showopthover(this);
	}, function(){
		hideopthover(this);
	})*/
	
	/*$("#optionbox").hover(function(){
		$(this).children(".opthighlight").animate({"opacity" : .5}, 300);
	}, function(){
		$(this).children(".opthighlight").animate({"opacity" : 0}, 2000);
	});*/
});

function showopthover(elem) {
	var thisbox = $(elem).attr("id");
	thisbox = thisbox.replace("a_","");
	$("#" + thisbox).animate({"opacity" : 1}, 200);
	var boxtitle = $("#a_" + thisbox).children("a").html();
	$("#optionbox").append("<div id=\"optlabelbox\" style=\"position:absolute;bottom:0;width:100%;text-align:center;padding:3px 0;font-size:.7em;background:#fff;opacity:.5;\">" + boxtitle + "</div>");
}
function hideopthover(elem) {
	var thisbox = $(elem).attr("id");
	var opacityback = (thisbox.indexOf("a_") >= 0) ? 0 : .5;
	thisbox = thisbox.replace("a_","");
	$("#" + thisbox).animate({"opacity" : opacityback}, 200);
	$("#optlabelbox").remove();
}
function showoptdescrip(elem) {
	//$("body").append("<div id=\"optdescripfade\" style=\"\"></div><div id=\"optdescrippos\" class=\"toggledescrip\" style=\"position:fixed;z-index:1999;top:0;left:0;width:100%;height:100%;\"><div id=\"optdescrippop\" style=\"\"><div id=\"optdescriptpad\"></div><a href=\"\" class=\"info_nav active\" id=\"info_prev\"><span>Previous</span></a><a href=\"\" class=\"info_nav active\" id=\"info_next\"><span>Next</span></a><div id=\"optdescripcontrols\"><a href=\"\" class=\"toggledescrip\">X</a></div></div></div></div>");
	
	if ($("#optdescrippos").length == 0) {
		$("#pagecontent").append("<div id=\"optdescripfade\" class=\"toggledescrip\"></div>");
		$("#pagecontent").append("<div id=\"optdescrippos\"><div id=\"optdescrippop\" style=\"\"><div id=\"optdescriptpad\"><div class=\"clearfloat\"></div></div></div></div>");
		$("#optdescrippop").append("<div id=\"optdescripcontrols\"><a href=\"\" class=\"toggledescrip\">X</a></div>");
		checkoptoffset();
		var contheight = parseInt($("#optdescrippop").height());
		var windowheight = parseInt($("#optdescrippos").height());
		var boxheight = contheight + 40;
		$("#optdescripfade").animate({"opacity" : .5});
		//var topoffset = (windowheight/2)-(boxheight/2);
		//$("#optdescrippop").css({"top" : topoffset});
		
		$(".toggledescrip").click(function(){
			hideoptdescrip();
			return false;
		});
		$("#optdescrippad").click(function(){
			return false;
		});
		$(".info_nav").click(function(){
			var direc = this.id;
			var curshown = $("#optdescrippop").attr("class");
			if (direc == "info_next") {
				var getit = $(curshown).next().attr("id");
				if (("#" + getit) == curshown) {
					getit = $(curshown).next().next().attr("id");
				}
			} else {
				var getit = $(curshown).prev().attr("id");
				if (("#" + getit) == curshown) {
					getit = $(curshown).prev().prev().attr("id");
				}
			}
			
			if ($("#" + getit).length > 0) {
				getpopprod($("#" + getit));
			}
			$(this).blur();
			return false;
		});
		$(window).bind("scroll",function(){
			checkoptoffset(0);
		});
	}
	getpopprod(elem);
}
function getpopprod(elem) {
	$("#optdescripfloater").remove();
	$("#optdescrippop").removeAttr("class");
	$("#infobox_loading").remove();
	$("#optdescriptpad").append("<div id=\"infobox_loading\"></div>");
	var thisbox = $(elem).attr("id");
	var thisbox = (thisbox.indexOf("a_") >= 0) ? "#" + thisbox : "#a_" + thisbox;
	
	/*var lookforward = $(thisbox).next().attr("id");
	var lookback = $(thisbox).prev().attr("id");
	if (!$("#" + lookforward).length > 0) {
		$("#info_next").removeClass("active");
	} else {
		$("#info_next").addClass("active");
	}
	if (!$("#" + lookback).length > 0) {
		$("#info_prev").removeClass("active");
	} else {
		$("#info_prev").addClass("active");
	}*/
	
	$("#optdescrippop").addClass(thisbox);
	var boxtitle = $(thisbox).children("a").html();
	var boxdescrip = $(thisbox).children(".data").html();
	var boxrel = $(thisbox).children("a").attr("rel");
	var boximg = (boxrel != "") ? boxrel : "/media/productinfo/default.jpg";
	var boxtype = ($(thisbox).parent().attr("id") == "standardlist") ? "Standard Feature" : "Optional Accessory";
	
	$("#optdescriptpad").prepend("<div id=\"optdescripfloater\"><h2><span id=\"optdescriptype\">" + boxtype + "</span>" + boxtitle + "</h2><img src=\"" + boximg + "\" id=\"optdescriptimg\" /><div class=\"info\"><p>" + boxdescrip + "</p></div><div class=\"clearfloat\"></div>");
	$("#optdescripfloater").attr("style","position:absolute;left:-1000px;width");
	$("#optdescriptimg").load(function(){
		if ($("#optdescriptimg").width() > 330) {
			$("#optdescriptimg").removeAttr("height").attr("width","330");
		}
		var imgwidth = parseInt($("#optdescriptimg").width());
		$("#optdescripfloater .info").css({"marginLeft" : imgwidth+20});
		
		var contheight = parseInt($("#optdescripfloater").height());
		var padheight = parseInt($("#optdescriptpad").height());
		var windowheight = parseInt($("#optdescrippos").height());
		var boxheight = contheight + 40;
		var topoffset = (windowheight/2)-(boxheight/2);
		
		
		if (contheight != padheight) {
			$("#optdescriptpad").animate({"height" : contheight});
			//$("#optdescrippop").animate({"top" : topoffset});
		}
		var windowwidth = parseInt($("#optdescrippos").width());
		var newgooffset = (windowwidth/2) - 460;
		checkoptoffset(contheight);
		$("#optdescripfloater").removeAttr("style").hide().fadeIn();
		$("#infobox_loading").fadeOut();
	});
}
function hideoptdescrip() {
	$("#optdescripfade").remove();
	$("#optdescrippos").remove();
	$("#optdescrippop").remove();
	$(".featurelist li.selected").removeAttr("class");
	$(window).unbind('scroll');
}
function checkoptoffset(finalheight) {
	var windowscroll = $(window).scrollTop();
	var pagecontpos = $("#pagecontent").offset().top;
	if (windowscroll > pagecontpos) {
		
		var gotoscroll = windowscroll-pagecontpos+105;
		var optheight = (finalheight > 0) ? finalheight+35 : $("#optdescrippos").height();
		var pageheight = $("#pagecontent").height();
		if (optheight+gotoscroll > pageheight) {
			gotoscroll = gotoscroll-((gotoscroll+optheight)-(pageheight+10));
		}
		$("#optdescrippos").css({"top" : gotoscroll });
		
	} else {
		$("#optdescrippos").css({"top" : 105 });
	}
}




function featsetup() {
	var featnum = $("#scroll_prodlist .product").length;
	var featheight = featperpage*($("#scroll_prodlist .product").height() + 1);
	var pageheight = $("#pagecontent").height()-50;
	var listtoheight = (featheight > pageheight) ? featheight : pageheight;
	//alert(featheight + "\n" + pageheight);
	
	var pages = Math.floor(featnum/featperpage);
	if (featnum%featperpage > 0) { pages++; }
	if (pages > 1) {
		$("#featpagination").html("<a href=\"\" id=\"featprev\">&laquo;</a><span> page <span id=\"curpage\">1</span> of " + pages + " </span><a href=\"\" id=\"featnext\">&raquo;</a>");
	} else {
		$("#featpagination").html("<span style=\"padding-right:10px;\"> page <span id=\"curpage\">1</span> of " + pages + " </span>");
	}
	$("#scroll_prodlist").attr("rel",pages-1);
	$("#scroll_prodlist").prepend("<div id=\"scroll_display\"></div>");
	$("#scroll_display").css({"width" : 270*pages}).attr("rel",0);
	for (x=0;x<pages;x++) {
		$("#scroll_display").append("<div class=\"scroller_page\" id=\"scrollpage" + x + "\"></div>");
		for (y=0;y<featperpage;y++) {
			$("#scroll_data .product:eq(0)").appendTo(".scroller_page#scrollpage" + x);
		}
	}
	if (pageheight > featheight) {
		var eachheight = Math.ceil(pageheight/4);
		$(".scroller_page .product").css({"height" : eachheight-1});
		featheight = eachheight*4;
	}
	$(".scroller_page").css({"height" : featheight});
	$("#scroll_prodlist").css({"height" : featheight});
	$("#scroll_data").remove();
	$(".scroller_page:not(#scrollpage0)").hide();
	$(".scroller_page .product").click(function(){
		window.location = $(this).children("a").attr("href");
	});
}

function prodscroll(direc) {
	var starting = parseInt($("#scroll_display").attr("rel"));
	var total = parseInt($("#scroll_prodlist").attr("rel"));
	var animto = (direc == "next") ? starting+1 : starting-1;
	if (animto < 0) { animto = total }
	if (animto > total) { animto = 0 }
	$(".scroller_page:visible").fadeOut(200);
	$(".scroller_page:eq(" + animto + ")").fadeIn(700);
	$("#scroll_display").attr("rel",animto);
	$("#curpage").html(animto+1);
}

function flipsetup() {
	var flipnum = $("#flipbox_gal .flipbox_entry").length;
	var pages = Math.floor(flipnum/flipperpage);
	if (flipnum%flipperpage > 0) { pages++; }
	if (pages > 1) {
		$("#flipbox_home").append("<div id=\"flipbox_arrow_left\" class=\"fliparrow\"></div><div id=\"flipbox_arrow_right\" class=\"fliparrow\"></div>");
		$("#flipbox_arrow_right").css({"left" : (105*flipperpage)+22});
		$("#flipbox_arrow_left").fadeIn(1000);
		$("#flipbox_arrow_right").fadeIn(1000);
	}
	$("#flipbox_gal").attr("rel",pages-1);
	$("#flipbox_gal").append("<div id=\"flip_display\"></div>");
	$("#flip_display").attr("rel",0);
	for (x=0;x<pages;x++) {
		$("#flip_display").append("<div class=\"flipper_page\" id=\"flippage" + x + "\"></div>");
		for (y=0;y<flipperpage;y++) {
			$("#flipbox_data .flipbox_entry:eq(0)").appendTo(".flipper_page#flippage" + x);
		}
	}
	$("#flipbox_data").remove();
	$(".flipper_page:not(#flippage0)").hide();
	$("#flipbox_home").append("<div id=\"flipgal_pages\"><p>Page <span id=\"flipgal_page_count\">1</span> of " + pages + "</p></div>");
}

function homeflip(elem) {
	$("#flipbox_gal .selected").removeClass("selected");
	$(elem).addClass("selected");
	$(elem).children("a").blur();
	var newhtwo = $(elem).children(".data").children("h2").html();
	var newp = $(elem).children(".data").children("p").html();
	$("#flipbox_bar h2").html(newhtwo);
	$("#flipbox_bar p").hide().html(newp).fadeIn(400);
	var getdata = $(elem).children("a").children("img").attr("rel").split("|");
	//var position = getdata[1].split(",");
	var position = [570,-30];
	$("#flipfullimg").remove();
	$("#flipbox_home").append("<img id=\"flipfullimg\" src=\"/imgs/flippers/full/" + getdata[0] + "\" style=\"position:absolute;top:-1000px;left:" + position[0] + "px;z-index:100;\"/><img id=\"loadingimg\" src=\"/_layouts/cleanparts/images/loading.gif\" style=\"position:absolute;top:80px;left:760px;z-index:30;\"/>");

	$("#flipfullimg").load(function(){
		if (!$("#ie_flipbox_home").length > 0) {
			$("#flipfullimg").hide();
			$("#flipfullimg").fadeIn(1000);
		}
		$("#flipfullimg").css({"top" : position[1] + "px"});
		$("#loadingimg").remove();
	});
}

function homeflippage(direc) {
	var starting = parseInt($("#flip_display").attr("rel"));
	var total = parseInt($("#flipbox_gal").attr("rel"));
	var animto = (direc == "next") ? starting+1 : starting-1;
	if (animto < 0) { animto = total }
	if (animto > total) { animto = 0 }
	$(".flipper_page:eq(" + starting + ")").hide();
	$(".flipper_page:eq(" + animto + ")").fadeIn(700);
	$("#flip_display").attr("rel",animto);
	
	var getpage = parseInt($("#flip_display").attr("rel"));
	var getbits = $("#flippage" + getpage).children().length;
	$("#flipbox_arrow_right").animate({"left" : (105*getbits)+22},200);
	$("#flipgal_page_count").html(1+getpage);
}
