jQuery.noConflict();

var b4fUl = "ul#homepage-image";
var b4fUl = "ul#homepage-image";
var slideDuration = 5000;
var fadeDuration = 500;

jQuery(document).ready(function(){
//jQuery(window).load(function(){
//jQuery(document).ready(function(){
	//initialize homepage fading
	/*if(jQuery(b4fUl).length > 0){
		imageTimer = setInterval("showNextImage(b4fUl);", 5000);
		//Hide all images, Set z-indexs to 0 and fade first image in
		jQuery(b4fUl + " li").hide();
		jQuery(b4fUl + " li").css("z-index","0");
		jQuery(b4fUl + " li:eq(0)").fadeTo(500,1).css("z-index","2");
	}*/

	//Orbit Hero Image Sloder
	if(jQuery(b4fUl).length > 0){
		jQuery(b4fUl).orbit({
			animation: 'fade',
			animationSpeed: 500,
			advanceSpeed: 7000,
			degrees: 5,
			startClockOnMouseOut: false,
			startClockOnMouseOutAfter: 500,
			directionalNav: true,
			bullets: true
		});
	}
	

	//convert brands to JS version
	brandsInit();
	
	//show/hide quick links
	linksInit();
	
});

function showNextImage(b4fUl){
	clearTimeout( imageTimer );
	var cTotal = jQuery(b4fUl).children().size();
	jQuery(b4fUl).children().each(function(){
		if(jQuery(this).css("z-index") == "2"){
			var cCurrent = jQuery(this).index();
			if(cCurrent < cTotal - 1){
				cToShow = cCurrent + 1;
			}else{
				cToShow = 0;
			}
		}
	});
	showHideContent(cToShow);
	imageTimer = setInterval("showNextImage(b4fUl);", 5000);
	
	//jQuery("div.paypal-logo").html("\n<br />cTotal: " + cTotal + "\n<br />cToShow: " + cToShow + "\n<br />");
}
function showHideContent(picNum){
	jQuery(b4fUl + " li:eq(" + picNum + ")").hide();
	jQuery(b4fUl).children().each(function(){
		if(jQuery(this).css("z-index") == "2"){
			jQuery(this).css("z-index","1");
			jQuery(this).addClass('removing');
		}else{
			jQuery(this).css("z-index","0");
			jQuery(this).find("div").hide();
		}
	});
	jQuery(b4fUl + " li:eq(" + picNum + ")").css("z-index","2");
	jQuery(b4fUl + " li:eq(" + picNum + ")").fadeTo(500, 1, function() {
		jQuery(this).find("img").css({
			'left' : '0px',
			'top' : '0px',
			'display' : 'block',
			'opacity' : '1'
		});
		jQuery(this).find("div").css({
			'left' : '0px',
			'top' : '0px',
			'display' : 'block',
			'opacity' : '0'
		});
		//jQuery(this).children("div").fadeTo(1000, 1);
		jQuery(this).find("div").animate({
			opacity: "1",
			top: "0px",
			left: "0px"
		}, 1000, function() {
			// Animation complete.
		});
	});
}


function brandsInit(){
	var b4fBrands = "div#homepage-brands";
	var ulTotalWidth = 0;
	jQuery(b4fBrands + " div.ulmask ul").children().each(function(){
		ulTotalWidth = ulTotalWidth + jQuery(this).width() + 12;
		//jQuery("div.paypal-logo").append("<p>this LI: " + jQuery(this).width() + "\n<br />Total: " + ulTotalWidth + "</p>");
	});
	
	jQuery(b4fBrands + " a.next, " + b4fBrands + " a.previous").css({
		'background-position' : '-8px 33px',
		'cursor' : 'pointer'
	});
	jQuery(b4fBrands + " a.previous").css({
		'background-position' : '0px 33px'
	});
	jQuery(b4fBrands + " div.ulmask ul").css({
		'width' : ulTotalWidth + 'px',
		'left' : '0px'
	});
	
	jQuery(b4fBrands + " a.next").click(function(e){
		e.preventDefault();
		var curOffset = parseInt(jQuery(b4fBrands + " div.ulmask ul").css("left"));
		var newOffset = curOffset - 100;
		if(newOffset < 0 - ulTotalWidth + 610){
			newOffset = 0 - ulTotalWidth + 610;
		}
		//jQuery(b4fBrands + " div.ulmask ul").css("left", newOffset + "px");
		jQuery(b4fBrands + " div.ulmask ul").animate({
			left: newOffset
		}, 500, function() {
			  // Animation complete.
		});
		
		//jQuery("div.paypal-logo").html("\n<br />curOffset: " + curOffset + "\n<br />newOffset: " + newOffset + "\n<br />ulTotalWidth: " + (0 - ulTotalWidth + 610));
	});
	jQuery(b4fBrands + " a.previous").click(function(e){
		e.preventDefault();
		var curOffset = parseInt(jQuery(b4fBrands + " div.ulmask ul").css("left"));
		var newOffset = curOffset + 100;
		if(newOffset > 0){
			newOffset = 0;
		}
		//jQuery(b4fBrands + " div.ulmask ul").css("left", newOffset + "px");
		jQuery(b4fBrands + " div.ulmask ul").animate({
			left: newOffset
		}, 500, function() {
			// Animation complete.
		});
		
		//jQuery("div.paypal-logo").html("\n<br />curOffset: " + curOffset + "\n<br />newOffset: " + newOffset + "\n<br />ulTotalWidth: " + (0 - ulTotalWidth + 610));
	});
	
	var numAcc = jQuery(".block-content.vertical ol").children().length;
	var curScroll = jQuery(".block-content.vertical ol").css("top");
	if( numAcc > 2 ){
		var maxScroll = (numAcc - 2) * 100;
		jQuery("#view-field-accessories a.prevPage").click(function(){
			if( parseInt(jQuery(".block-content.vertical ol").css("top")) < 0){
				jQuery(".block-content.vertical ol").animate({"top": "+=100px"});
			}
		});
		jQuery("#view-field-accessories a.nextPage").click(function(){
			var tempTop = parseInt(jQuery(".block-content.vertical ol").css("top"));
			if( !tempTop ){ tempTop = 0; }
			if( tempTop > (0 - maxScroll)){
				jQuery(".block-content.vertical ol").animate({"top": "-=100px"});
			}
		});
	}
}

function linksInit(){
	jQuery("a.quick-links-link").click(function(e){
		e.preventDefault();
		if(jQuery("#quick-links").css("height") == "122px"){
//			jQuery(this).children("span").css("background-position", "107px 0px");
			jQuery(this).children("span").animate({
				top: "0px"
			}, 500);
			jQuery("#quick-links").animate({
				height: "0px"
			}, 500);
		}else{
			jQuery(this).children("span").animate({
				top: "-29px"
			}, 500);
			jQuery("#quick-links").animate({
				height: "122px"
			}, 500);
		}
	});
}

function changeProducts(elem,el)
{	
	jQuery('.product-accessories .scrollerHeader').each(function(){
		jQuery(this).removeClass("active");
	});
	
	jQuery(elem).addClass('active');
	
	jQuery('.product-accessories .scrollerBody').each(function(){
		jQuery(this).hide();
	});
	
	jQuery('#'+el).show();
}
function changeProductFields(elem,el)
{	
	jQuery('.product-fields .scrollerHeader').each(function(){
		jQuery(this).removeClass("active");
	});
	
	jQuery(elem).addClass('active');
	
	jQuery('.product-fields .scrollerBody').each(function(){
		jQuery(this).hide();
	});
	
	jQuery('#'+el).show();
}
