var addedDivNum = 2;
var nextInterval = null;
var activeRotatingImage = 1;
var nextRotatingImage = 2;
var numRotatingImages = 0;
var arrayPos = 0;

function rotateImage() {
	numRotatingImages = $('.hp-slide').length;
	// Checks to make sure their is more than one image before attempting to rotate
	if(numRotatingImages > 1) {
        var $active = $(".hp-slide.active");
        var $next = $active.next(".hp-slide");
        if ($next.size() == 0) {
            $next = $(".hp-slide:first");
        }
		$next.css('z-index',4).fadeIn(1000, function() {
			$active.css('display','none').css('z-index',2);
			$next.css('z-index',3);
		});		

        $(".hp-slide").removeClass("active");
        $next.addClass("active");

		// This part of the function creates the loop so the rotation continues infinitely
		try {
			nextInterval = setTimeout(rotateImage,8000);
		}catch(e){}
		return false;
	}
}

function loadSlides(){
    for (var i = 0; i < photoArray.length; i++) {
        var addedDivNum = i + 2;
        if (photoArray[0][i] != null && photoArray[0][i].length > 0) {
            $('#home-hero').append('<div id="hp-slide-' + addedDivNum + '" class="hp-slide"></div>');
            var htmlStr = '<img src="' + photoArray[0][i] +  '" class="slide-show-image" border="0" width="966" height="469">';
            htmlStr = htmlStr + photoArray[1][i];
            $('#hp-slide-' + addedDivNum).html(htmlStr);
        }
    }
}

function writeUsesPopup(popupType,engineUse) {
	
	var $popupTemplate = $(".uses-popup").clone();
	$('#eng-nav-icons-popup').append($popupTemplate);
	$('#eng-nav-icons-popup .uses-popup').attr('id',popupType + '-uses-popup');
	$('#eng-nav-icons-popup .uses-popup .popup-leftCol').attr('id',engineUse + '-leftCol');
	$.get('/ajax/getSeriesProducts.htm?enginesUse=' + engineUse + '&useType=' + popupType, '', function(data){
		$('#eng-nav-icons-popup .use-text-title').html(data.enginesCategory.enginesUseTrans);
		
		$.each(data.enginesCategory.engines, function(idx,engine){
			var $engineInfo = '<li class="popup-engine">';
			$engineInfo += '<a href="' + engine.engineSeriesUrl + '" class="list-' + idx + '">';
			if (popupType == 'home') {
				$engineInfo += '<img src="/onlinecatalog/jpg135x87/' + engine.engineImg + '" class="popup-engine-pic" border="0">';
			} else {
				$engineInfo += '<img src="/onlinecatalog/jpg90x58/' + engine.engineImg + '" class="popup-engine-pic" border="0">';
			}
			$engineInfo += '<div class="popup-engine-text"><p>';
			$engineInfo += '<span class="engine-text-title">' + engine.engineName + '</span>';
			$engineInfo += '<span class="engine-text-hp">' + engine.engineHp + '</span>';
			$engineInfo += '<span class="engine-text-descrip">' + engine.engineDesc + '</span>';
			$engineInfo += '</p></div>';
			$engineInfo += '</a>';
			$engineInfo += '</li>';
			$('#eng-nav-icons-popup ul.popup-engines-list').append($engineInfo);

            if(engine.brands.length && popupType != 'pro') {
                //$('#eng-nav-icons-popup ul#engine-brands').append('<li class="popup-list-title">Brands</li>');
                $.each(engine.brands, function(i, val) {
                    $('#eng-nav-icons-popup ul#engine-brands').append('<li class="brands-list-' + idx + '">' + val +  '</li>');
                });
            }

			if(engine.whereToBuy.length && popupType != 'pro') {
				//$('#eng-nav-icons-popup ul#engine-wtb').append('<li id="list-wtb-title" class="popup-list-title">Where to Buy</li>');
				$.each(engine.whereToBuy, function(i, val) {
					$('#eng-nav-icons-popup ul#engine-wtb').append('<li class="wtb-list-' + idx + '">' + val +  '</li>');
				});
			}
				
				
		});
		hideClueTip();
		if(!$.browser.msie){
			$('#eng-nav-icons-popup').hide().fadeIn("slow");
		}else if($.browser.msie && $.browser.version.substr(0,1)>=8){
			$('#eng-nav-icons-popup').hide().fadeIn("slow");
		}else{
			enableAlphaImages();
			$('#eng-nav-icons-popup').show();
		}	
	},"json");
}

function closePopUp(){
	$('#eng-nav-icons-popup').fadeOut('slow', function() { $('#eng-nav-icons-popup').html(""); } );
}

function hideClueTip(){
	$('#cluetip').stop(true,true).hide();
}

$(function(){
	
	// Hover controls for the home page Spiffs
	$('a.engn-homespiff-copy').hover(
		function() {
			$(this).children('img').stop().animate({"opacity": 1},250);			
		}, function() {
			$(this).children('img').stop().animate({"opacity": 0},250);		
		}	
	);
	
	//Adding the ClueTips for the Nav Icons
	if ($.browser.msie && $.browser.version.substr(0,1)<7) {
		
		// becasue IE6 is lame, we can't have an auto width.
		$('.hp-icon').cluetip({
			
	    	splitTitle: '|',
	    	showTitle: false,
			cursor:'pointer',
			positionBy:'mouse',
			cluezIndex:999,
			tracking:true,
			cluetipClass: 'rounded',
			dropShadow: false,
			width:100,
			fx: {
	              open:       'fadeIn',
	              openSpeed:  '1'
	    		}
	    });

	}else{
	
		$('.hp-icon').cluetip({
	    	splitTitle: '|',
	    	showTitle: false,
			cursor:'pointer',
			positionBy:'mouse',
			cluezIndex:999,
			tracking:true,
			cluetipClass: 'rounded',
			dropShadow: false,
			fx: {
	              open:       'fadeIn',
	              openSpeed:  '1'
	    		}
	    });

	}
	
	
	$("li.popup-engine a").live("mouseover", function() {
		var whichList = $(this).attr("class");
		if($("li.wtb-" + whichList).length) {
			$("li#list-wtb-title").css('display','block');
			$("li.wtb-" + whichList).css('display','block');
		}
	});
	$("li.popup-engine a").live("mouseout", function() {
		var whichList = $(this).attr("class");
		if($("li.wtb-" + whichList).length) {
			$("li#list-wtb-title").css('display','none');
			$("li.wtb-" + whichList).css('display','none');
		}
	});

    $("li.popup-engine a").live("mouseover", function() {
		var whichList = $(this).attr("class");
		if($("li.brands-" + whichList).length) {
			$("li#list-brands-title").css('display','block');
			$("li.brands-" + whichList).css('display','block');
		}
	});
	$("li.popup-engine a").live("mouseout", function() {
		var whichList = $(this).attr("class");
		if($("li.brands-" + whichList).length) {
			$("li#list-brands-title").css('display','none');
			$("li.brands-" + whichList).css('display','none');
		}
	});
	
	// Bind an additional mouseout to make sure the cluetip goes away.
	$('#eng-nav-icons').mouseout(function(){
		hideClueTip();
	});
	
	// run this on load of the home page
	$(document).ready(function(){
		loadSlides();
		rotateImage();
	});
});

