SW.local.flashVideoPlayer = {
    panel:null,
    flashPlayerURL:null,
    selectedNav:null,
    videoArray:[],
    init:function(){
        SW.local.flashVideoPlayer.flashPlayerURL=this.flashPlayerURL;
    },
    loadPlayer:function(mURL,selectedVideoTitle,showNav){
        var videoURL = mURL;
        if (!this.panel){
            var flashPlayerPanel = document.createElement("div");
            flashPlayerPanel.id = "flashPlayerPanel";

            this.panel = new YAHOO.widget.Panel(flashPlayerPanel,{
                close:false,
                visible:false,
                modal:true,
                underlay:"shadow",
                constraintoviewport:false,
                fixedcenter: true,
                zIndex:1030
            });
            this.panel.hideEvent.subscribe(function(){
                var flashMovieContainer = yuiDom.get("flashcontentMediaPlayer");
                flashMovieContainer.innerHTML = "";
            });
            var flashPlayerClose = yuiDom.get("closeMediaWindow");
            yuiDom.addClass(flashPlayerClose,"closeIcon");
            yuiEvent.addListener(flashPlayerClose,"click",function(){
                SW.local.flashVideoPlayer.panel.hide();
            });
            var playerPopupContainer = yuiDom.get("mediaPlayerContainer");

            var title = yuiDom.get("videoTitle");
            title.innerHTML = selectedVideoTitle;

            this.panel.setBody(playerPopupContainer);
            this.panel.render(document.body);
            yuiDom.setStyle(playerPopupContainer,"display","block");
        }
        this.panel.show();
        this.playMovie(videoURL);
        if (showNav){
            this.writeVideoNav(selectedVideoTitle);
        }
    },
    writeVideoNav:function(selectedVideoTitle){
        var sectionTitles=document.getElementById("sectionTitles");
        sectionTitles.innerHTML ="";
        var sectionVideoArray = SW.local.flashVideoPlayer.videoArray;
        if  (sectionVideoArray.length > 0 ) {
            for(i=0; i < sectionVideoArray.length; i++){
                var sectionNavItemContainer= document.createElement("li");
                var sectionNavItem= document.createElement("a");
                sectionNavItemContainer.appendChild(sectionNavItem);
                sectionNavItem.href = sectionVideoArray[i].videoURL;
                sectionNavItem.id=sectionVideoArray[i].videoTitle;
                if (selectedVideoTitle == sectionVideoArray[i].videoTitle) {
                    SW.local.flashVideoPlayer.selectedNav = sectionNavItem;
                    yuiDom.addClass(sectionNavItem,"locked");
                }
                sectionNavItem.innerHTML=sectionVideoArray[i].videoTitle;
                sectionTitles.appendChild(sectionNavItemContainer);

                yuiEvent.addListener(sectionNavItem,"click",SW.local.flashVideoPlayer.playMovieBridge,{videoURL:sectionVideoArray[i].videoURL});
            }
        }
    },
    playMovie:function(videoURL){
        var swfObj = new SWFObject(SW.local.flashVideoPlayer.flashPlayerURL, "player", "520", "400", "8,0,0,0", "#EBEAE6");
        swfObj.addVariable("xContentPath",videoURL);
        swfObj.addParam("allowScriptAccess","sameDomain");
        swfObj.addParam("wmode","Opaque");
        swfObj.setAttribute("redirectUrl","");
        swfObj.write("flashcontentMediaPlayer");
    },
    playMovieBridge:function(e,params){
        yuiEvent.preventDefault(e);
        yuiDom.removeClass(SW.local.flashVideoPlayer.selectedNav,"locked");
        yuiDom.addClass(this,"locked");
        SW.local.flashVideoPlayer.selectedNav=this;
        SW.local.flashVideoPlayer.playMovie(params.videoURL);
    },
    loadPlayerBridge:function(e,config){
        yuiEvent.preventDefault(e);
        SW.local.flashVideoPlayer.loadPlayer(config.videoURL,config.videoName,config.showNav);
    }
};

SW.local.relatedLinks = {
    init:function(){
        var videoTourTiggers = yuiDom.getElementsByClassName('virtualTourIcon','a');
        videoTourTiggers.forEach(function(tigger){
            yuiEvent.addListener(tigger,"click",SW.local.relatedLinks.openVirtualTourWindow,tigger);
        });

        var videoPopupTiggers = yuiDom.getElementsByClassName("videoTourIcon",'a');
        var count=0;
        if (videoPopupTiggers.length>0){
            videoPopupTiggers.forEach(function(tigger){
                SW.local.flashVideoPlayer.videoArray[count++]=({videoURL :tigger.href,videoTitle:tigger.id});
                var config ={
                    videoURL:tigger.href,
                    videoName:tigger.text,
                    showNav:false
                }
                yuiEvent.addListener(tigger,"click",SW.local.flashVideoPlayer.loadPlayerBridge,config);
            });
        }

        var destinationPopupTriggers = yuiDom.getElementsByClassName('destinationPopup','a');
        destinationPopupTriggers.forEach(function(tigger){
            yuiEvent.addListener(tigger,"click",SW.local.relatedLinks.openDestinationWindow,tigger);
        });
    },
    openVirtualTourWindow:function(e,tigger){
        yuiEvent.preventDefault(e);
        window.open(tigger.href,'virtualtour','toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width=612,height=427');
    },
   /* openVideoTourWindow:function(e,tigger){
        yuiEvent.preventDefault(e);
      //  window.open(tigger.href,'virtualtour','toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width=612,height=427');
    },*/
    openDestinationWindow:function(e,tigger){
        yuiEvent.preventDefault(e);        
        window.open(tigger.href,'destination','toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,width=800,height=600');
    }
};
/* TEMP FLASH PLAYER  ----- REPLACE WITH CORRECT PLAYER */
yuiEvent.onDOMReady(SW.local.flashVideoPlayer.init,{flashPlayerURL:"/sheraton/common/components/video/flv_player.swf"},true);
yuiEvent.onDOMReady(SW.local.relatedLinks.init,true);
