var moreInfoTimer;
var moreInfoStatus = 1; // Previous 2 Variables used in cursor.js

// featuredBrandIndex variable declared in page
var lastBrand = brandObject.img.length - 1;

jQuery.preloadImages = function() {
	for(var i = 0; i<arguments.length; i++) {
		jQuery("<img>").attr("src", arguments[i]);
	}
}

$.preloadImages(brandObject.img[1],brandObject.img[lastBrand]);

(function($) {
	$(document).ready(function() {	
		if (featuredBrandIndex == 0) {
			$("#brand-previousFeatured").attr("src", "/images/" + brandObject.img[lastBrand]);
			$("#brand-nextFeatured").attr("src", "/images/" + brandObject.img[featuredBrandIndex + 1]);
		} else if (featuredBrandIndex == lastBrand) {
			$("#brand-previousFeatured").attr("src", "/images/" + brandObject.img[featuredBrandIndex - 1]);
			$("#brand-nextFeatured").attr("src", "/images/" + brandObject.img[0]);		
		} else {
			$("#brand-previousFeatured").attr("src", "/images/" + brandObject.img[featuredBrandIndex - 1]);
			$("#brand-nextFeatured").attr("src", "/images/" + brandObject.img[featuredBrandIndex + 1]);		
		}
	
		$("#brand-previous").click(
            function() {
				$("#brand-previousFeatured").fadeIn(1500).queue(
					function() {
						isIndexZero = featuredBrandIndex == 0;
						featuredBrandIndex = isIndexZero ? lastBrand : featuredBrandIndex - 1;
						$("#brand-moreInfo").attr("href", brandObject.href[featuredBrandIndex]);
						active_brand_slug = brandObject.slug[featuredBrandIndex];
						if(isIndexZero) {
							$("#brand-featured").attr("src", "/images/" + brandObject.img[featuredBrandIndex]);
							document.getElementById('brand-previousFeatured').style.display='none';
							var temp = this;
							setTimeout(function(){$(temp).attr("src", "/images/" + brandObject.img[(featuredBrandIndex !=0)?featuredBrandIndex - 1:brandObject.img.length-1]);},300);
							$("#brand-nextFeatured").attr("src", "/images/" + brandObject.img[0]);
						} else {
							$("#brand-featured").attr("src", "/images/" + brandObject.img[featuredBrandIndex]);
							document.getElementById('brand-previousFeatured').style.display='none';
							var temp = this;
							setTimeout(function(){$(temp).attr("src", "/images/" + brandObject.img[(featuredBrandIndex !=0)?featuredBrandIndex - 1:brandObject.img.length-1]);},300);
							$("#brand-nextFeatured").attr("src", "/images/" + brandObject.img[featuredBrandIndex + 1]);		
						}
						moreInfoStatus = 1;
						moreInfoTimer = null;
						$(this).dequeue();
					}
				)	
			}
		);
		$("#brand-next").click(
            function() {
				$("#brand-nextFeatured").fadeIn(1500).queue(
					function() {
						isIndexLast = featuredBrandIndex == lastBrand;
						featuredBrandIndex = isIndexLast ? featuredBrandIndex = 0 : featuredBrandIndex + 1;
						$("#brand-moreInfo").attr("href", brandObject.href[featuredBrandIndex]);
						$("#brand-featuredImg").attr("href", brandObject.href[featuredBrandIndex]);
						active_brand_slug = brandObject.slug[featuredBrandIndex];
						if(isIndexLast) {
							$("#brand-featured").attr("src", "/images/" + brandObject.img[featuredBrandIndex]);
							document.getElementById('brand-nextFeatured').style.display='none';
							$("#brand-previousFeatured").attr("src", "/images/" + brandObject.img[lastBrand]);
							var temp = this;
							setTimeout(function(){$(temp).attr("src", "/images/" + brandObject.img[(featuredBrandIndex + 1) % brandObject.img.length]);},300);
						} else {
							$("#brand-featured").attr("src", "/images/" + brandObject.img[featuredBrandIndex]);
							document.getElementById('brand-nextFeatured').style.display='none';
							$("#brand-previousFeatured").attr("src", "/images/" + brandObject.img[featuredBrandIndex - 1]);
							var temp = this;
							setTimeout(function(){$(temp).attr("src", "/images/" + brandObject.img[(featuredBrandIndex + 1) % brandObject.img.length]);},300);
						
						}
						moreInfoStatus = 1;
						moreInfoTimer = null;
						$(this).dequeue();
					}
				)
			}
		);
    });
})(jQuery);	

// Simple follow the mouse script
var divName = 'moreInfo'; // div that is to follow the mouse
                       // (must be position:absolute)
var offX = -148;          // X offset from mouse position
var offY = -25;          // Y offset from mouse position

function mouseX(evt) {if (!evt) evt = window.event; if (evt.pageX) return evt.pageX; else if (evt.clientX)return evt.clientX + (document.documentElement.scrollLeft ?  document.documentElement.scrollLeft : document.body.scrollLeft); else return 0;}
function mouseY(evt) {if (!evt) evt = window.event; if (evt.pageY) return evt.pageY; else if (evt.clientY)return evt.clientY + (document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop); else return 0;}

function follow(evt) {
	if (document.getElementById) {
		var obj = document.getElementById(divName).style; obj.visibility = 'visible';
		if (navigator.appVersion.indexOf("Mac")!=-1) {
			if ($.browser.safari) {
				obj.left = ((parseInt(mouseX(evt))+offX) + 124) + 'px';
				obj.top = (parseInt(mouseY(evt))+offY) + 'px';
			} else {
				obj.left = ((parseInt(mouseX(evt))+offX) + 150) + 'px';
				obj.top = (parseInt(mouseY(evt))+offY) + 'px';
			}
			
		} else {
			obj.left = (parseInt(mouseX(evt))+offX) + 'px';
			obj.top = (parseInt(mouseY(evt))+offY) + 'px';
		}
	}
};

(function($) {
	function removeMoreInfo() {
		$("#moreInfo").animate({opacity:0},300);
		$("#brand-featured").unbind('mousemove',follow)
		moreInfoStatus = 0;
	}
	
	
	$(document).ready(function() {
 		$("#brand-featured").hover(
 			function() {
				if(moreInfoStatus == 1) {
	 				$("#brand-featured").mousemove(follow);
					$("#moreInfo").css("opacity",100);
				}
				if(!moreInfoTimer){
					moreInfoTimer = setTimeout(removeMoreInfo,2500);
				}
 			}, function() {
				$("#moreInfo").css("visibility","hidden");
				$("#brand-featured").unbind('mousemove',follow)
 			}
 		);
     });
})(jQuery);