///////////////////////////// Window Onload///////////////////////////////

/*-------------------------- Auto Button ----------------------------*/
// Auto Button Rollovers for images with ".autobtn" class
var imgText;
$(".autobtn").each(function() {
	imgText = $(this).html();
	$(this).html("");
	$(this).append("<span class=\"btn-left\"></span>");
	$(this).append("<span class=\"btn-mid bold white large\">" + imgText + "</span>");
	$(this).append("<span class=\"btn-right\"></span>");
});

$(".autobtn").mouseover(function() {
	$(this).children(".btn-left").css("background-image","url(images/btn_leftcap_hover.png)");
	$(this).children(".btn-mid").css("background-image","url(images/btn_bg_hover.png)");
	$(this).children(".btn-right").css("background-image","url(images/btn_rightcap_hover.png)");
});

$(".autobtn").mouseout(function() {
	$(this).children(".btn-left").css("background-image","url(images/btn_leftcap.png)");
	$(this).children(".btn-mid").css("background-image","url(images/btn_bg.png)");
	$(this).children(".btn-right").css("background-image","url(images/btn_rightcap.png)");
});

$(".autobtn-wide").each(function() {
	imgText = $(this).html();
	$(this).html("");
	$(this).append("<span class=\"btn-left\"></span>");
	$(this).append("<span class=\"btn-mid bold white large\">" + imgText + "</span>");
	$(this).append("<span class=\"btn-right\"></span>");
});

$(".autobtn-wide").mouseover(function() {
	$(this).children(".btn-left").css("background-image","url(images/btn_leftcap_wide_hover.png)");
	$(this).children(".btn-mid").css("background-image","url(images/btn_bg_hover.png)");
	$(this).children(".btn-right").css("background-image","url(images/btn_rightcap_wide_hover.png)");
});

$(".autobtn-wide").mouseout(function() {
	$(this).children(".btn-left").css("background-image","url(images/btn_leftcap_wide.png)");
	$(this).children(".btn-mid").css("background-image","url(images/btn_bg.png)");
	$(this).children(".btn-right").css("background-image","url(images/btn_rightcap_wide.png)");
});

$(".autobtn-thin").each(function() {
	imgText = $(this).html();
	$(this).html("");
	$(this).append("<span class=\"btn-left\"></span>");
	$(this).append("<span class=\"btn-mid bold white large\">" + imgText + "</span>");
	$(this).append("<span class=\"btn-right\"></span>");
});

$(".autobtn-thin").mouseover(function() {
	$(this).children(".btn-left").css("background-image","url(images/btn_leftcap_thin_hover.png)");
	$(this).children(".btn-mid").css("background-image","url(images/btn_bg_thin_hover.png)");
	$(this).children(".btn-right").css("background-image","url(images/btn_rightcap_thin_hover.png)");
});

$(".autobtn-thin").mouseout(function() {
	$(this).children(".btn-left").css("background-image","url(images/btn_leftcap_thin.png)");
	$(this).children(".btn-mid").css("background-image","url(images/btn_bg_thin.png)");
	$(this).children(".btn-right").css("background-image","url(images/btn_rightcap_thin.png)");
});

$.preloadImages("images/btn_bg_hover.png","images/btn_leftcap_hover.png", "images/btn_leftcap_wide_hover.png", "images/btn_rightcap_hover.png", "images/btn_rightcap_wide_hover.png", "images/btn_leftcap_thin_hover.png", "images/btn_bg_thin_hover.png", "images/btn_rightcap_thin_hover.png");
// end Auto Button code


/*-------------------------- Activate Flippys ----------------------------*/

$("#secondary .flippy").not(".mini_lineup").setFlippy(155);
$(".mini_lineup").setFlippy(195);
$(".tax-tools-flippy").setFlippy(255);
$("#secondary .home_page").setFlippy(213);
$("#secondary .mini_lineup").setFlippy(150);
$("#primary .advantages").setFlippy(160);
$("#primary .reviews").setFlippy(268);

