var BannerIsHidden = true;
var CurrentBanner = 1;
var MainCount = 0;

//commonly used objects
var MasterarrowcontainerObj;
var NextarrowcontainersObj;
var PrevarrowcontainerObj;
var CurrentbannerObj;

var BannerType = "Homepage";

if(window.location.pathname != "/"){
  BannerType = "Product";
}
$(document).ready(function() {
    //configure banners
    $(".bannercontainer").each(function(e) {

        //show the first banner
        if (e == 0) {
            this.style.marginLeft= "0";
        }

        //get objects to be used in the click event
        var moretext = $(this).find(".moretext");
        var hiddenimg = $(this).find(".hiddenimg");
        var expimg = $(this).find(".expimg");
        
        var flashcontent = $(this).find(".flashcontent");
        var pathtoswf = $(this).find(".pathtoswf");
        var pathtoflv = $(this).find(".pathtoflv");
        var flashimage = $(this).find(".flashimage");

	//load any flash content contained within the banners
	if(pathtoswf.length && flashcontent.length){
		var s1 = new SWFObject(pathtoswf.val(),flashcontent[0].id + "swfobj","356","200","9","#FFFFFF");
		s1.addParam("allowfullscreen","true");
		s1.addParam("wmode","transparent");
		s1.addParam("allowscriptaccess","always");
		s1.addParam("flashvars","file=" + pathtoflv.val() + "&image=" + flashimage.val());
		s1.write(flashcontent[0].id);
	}

        $(this).find(".seemorebtn").click(function() {
            clearInterval(AutoScroll);
            if (BannerIsHidden) {
                pageTracker._trackEvent("" + " Banners", "", "");
                pageTracker._trackEvent(BannerType + " Banners", "See more button click", CurrentBanner.toString());
                //slide content, show hide button
                this.blur();

                $(this).animate({ "marginLeft": "865px" }, 300)
                  .removeClass("seemorebg")
                  .addClass("hidemorebg");
                  
                  if(isIE6){
	                  $(this).html("<span id='seemoreimg'></span>");
	                  $("#seemoreimg").supersleight();
                  }

                hiddenimg.animate({ "marginLeft": "-300px" }, 100);

                moretext.animate({ "width": "903px" }, 300);

                $(moretext).find(".revealcontent").animate({ "marginLeft": "0" }, 300, function(){
                });

                BannerIsHidden = false;
                
                
            }
            else {
                pageTracker._trackEvent(BannerType + " Banners", "Hide button click", CurrentBanner.toString());
                //slide content back, show see more button
                this.blur();

                $(this).animate({ "marginLeft": "220px" }, 300)
                  .removeClass("hidemorebg")
                  .addClass("seemorebg");

		if(isIE6){
			$("#seemoreimg").remove();
		}
		
                $(moretext).find(".revealcontent").animate({ "marginLeft": "-900px" }, 300);
                
                //stop to flash video from playing
                if(flashcontent[0] != null){
	                var swf = $("#" + flashcontent[0].id + "swfobj")[0].sendEvent('STOP');
                }
                
                
                moretext.animate({ "width": "260px" }, 300, function() {
                        hiddenimg.animate({ "marginLeft": "0px" }, 100);
                });
                                

                BannerIsHidden = true;
            }
        });
        MainCount++;
        
    });
    

    $(".bannercount").html(MainCount);

    //add the next arrow containers to each banner
    MasterarrowcontainerObj = $("#masterarrowcontainer");
    $(".morecontainer").prepend("<div id='arrowcontainer' class='nextarrowcontainer'>" + MasterarrowcontainerObj.html() + "</div>");

    //load commonly used object
    NextarrowcontainersObj = $(".nextarrowcontainer");
    PrevarrowcontainerObj = $("#prevarrowcontainer");
    CurrentbannerObj = $(".currentbanner");


    $(".nextarrowcontainer").click(function() {
        clearInterval(AutoScroll);
        pageTracker._trackEvent(BannerType + " Banners", "Next button click", CurrentBanner.toString());
        
        var temp = CurrentBanner;
        $(".banner" + CurrentBanner).css("z-index", "3");

        CurrentBanner++;

        if (CurrentBanner > MainCount) {
          CurrentBanner = 1;
        }
        
        //overlay master arrow container for content to slide underneath
        MasterarrowcontainerObj.show();
        NextarrowcontainersObj.hide();

        $(".banner" + CurrentBanner)
        	.css({
        	    "z-index": "4",
        	    "margin-left": "920px",
        	    "display": "block"
        	})
        	.animate({ "marginLeft": "0px" }, 500, function() {
        	    CurrentbannerObj.html(CurrentBanner);


        	    NextarrowcontainersObj.show();
        	    MasterarrowcontainerObj.hide();
        	    
        	    //more the previous banner off screen
        	    $(".banner" + temp).css("margin-left", "1000px");
        	    
        	    
        	    
        	});
    });
    
    
    //fix pngs
    $(".seemorebtn, .foreground, .nextarrowcontainer, #masterarrowcontainer").supersleight();
    
    //fix cursor issues
    /*
    $("area").mouseover(function(){
    $(this).css('cursor', 'pointer');
    });

    */
    $(".bannercontainer img").mouseover(function(){
    $(this).css('cursor', 'default');
	});
	
	$(".bannercontainer img").css('cursor', 'default');
	
	$(".lbthumb img").mouseover(function(){
    $(this).css('cursor', 'pointer');
	});
	
	//track banner link clicks
	$("#homefeaturedcontainer area").each(function(){
	    var href = $(this).attr("href");
      $(this).attr("href", "#").click(function(e){
        e.preventDefault();
        pageTracker._trackEvent(BannerType + " Banners", "Image map click", href, CurrentBanner);
        setTimeout("window.location.href = '" + href + "'", 300);
      });
  });
  
  var AutoScroll = setInterval("NextBanner()", 4000);
});

function NextBanner(){
  pageTracker._trackEvent(BannerType + " Banners", "Next button click", CurrentBanner.toString());

  var temp = CurrentBanner;
  $(".banner" + CurrentBanner).css("z-index", "3");

  CurrentBanner++;

  if (CurrentBanner > MainCount) {
    CurrentBanner = 1;
  }

  //overlay master arrow container for content to slide underneath
  MasterarrowcontainerObj.show();
  NextarrowcontainersObj.hide();

  $(".banner" + CurrentBanner)
  	.css({
  	    "z-index": "4",
  	    "margin-left": "920px",
  	    "display": "block"
  	})
  	.animate({ "marginLeft": "0px" }, 500, function() {
  	    CurrentbannerObj.html(CurrentBanner);


  	    NextarrowcontainersObj.show();
  	    MasterarrowcontainerObj.hide();

  	    //more the previous banner off screen
  	    $(".banner" + temp).css("margin-left", "1000px");



  	});
}