/**
 * HomagepageSlideShow 
 * 
 * @author arjaya
 * @desc this manages the pfa slide show with flash support
 */   

HomepageSlideShow = function(){
    
//elements
    this.$bannerWindow  = jQuery("#banner-window");
    this.$bannerContent = jQuery("#banner-content");
    this.$bannerDesc    = jQuery("#banner-desc");
    this.$bannerMenu    = jQuery("#banner-menu");
    
//data
    this.deeplinkID       = this.getDeeplinkID();
    this.curFlashSlideID  = "";
    this.prevFlashSlideID = this.curFlashSlideID;
    this.flashSlides      = []; //keeps references to all flash slides
    this.numFlashSlides   = 0; //counts how many flash slides there are
    this.contentHeight    = this.$bannerContent.height();
    
//initial calls
    this.$bannerDesc.css("display", "block");
    
    this.initSlideShow();
}


HomepageSlideShow.prototype.initSlideShow = function(){
    
    var self = this;
    
    this.$bannerWindow.slideshow({
        speed              : 900, //changed from 1200
        timeout            : 5000, //changed from 7500
        type               : 'sequence',
        autoRotate         : true,
        firstSlide         : this.deeplinkID,
        maxRotateCount     : 1, //auto rotate slide show once and stop
        pauseOnHover       : true, //pause slideshow when mouse is over it
        pauseOnNavClick    : true, //once they click on a slideshow nav button, pause the slideshow
        slideDesc          : 'banner-desc',
        slideShowNav       : 'banner-menu',
        slideShowThumbnail : 'banner-thumbs',
        slideChangeStart   : function (args) { self.onSlideChangeStart(args); },
        slideChangeEnd     : function (args) { self.onSlideChangeEnd(args); }
    });
    
    /*
     * Pauses the slideshow if the first slide has flash and the user has flash player
     */
    if(this.$bannerWindow.find("li:first").find(".flash-slide").length > 0){
        if(flashMajorVer >= 9){
            this.$bannerWindow.data("SlideShow").togglePause();
            this.$bannerDesc.find("li:first").find("dl").hide();
            this.$bannerContent.height(20);//make the height of the content 20 so you can interact with flash
        }else{
            this.$bannerWindow.find("li:first").find(".flash-slide").hide();
        }
    }

    this.sendInitialCRMEvent();
    this.initFlashSlides();
}


/**
 * CHECK IF ANY SLIDES ARE FLASH
 * If so create a Flash SLide for each one
 * 
 * @return
 */
HomepageSlideShow.prototype.initFlashSlides = function(){
    
    var self  = this;
    
    this.$bannerWindow.find(".flash-slide").each(function(){
        
        var $element = jQuery(this);
        
        if(flashMajorVer >= 9){
            
            var id         = $element.attr("id");
            var slideURL   = $element.find("a").attr("href");
            
            var $parent    = $element.parent();
            var slideIndex = self.$bannerWindow.find("li").index($parent);
            var autoPlay   = (self.deeplinkID == slideIndex);
            
            //hide the description area if it's flash
            var slideDescID     = $parent.attr("id").replace("banner-", "").capitalize();
            var prettySlideName = slideDescID;
            slideDescID         = "pfa"+slideDescID;
            self.$bannerDesc.find("#"+slideDescID+" dl").hide();
            
            prettySlideName = self.getPrettySlideName(prettySlideName);
            
            var options = {
                    slideURL         : slideURL, //url to the slide that will be loaded
                    autoPlay         : autoPlay, //true only if flash slide is the first slide
                    slideIndex       : slideIndex+1, //the position of the slide used for tagging
                    slideName        : prettySlideName, //used for tagging
                    pageType         : "Flash", //used for tagging
                    height           : 770,
                    width            : 1200,
                    onEmbedComplete  : function(){ },
                    onAnimationStart : function(){ },
                    onAnimationEnd   : function(){ self.$bannerWindow.data("SlideShow").togglePause(); },
                    onCTAClick       : function(id){ }
            }
            
            var flashSlide = new FlashSlide(id, options);
            
            self.flashSlides[id]  = flashSlide;
            self.numFlashSlides++;
            
            //if this slide is the first slide
            if(slideIndex == 0){
                self.curFlashSlideID  = id;
                self.prevFlashSlideID = self.curFlashSlideID;
            }
            
            //remove the background image since it is no longer needed
            $parent.css("background", "#000000");
        }
        
        //hide flash div if there's no flashplayer 9 or higher
        else{
            $element.hide();
        }
        
    });
}


/**
 * Gets called at the beginning of when the new slide is fading in
 * 
 * @param args
 * @return
 */