/*-------------------------- Activate iTab -----------------------------*/

$(".itab > li > a").bind("click", function() {
	var target = $(this).parent();
	$(".itab li").addClass("itabby");
	if (target.attr("class").indexOf("active") == -1) {

		var itab_offset = $(".itabby").offset({ border: true, padding: true });
		if ($(window).scrollTop() > itab_offset.top) {
			$(window).scrollTop(itab_offset.top - 10);
		}
		
		$(".itab .active").removeClass("active");
		target.addClass("active");
		var thisUrl = $(this).attr("href");
		$(".itabscroll").slideTo(thisUrl);
		return false;		
	}
	return false;
});


$(".itab_options a.itab_prev").hover(function() {
	$(this).css("background", "url(images/itab_arr_left_on.png)").css("cursor","pointer");
}, function() {
	$(this).css("background", "url(images/itab_arr_left.png)").css("cursor","default");
});
$(".itab_options a.itab_next").hover(function() {
	$(this).css("background", "url(images/itab_arr_right_on.png)").css("cursor","pointer");
}, function() {
	$(this).css("background", "url(images/itab_arr_right.png)").css("cursor","default");
});

$(".itab_prev").bind("click", function() {
	var target = $(".itab .active").prev("li").children("a");
	if (target.attr("href"))	target.click();
	else	$(".itab li:last").children("a").click();
});
$(".itab_next").bind("click", function() {
	var target = $(".itab .active").next("li").children("a");
	if (target.attr("href"))	target.click();
	else	$(".itab li:first").children("a").click();
});


/*-------------------------- Toggly Functionality ----------------------------*/
$(".toggly dd").hide();
$(".toggly dd").after("<br />");
$(".toggly dt").toggle(function() {
	var toggle_class = $(".toggly").attr("class");
	if (toggle_class.indexOf("single")>0) {
		if ($(this).parent("dl").parent("div")) {
			$(this).parent("dl").parent("div").find("dt.on").click();
		} else {
			$(".toggly dt.on").click();
		}
	}
	$(this).next("dd").slideDown("fast");
	$(this).addClass("on");
}, function() {
	$(this).next("dd").slideUp("fast");
	$(this).removeClass("on");
});

$(".toggly_expand").click(function() {
	$(".toggly dt").each(function() {
		if ($(this).attr("class") != "on") {
			$(this).click();
		}
	});
});

$(".toggly_collapse").click(function() {
	$(".toggly dt").each(function() {
		if ($(this).attr("class") == "on") {
			$(this).click();
		}
	});
});

/*--------------------- old button rollover functionality ---------------------*/
$(".rollover").mouseover(function(){
	var imgUrl = $(this).attr("src");
	var imgUrlString = /\.jpg|\.jpeg|\.png|\.gif/g;
	var imgUrlType = imgUrl.match(imgUrlString);
	var imgFileParts = imgUrl.split(imgUrlType);
	$(this).attr("src",imgFileParts[0] + "_hover" + imgUrlType);
});

$(".rollover").mouseout(function(){
	var imgUrl = $(this).attr("src");
	if (imgUrl.indexOf("_hover")>0) {
		var imgFileParts = imgUrl.split("_hover");
		$(this).attr("src",imgFileParts[0] + imgFileParts[1]);
	}
});	

/*-------------------------- Image preloads ----------------------------*/
$.preloadImages("images/btn_search_go_hover.gif", "images/itab_arr_left_on.png", "images/itab_arr_right_on.png");

/*-------------------------- Reset Slidey  ----------------------------*/

setInterval("resetSlidey()",3000);

/*------------------------------ PNG Fix  ------------------------------*/

$("a[href*=.png]").css("behavior", "url(/js/iepngfix.htc)");
$("img[src*=.png]").css("behavior", "url(/js/iepngfix.htc)");

