/*
Script: Live.class.js
	Contains the Brainsonic Live class

Author:
	Brainsonic - http://www.brainsonic.com
*/

var Live = new Class({
	
		//behaviour parameters
		hasDynamicPanels: false,
		panelsRefreshFrequency: 30000,
		bIsSimulated: true,
		urlService: 'timeService.php',
		sessionId: '9bce3bbc-7c5b-48c9-9799-9a32ffeabffc',		
		statsStartUrl: 'http://customers.brainsonic.com/DesktopModules/BrainsonicLive/stat_start.aspx',
		statsEndUrl: 'http://customers.brainsonic.com/DesktopModules/BrainsonicLive/stat_end.aspx',
		streamUrl: '',		
		seekAtTime: 0,
			
		/*
			Function: initialize
			Parameters none
			Description: initialize the live user interface
		*/		
		initialize: function(param){
			this.togglers = $$('h3.toggler');
			this.iframeStats = $('framestat');
			this.stretchers = $$('div.accordion');
			this.iframeStatsClose = $('framestatclose');
			this.accordionContainer = $('accordionContainer');
			this.stretchers.each(function(item){
				item.setStyles({'height': '0', 'overflow': 'hidden'});
			});
			
			this.initializeAccordion();
			//this.initializeStats();
			
			if(this.hasDynamicPanels)
			{
				this.refreshPanels();
				this.refreshPanels.periodical(this.panelsRefreshFrequency, this);
			}
			this.playerContainer = $('playerContainer');
			
			//then call ajax if Simulate is true
			if (this.bIsSimulated)
			{		
				// the first save the time client at starting making request to call service ( to caculate elapsed time later)
				var time=(new Date()).getTime();				
				this.timeRequest=time;
				// then call ajax service
				new XHR({onSuccess: this.callbackFromServer.bind(this)}).send(this.urlService,'');				
			}
			// else then call usual media player embed code
			else 
			{
				/*this.playerContainer.setHTML(this.getPlayerCode(this.playerContainer.getStyle('width').toInt(),
								this.playerContainer.getStyle('height').toInt(), this.streamUrl, 0));*/
			}				
		},		
		
		/*
			Function: initializeAccordion
			Parameters: none
			Description: initialize the accordion part of the interface
		*/
		initializeAccordion: function(){
		
			myAccordion = new Fx.Accordion(this.togglers, this.stretchers, { 
			opacity: true, start: "first-open", fixedHeight: this.accordionContainer.getStyle('height').toInt(), 
			fixedWidth:this.accordionContainer.getStyle('width').toInt(),transition: Fx.Transitions.quadOut,			
			onActive: function(toggler, i){
				toggler.setStyle('background', 'url(images/tab_active.png) no-repeat');
				
				if(toggler.getNext().getFirst().getTag())
				{
					if(toggler.getNext().getFirst().getTag() != "iframe")
						toggler.getNext().setStyle('overflow', 'auto');
				}
			},
		
			onBackground: function(toggler, i){
				toggler.setStyle('background', 'url(images/tab_bg.png) no-repeat');
  			}
		});
			
			myAccordion.showThisHideOpen(0);
		},	
		
		/*
			Function: initializeAccordion
			Parameters: none
			Description: initialize the accordion part of the interface
		*/
		initializeStats: function(){
			this.iframeStats.setProperty('src', this.statsStartUrl+'?UID='+this.getUid()+'&SID='+this.sessionId);
		},
		
		getUid: function(){
			var today = new Date();
			var uid = today.getFullYear()+""+ today.getMonth()+""+ today.getDate()+ "" +today.getUTCHours()+ "" +today.getUTCMinutes()+ "" +today.getUTCSeconds() + "" +100000000000000000*Math.random();
			return uid;
		},
		
		
		/* 
			Function: refreshPanels 
		   	Parameters: none
			Description: Add dynamic panels availability
		   	Only run if the hasdynamicPanels property is set to true
		*/
		refreshPanels: function(){
			
				var i;
			
				try{
				
					if(window.ActiveXObject){
					
						i= new ActiveXObject("Microsoft.XMLDOM");
						i.async=false;
						i.load('files/xml/live.xml');
						this.displayPanelChanges(i);
					}
					else if(document.implementation && document.implementation.createDocument)
					{
						i = document.implementation.createDocument('', '', null);
						i.load('files/xml/live.xml');
						i.onload = this.displayPanelChanges(i);
					}
				}catch(ex){}				
		},
		
		displayPanelChanges: function(xmldocument){	
			
				var tabList = xmldocument.getElementsByTagName('tab');
				$each(tabList, function(element)
							{		
								
								var frame = $$('#accordionContainer div.accordion')[element.getAttribute('id')].getFirst();											 
								 if(frame.getTag() == "iframe")
								 {
									if(frame.getProperty('src') != element.getAttribute('url'))
									{
										frame.setProperty('src', element.getAttribute('url'));
													
										if(element.getAttribute('toggle') == "true")
											myAccordion.showThisHideOpen(element.getAttribute('id'));
									}						
								  }
					});
		},
		
		
		/* 
			Function: getPlayerCode 
		   	Parameters: width, height, url
			Description: Generates the html code to display the Windows media video player
		*/
		getPlayerCode: function(width, height, Url, currentPosition){
			
			playerCode ="";
			
			return playerCode;
		},

		getQueryVariable: function (variable) 
		{
			var query = window.location.search.substring(1);
			var vars = query.split("&");
				for (var i=0;i<vars.length;i++) {
					var pair = vars[i].split("=");
					if (pair[0] == variable) {
						return pair[1];
					}
				}
		},		

		/*
		 Function: callbackFromServer
		 Parameter: output value from before ajax call
		 Desc :to receive and process from result of Ajax call
		*/
		callbackFromServer: function(timeThreshold){
			
			if (timeThreshold >0 && timeThreshold != -1 )
			{
						var elapsedTime=(((new Date()).getTime()-this.timeRequest)/1000);
						this.seekAtTime = timeThreshold.toInt() + elapsedTime.toInt();
						createFlash(this.seekAtTime);
			}

		}
});