HomepageSlideShow.prototype.onSlideChangeStart = function(args){
    setBgrPosition();
    
    
    var slideID        = args.slideID +1;
    var slidePlacement = args.cause;       //comes in as 'Click' or 'Auto'
    var slideName      = args.tagName - 1;     //name of the slide
    var count          = args.count;
    
    tagName = String(this.$bannerDesc.find('li').eq(args.slideID).attr('id')||String(slideName)).toLowerCase();
    
    tagName = this.getPrettySlideName(tagName);

    var $bannerSlide = this.$bannerWindow.find("li").eq(args.slideID);
    
    this.curFlashSlideID = "";
    
    //return the banner content's  height to the original height
    if(this.$bannerContent.height() < this.contentHeight){
        this.$bannerContent.height(this.contentHeight);
    }
    
    //Pauses the slideshow if the slide is flash
    if($bannerSlide.find("object").length > 0 && flashMajorVer >= 9){
        $bannerSlide.find("object").height(770);
        this.curFlashSlideID  = $bannerSlide.find("object").attr("id");
        this.$bannerWindow.data("SlideShow").togglePause();
    }

    this.sendSlideCRMEvent(slideID, tagName, biPageType, count, slidePlacement);
}


/**
 * Called when the new slide is finished showing
 * Use this for calling events on the Flash Slides.
 * 
 * @param args
 * @return
 */
HomepageSlideShow.prototype.onSlideChangeEnd = function(args){
    
    var $bannerSlide = this.$bannerWindow.find("li").eq(args.slideID);
    
    
    if(this.numFlashSlides > 0  && flashMajorVer >= 9){
        
        //stop the previous flash slide if there was one
        if(this.prevFlashSlideID != ""){
            if(typeof this.flashSlides[this.prevFlashSlideID] !== "undefined"){
                jQuery("#"+this.prevFlashSlideID).height(1);
                this.flashSlides[this.prevFlashSlideID].stopSlide();
            }
        }
        
        // tell the new flash slide to play
        if(this.curFlashSlideID != ""){
            if(typeof this.flashSlides[this.curFlashSlideID] !== "undefined"){
                this.flashSlides[this.curFlashSlideID].playSlide();
            }
            
            this.$bannerContent.height(20);
        }
    }
    
    this.prevFlashSlideID = this.curFlashSlideID;
}



HomepageSlideShow.prototype.sendInitialCRMEvent = function(){
    
    var tagName     = String(this.$bannerDesc.find('li').eq(this.deeplinkID).attr('id')||String(this.deeplinkID)).toLowerCase();
    var bannerID    = "banner-"+tagName.replace("pfa", "");
    
    tagName = this.getPrettySlideName(tagName);

    crmEvent1({ "num": this.deeplinkID+1, "placement": "Auto", "name": tagName, "pageType": biPageType, "flashHTML": biPageType });
}


/**
 * Tagging events fired when slide changes
 * placement -- Auto or Click depending if it was from a click or auto rotation
 */
HomepageSlideShow.prototype.sendSlideCRMEvent = function(slideID, tagName, biPageType, count, slidePlacement){
    

    //console.log("sendSlideCRMEvent  slideID "+slideID+" tagName "+tagName);
    
    //tagging everytime the slide auto rotates to the next one. Stop tagging after the first time it reaches the end.
    if(slidePlacement == "Auto"){

        if(count < 1){
            crmEvent4({"num":slideID, "placement":"Auto", "name":tagName, "pageType":biPageType, "flashHTML":biPageType});
        }
        
        //if they deep link to a PFA that isn't the first one, this allows event1 to fire for the first PFA when it loops back for the first time
        else if(this.deeplinkID > 0){
            this.deeplinkID = 0;
            crmEvent4({"num":slideID, "placement":"Auto", "name":tagName, "pageType":biPageType, "flashHTML":biPageType});
        }
         
  //Tagging for when they click on a slideshow nav button
    }else{
        crmEvent4({"num":slideID, "placement":"Number" , "name":tagName, "pageType":biPageType, "flashHTML":biPageType});
    }
}



/**
 * Check if they are deeplinking to a specific slide
 * 
 * @return 
 */
HomepageSlideShow.prototype.getDeeplinkID = function(){
    
    var self   = this;
    var tempID = 0;
    
    if(/pfa=([\w\-]+)/.test(location.hash)){
        var pfa=RegExp.$1;  
        this.$bannerContent.find('li h1').each(function(i){
            if(this.innerHTML.toLowerCase().indexOf(pfa)>=0){
                tempID = 0+i;
            }
        });
    }
    
    return tempID;
}




HomepageSlideShow.prototype.getPrettySlideName = function(tagName){
    
    //removes "pfaInnovation-" from the tagName and Capitalizes
    if(tagName.indexOf("innovation") != -1){
        tagName = tagName.replace(/pfa[a-z]+[-_]/ig,'').replace(/\b\w/,function(letter){ 
            return letter.toUpperCase()
        });
    }
    
    //removes pfa and "-" from the tagName and Capitalizes each word
    else{
        tagName = tagName.replace("pfa", "").toString();
        
        if(tagName.indexOf("-") != -1){
            tagName = tagName.replace(/-/g, " "); //replace all instances of -
            tagName = tagName.capitalize();
            tagName = tagName.replace(/(\s)/g, ""); //remove all spaces
        }else{
            tagName = tagName.capitalize();
        }
    }
    
    return tagName;
}
    


String.prototype.capitalize = function(){
   return this.replace( /(^|\s)([a-z])/g , function(m,p1,p2){ return p1+p2.toUpperCase(); } );
}

//clickable menu
function bringToFront(){
    jQuery("#banner-menu").css("z-index","1380");
}
function sendToBack(){
    jQuery("#banner-menu").css("z-index","30");
}