$("div,p,a,span").each(function() {
	if ($(this).css("background-image")) {
		var bgimg = $(this).css("background-image");
		if (bgimg.indexOf(".png")>0) {
			$(this).css("behavior", "url(/js/iepngfix.htc)");
		}
	} else if ($(this).css("background")){
		var bgimg = $(this).css("background");
		if (bgimg.indexOf(".png")>0) {
			$(this).css("behavior", "url(/js/iepngfix.htc)");
		}
	}
});

/*------------------------------ Poppy Box  ------------------------------*/
$(".poppy, .thickbox").popIt();

/*------------------------- Anchor AutoLaunch  -------------------------*/

var thisUrl = location.href;
var thisUrlAnchor = thisUrl.split("#go_");
if (thisUrlAnchor[1]) {
	window.scrollTo(0,0);
	$("a[href*=#"+thisUrlAnchor[1]+"]").click();
}	
var thisUrlAnchor = thisUrl.split("#pop_");
if (thisUrlAnchor[1]) {
	$("a[class*="+thisUrlAnchor[1]+"]").click();
}	

/*---------------------------- Shadow Pop ----------------------------*/

$.each( $("div.pop .productbox, #sign_in_box"), function(i, n){
	if (!$(n).children("div.shadow").html()) {
		$(n).append('<div class="shadow"><table cellspacing=0 cellpadding=0><tr><td><img src="images/shadow_tl.png" class="tl png" border=0 width=17 height=17></td><td><img src="images/shadow_tc.png" class="tc png" border=0 width=17 height=17></td><td><img src="images/shadow_tr.png" class="tr png" border=0 width=17 height=17></td></tr><tr><td><img src="images/shadow_ml.png" class="ml png" border=0 width=17 height=17></td><td style="background-color:#FFF"></td><td><img src="images/shadow_mr.png" class="mr png" border=0 width=17 height=17></td></tr><tr><td><img src="images/shadow_bl.png" class="bl png" border=0 width=17 height=17></td><td><img src="images/shadow_bc.png" class="bc png" border=0 width=17 height=17></td><td><img src="images/shadow_br.png" class="br png" border=0 width=17 height=17></td></tr></table></div>');
	}
});



/* ------------------------------ Append cid ------------------------------*/
if (typeof window.getURLParam == 'function') {cidURLz = getURLParam("cid");}


	$(function(){
	$(".appendcid").each(function(){
		var storecidURL = $(this).attr("href");
		referrercid = "int_tt_";
		var pathName = window.location.pathname;
		var lastSlashIndex = pathName.lastIndexOf('/');
		var lastDotIndex = pathName.lastIndexOf('.');
  		var fileName = (pathName.substring((lastSlashIndex + 1), lastDotIndex) + '_');
	
		if (cidURLz) {			
			currentcid = cidURLz;
		}
		else if (cidCookie) {
			currentcid = cidCookie;
		}
		else {
				currentcid ="null";						
		}
		var hrefStore = $(this).attr('href');
		if (hrefStore.match("=") == "=") {
			newstorecidURL = storecidURL + "&cid=" + referrercid + fileName + currentcid;	
		}
		else {
			newstorecidURL = storecidURL + "?cid=" + referrercid + fileName + currentcid;	
		}		
		$(this).attr("href",newstorecidURL);		
	});
});

/* Bazaarvoice Update Script */
$(function(){	
	$('.bzInner').each(function(){
		bzValItem = $(this).children('.bzValue').html();
		$(this).css('width',bzCalc(bzValItem));
	});	
});

function bzCalc (bzVal){
	bzWhole = Math.floor(bzVal);
	bzDecimal = bzVal % 1;
	bzVal = 1 + (bzWhole*15) + Math.round(13*bzDecimal);
	return bzVal;
}

/* ------------------------------ enable auto popup thickboxes ------------------------------*/
$(document).ready(function () {
	var linkClass = location.href.split('#')[1];
	// check if this link exists and doesn't point to multiple links
	$('.'+linkClass).each(function (i) {
		if (i==0) {
			// link must also have a poppy or thickbox class
			if ($(this).hasClass('poppy') || $(this).hasClass('thickbox')) {
				$(this).click();
			}
		}	
	});
});
