ctv.ca.ContentStack = function (configController) 
{
	this.configController = configController;
    this.PlayListArr = new Array();
    this.tabNamesArr = new Array();
    this.dataMsgArr = new Array();
    this.XmlUrlArr = new Array();
    this.callingTab = 0;
    this.initDataLoaded = false;
    
    this.currentTabIndex = 0;
    this.currentPlayingList = null;
    this.nextPlayingList = null;
    this.currentVideo = null;
    this.nextVideo = null;
    this.currentClip = null;
    this.nextClip = null;
    
    this.UIType = "";
    this.UIObj = null;
	
	this.playLoadedList = false;
	
	this.dlVid = 0;

    ctv.ca.ContentStack.createEvent("initDataLoaded");
    ctv.ca.ContentStack.createEvent("playVideo");
    ctv.ca.ContentStack.createEvent("currentPlaylistChange");
    ctv.ca.ContentStack.createEvent("currentVideoChange");
    ctv.ca.ContentStack.createEvent("currentClipChange");   
}

ctv.ca.ContentStack.prototype = {
    createUI : function() {

        //$get("clipStackBG").style.height = stackHeight + 8 + "px";
        
        if (this.configController.contentStackType == 'swf')
        {        	        	
            this.createSWFUI();
        }
        else if (this.configController.contentStackType == 'html')
        {
            this.createHTMLUI();
        }
    },
    
    createSWFUI : function()
    {
    	try
        {
        	var stackWidth = 881;
            var stackHeight = 215;
            
            this.UIType = "swf";
            
			var so = new SWFObject(appPath + '/swf/TabbedClipStack2.swf','swfStack',stackWidth,stackHeight,'9');
			//var so = new SWFObject(appPath + '/swf/TabbedClipStack2-debug.swf','swfStack',stackWidth,stackHeight,'9');
			var qs = new Querystring();

            so.addVariable("width",stackWidth);
            so.addVariable("height",stackHeight);           
            
            
            //For Colors - transparent = comment it out
            so.addVariable("bgColor","#346AA8");           
            
            so.addVariable("bgAlpha","0.3");
            so.addVariable("bgColorOver","#397BC6");           
            so.addVariable("bgColorSelected","#346AA8");
            
            //For the Title font
            so.addVariable("fontTitleColor","#71B9DF");        
            so.addVariable("fontTitleColorOver","#DFB841");                    
            so.addVariable("fontTitleColorSelected","#DFB841");
            so.addVariable("fontTitleSize","13"); 
            so.addVariable("fontTitleWeight","bold");
            
            //For the alpha 'OVER' value of the clip background
			so.addVariable("clipAlpha", "1.0");
                  
            
            
            //For the description font
            so.addVariable("fontColor","#ffffff");
            so.addVariable("fontColorOver", "#D941DF");
			so.addVariable("fontColorSelected", "#41DF48");
			so.addVariable("fontSize", "9");                       
            so.addVariable("fontWeight","bold");                       
            
            //For the Tab font size
            so.addVariable("tabFontSize","9");
            so.addVariable("toolTip","Watch");
            
            //For the Clip Properties
			so.addVariable("clipWidth","139");
			so.addVariable("clipHeight","145");
			so.addVariable("clipPaddingRight","218");      //this is for the padding between each of the clipCells
			
			//position of the Watch or Play Button
			so.addVariable("cmdBarX", "69");
			so.addVariable("cmdBarY", "8");
			
			//Image Position
			so.addVariable("imageX", "5");
			so.addVariable("imageY", "5");
			
			//Text Position
			so.addVariable("textX", "5");
			so.addVariable("textY", "62");
			
			//Clip Image Size
			// To size Width add Width and not Height
			// To size Height add Height and not Width
			// To size both, add both
			// To size neither, leave blank
			
			so.addVariable("clipImageWidth", "57");
			so.addVariable("clipImageHeight", "57");
				
			
			
			this.currentTabIndex = qs.get("tab", "0");
			so.addVariable("tab",this.currentTabIndex);
			
			this.dlVid = qs.get("vid", "0");
			so.addVariable("vid",this.dlVid);
	
            so.addParam("wmode", "transparent");                       
            so.write($get("clipStack0"));        
            //fix ie bug: external interface fails with flash obj in a form
            window.swfStack = $get("swfStack");            
           
        }
        catch (e)
        {
            //alert(e);
        }
    },
    
    createHTMLUI : function()
    {
        this.UIType = "html";
        this.onUIReady();
    },
    
    loadPlayList : function(serviceId, name) {
        var playList = new ctv.ca.PlayList(0, serviceId, name, new Array());
        
        var xhr = new CTVFlashXMLHttpRequest();
        var params = new Array();
		params.push(playList);
		xhr.HttpCall("GET", ctvConfigController.pubsetServiceUrl + serviceId,"ctvContentStack.processResponse", params);
    },
    
    processResponse : function(response)
    { 	
     	var playList = response[0];
		XMLParser.parsePlayList(playList, response[1]);

		this.PlayListArr[this.callingTab] = playList;
        playList.index = this.callingTab;
        //debug(this.PlayListArr.length);
        
        if (!this.initDataLoaded) 
		{
            this.currentPlayingList = playList;
            this.nextPlayingList = this.currentPlayingList;
			if (this.dlVid == 0)
			{
				//play the first clip
				this.currentVideo = playList.videos[0];
	            this.currentClip = this.currentVideo.clips[0];
            }
			else
			{
				//look for the vid
				var found = false;
				for (var i=0; i<playList.videos.length; i++)
				{
					this.currentVideo = playList.videos[i];
					for (var j=0; j<this.currentVideo.clips.length; j++)
					{
						this.currentClip = this.currentVideo.clips[j];
						if (this.currentClip.clipId == this.dlVid)
						{
							found = true;
							break;
						}						
					}
					if (found)
						break;
				}
				
				if (!found)
				{
					this.currentVideo = playList.videos[0];
					this.currentClip = this.currentVideo.clips[0];
				}
			}
			
			this.nextVideo = this.currentVideo;
            this.nextClip = this.currentClip;				
			
			this.renderPlayList(this.PlayListArr[this.callingTab],this.callingTab);
			this.initDataLoaded = true;
            this._raiseEvent("initDataLoaded",this.initDataLoaded);
        }
		else if (this.playLoadedList)
		{
            this.currentPlayingList = playList;
            this.nextPlayingList = this.currentPlayingList;	
			if (this.dlVid == 0)
			{
				//play the first clip
				this.currentVideo = playList.videos[0];
	            this.currentClip = this.currentVideo.clips[0];
            }
			else
			{
				//look for the vid
				var found = false;
				for (var i=0; i<playList.videos.length; i++)
				{
					this.currentVideo = playList.videos[i];
					for (var j=0; j<this.currentVideo.clips.length; j++)
					{
						this.currentClip = this.currentVideo.clips[j];
						if (this.currentClip.clipId == this.dlVid)
						{
							found = true;
							break;
						}						
					}
					if (found)
						break;
				}
				
				if (!found)
				{
					this.currentVideo = playList.videos[0];
					this.currentClip = this.currentVideo.clips[0];
				}
			}
			this.nextVideo = this.currentVideo;
            this.nextClip = this.currentClip;				
            
            this.initDataLoaded = true;
			this.renderPlayList(this.PlayListArr[this.callingTab],this.callingTab);
			this.playLoadedList = false;
			this._raiseEvent("initDataLoaded",this.initDataLoaded);			
		}
		else
			this.renderPlayList(this.PlayListArr[this.callingTab],this.callingTab); 
    },
      
    loadInitData : function (ltype, value) {
        //this.loadPlayList(50,"Testing");
        //this.loadEpisode(754);
        //this.loadClip(2686);
        //this.loadBin(203);
        if (ltype == 'bid')
            this.loadBin(value);
        else if (ltype == 'sid')
            this.loadPubset(value);
        else if (ltype == 'id')
            this.loadEpisode(value);
        else if (ltype == 'vid')
            this.loadClip(value);
    },
	
	getXmlUrl : function(tabIndex) 
	{		
		this.callingTab = tabIndex;
		
		if (this.XmlUrlArr[tabIndex] == "")
			this.UIObj.errorMsg(this.dataMsgArr[tabIndex], tabIndex);
		else
 			ctvContentStack.loadXmlFile(this.XmlUrlArr[tabIndex]);   
	},
	
	loadXmlFile : function(url) 
	{		
        var playList = new ctv.ca.PlayList(this.callingTab, 0, "xmlfile", new Array());
        
        var xhr = new CTVFlashXMLHttpRequest();
        var params = new Array();
		params.push(playList);
		xhr.HttpCall("GET", url, "ctvContentStack.processResponse", params);	
		
	},
    
    loadPubset : function (pubsetServiceId) {
        this.loadPlayList(pubsetServiceId, "pubsetService" + pubsetServiceId);
    },
    
    loadEpisode : function (episodeId) {
        var playList = new ctv.ca.PlayList(0, episodeId, "Episode" + episodeId, new Array());
        
        var xhr = new CTVFlashXMLHttpRequest();
        var params = new Array();
		params.push(playList);
		xhr.HttpCall("GET", ctvConfigController.episodeUrl + episodeId,"ctvContentStack.processResponse", params);
    },
   
    loadClip : function (clipId) {    	
        var playList = new ctv.ca.PlayList(0, clipId, "Clip" + clipId, new Array());
        
        var xhr = new CTVFlashXMLHttpRequest();
        var params = new Array();
		params.push(playList);
		xhr.HttpCall("GET", ctvConfigController.clipUrl + clipId,"ctvContentStack.processResponse", params);
    },
    
    loadBin : function (binId) {
        var playList = new ctv.ca.PlayList(0, binId, "Bin" + binId, new Array());
        
        var xhr = new CTVFlashXMLHttpRequest();
        var params = new Array();
		params.push(playList);
		xhr.HttpCall("GET", ctvConfigController.binUrl + binId,"ctvContentStack.processResponse", params);  
    },
    
    loadInitShowData : function (showid, defaultBinId) {
        var playList = new ctv.ca.PlayList(0, showid, "Show" + showid, new Array());
        
        var url = ctvConfigController.showVideoUrl + "?aid=" + showid + "&dbid=" + defaultBinId;
		var xhr = new CTVFlashXMLHttpRequest();
        var params = new Array();
		params.push(playList);
		xhr.HttpCall("GET", url,"ctvContentStack.processResponse", params);     
    },

     
    playClip : function(clip) {
       this._raiseEvent("playVideo", clip);
       if (this.UIObj != null)
       {
            if (typeof this.UIObj.setSelectedClipItem != "undefined")
                this.UIObj.setSelectedClipItem(this.currentTabIndex, clip.playListIndex, clip.parentVideoIndex, clip.clipIndex);
       }
    },
      
    playVideo : function (playListIndex, videoIndex, clipIndex, tabStackIndex) {  
    	//alert(playListIndex + '^' + videoIndex + '^' + clipIndex)  	
		var clip = null;
		try {
			if (this.currentTabIndex != tabStackIndex)
			{
				//tabs changed, unselect the current item
				if (typeof this.UIObj.unSelectCurrentClipItem != "undefined")
					this.UIObj.unSelectCurrentClipItem(this.currentTabIndex);				
			}
			this.currentTabIndex = tabStackIndex;
			//debug("ctvContentStack.playVideo:\n" + "playListIndex: " + playListIndex + ", videoIndex: " + videoIndex + ", clipIndex: " + clipIndex);
            clip = this.PlayListArr[tabStackIndex].videos[videoIndex].clips[clipIndex];
			this.currentPlayingList = this.PlayListArr[tabStackIndex];
            this.currentVideo = this.PlayListArr[tabStackIndex].videos[videoIndex];
            this.currentClip = clip;
			
			this.nextPlayingList = this.currentPlayingList;
			this.nextVideo = this.currentVideo;
            this.nextClip = this.currentClip;				
			
            this.playClip(clip);
            this.adjustNextClip(); 
		} catch (e) {}
    },
    
    playNextVideo : function() {
        this.currentPlayingList = this.nextPlayingList;
        this.currentVideo = this.nextVideo;
        this.currentClip = this.nextClip;
        this.playClip(this.currentClip);
        
        this.adjustNextClip();       
    },
    
    adjustNextClip : function()
    {
        //advance nextclip
        if (this.currentClip.clipIndex == this.currentVideo.clips.length - 1) { //end of clip list
            if (this.currentVideo.videoIndex == this.currentPlayingList.videos.length -1)  { //end of video list
                if (this.currentPlayingList.index == this.PlayListArr.length - 1) { //end of play list
                    //loop to the first none empty playlist
					for (var i=0; i<this.PlayListArr.length; i++)
					{
						if (this.PlayListArr[i] != null && this.PlayListArr[i].videos !=null && this.PlayListArr[i].videos.length != 0)
						{
							this.nextPlayingList = this.PlayListArr[i];
							break;
						}
					}
                    
                    this.nextVideo = this.nextPlayingList.videos[0];
                    this.nextClip = this.nextVideo.clips[0];
                } else {
                    var foundNext = false;
					for (var i=this.currentPlayingList.index+1; i<this.PlayListArr.length; i++)
					{
						if (this.PlayListArr[i] != null && this.PlayListArr[i].videos !=null && this.PlayListArr[i].videos.length != 0)
						{
							foundNext = true;
							this.nextPlayingList = this.PlayListArr[i];
							break;
						}
					}
					
					//loop from firt playlist
					if (!foundNext)
					{
						for (var i=0; i<this.PlayListArr.length; i++)
						{
							if (this.PlayListArr[i] != null && this.PlayListArr[i].videos !=null && this.PlayListArr[i].videos.length != 0)
							{
								this.nextPlayingList = this.PlayListArr[i];
								break;
							}
						}					
					}					
                    this.nextVideo = this.nextPlayingList.videos[0];
                    this.nextClip = this.nextVideo.clips[0];
                }
            } else {
                this.nextVideo = this.currentPlayingList.videos[this.currentVideo.videoIndex+1];
                this.nextClip = this.nextVideo.clips[0];
            }
        } else {
            this.nextClip = this.currentVideo.clips[this.currentClip.clipIndex+1];
        }    
    },
        
    renderPlayList : function(playList, tabIndex) {    	
        if (this.UIObj != null)
        {      	      	
            this.UIObj.renderPlayList(playList, tabIndex);
        }

    },
    
    prev : function () {
    	alert("prev"); 	       
    },
    
    
    next : function () { 	       
    	alert("next");
    },
    
    getVideo : function(playListIndex, videoIndex) {
        return this.PlayListArr[playListIndex].videos[videoIndex];
    },
    
    getClip : function (playListIndex, videoIndex, clipIndex) {
        return this.PlayListArr[playListIndex].videos[videoIndex].clips[clipIndex];
    },
    
    onVideoRequest : function (sender) {
        this.playNextVideo();
    },
    
    setTabs : function (tabArr)
    {
        for (var i = 0; i < tabArr.length; i++) {            
            this.tabNamesArr.push(tabArr[i].tabName); 
            this.XmlUrlArr.push(tabArr[i].xmlUrl);           
            this.dataMsgArr.push(tabArr[i].dataMsg);
        }
        
    },
    
    setPlayList : function(tabIndex){    	    	 
    	this.renderPlayList(this.PlayListArr[tabIndex],tabIndex);       
    	       
    },
    
    onUIReady : function(sender) {
    	//debug("ContentStack init()");
    	 
        if (this.UIType == "swf")
        {        		
            this.UIObj = new ctv.ca.ContentStackUISwf(this, $get("swfStack"));
            this.UIObj.setTabs(this.tabNamesArr);
        }
        else if (this.UIType == "html")
        {
            this.UIObj = new ctv.ca.ContentStackUIHtml();
            ctvContentStack.add_playVideo(this.UIObj.onPlayVideo);
        }
        
        
       //if (typeof this.UIObj.renderPlayList != "undefined")
            //this.renderPlayList(this.PlayListArr[0],this.currentClip.playListIndex, this.currentClip.parentVideoIndex, this.currentClip.clipIndex);       
 
    }

}

ctv.ca.ContentStack.registerClass('ctv.ca.ContentStack', Sys.Component);

var ctvContentStack = new ctv.ca.ContentStack(ctvConfigController);





