//The currently playing item (filename)
var currentItem='';

//Id of the player object
var playerID='playerFLV';

//Location of the swf of the player
var swf_url='http://dbms.telecomtv.com/js/flvplayer.swf';

//Current Video Id
var videoId     = -1;

//How often the database should be polled to get the slides (in seconds)
var poll        = 10;

//Meta data variables
var meta        = '';
var metaCount   = 0;

//The player object
var player      =  null;

//Current playhead time
var currentTime = -1;


//-----------------------------------------------------------

//Create the video player
function createPlayer(container, w, h, url, stream, absolute_url, auto, live, controlbar, volume) {

	//Clear the container that the player will be written to
	if (document.getElementById(container)) { document.getElementById(container).innerHTML=''; }

	//Set the current item to url
	currentItem = url;

	//Set the player's characteristics
	window[playerID] = new Object();
	var so = new SWFObject(swf_url,playerID,w,h,'8');
	so.addParam('wmode','transparent');
	so.addParam('allowscriptaccess','always');
  	so.addParam('allowfullscreen','true');	
	so.addVariable('javascriptid','playerFLV');
	so.addVariable('enablejs','true');
	so.addVariable('width',w);
	so.addVariable('height',h);
  	so.addVariable('backcolor','000000');	
  	so.addVariable('frontcolor','FFFFFF');
	so.addVariable('usefullscreen','true');
	so.addVariable('autostart', auto);
	so.addVariable('volume', volume);
	so.addVariable('type', 'video');

	if (!controlbar) { so.addVariable('controlbar', 'none'); }
	ext = '';

	//File part
	if (absolute_url == "") { 
		if (stream == "http") {
			absolute_url = 'http://video.telecomtv.com/web2/ugc/flv/';
			if (url.substr(-4) != ".flv") { url = url +'.flv'; }
		}	
		else if (stream == "h264") {
			absolute_url = 'http://video.telecomtv.com/h264/';
			ext = '.mp4';
		}
		else if (stream == "mydeo") {
			absolute_url = '/live-sites/www.telecomtv.com/low/';
			ext = '.flv';
		}
		else if (stream == "mydeoh264") {
			absolute_url = '/live-sites/www.telecomtv.com/high/';
			ext = '';
		}
	}

	//Streamer part
	if (stream == "live") {
		so.addVariable('streamer','rtmpt://mydeo.fc.llnwd.net/mydeo/sub13/');	
		so.addVariable('subscribe', 'true');
	}
	else if (stream == "http") {
		absolute_url = stream + '://'+ absolute_url;
	}
	else if (stream == "absolute") {
	}
	else {
		so.addVariable('streamer','rtmpt://mydeo.fcod.llnwd.net/a584/d1/');	
		absolute_url = 'decisive' + absolute_url;
	}
	//alert(absolute_url + url);
	
	so.addVariable('file', absolute_url + url);

	//Write the object to the container
	so.write(container);
	
	//Apply IE patch to the player
	SWFFormFix(playerID);
};

	//else if (stream == "limelightlive") {
		//so.addVariable('streamer','rtmpt://telecomtv.fc.llnwd.net/telecomtv/');	
		//so.addVariable('subscribe', 'true');
	//}
	//else if (stream == "limelight") {
		//so.addVariable('streamer','rtmpt://telecomtv.fcod.llnwd.net/a1411/o16/');	
	//}

//----------------------------EVENTS-------------------------------

    function playerReady(obj)
    {
      	player = gid(obj.id);
	
		//Add the event listeners
		addListeners();
    };

	
    function addListeners()  {
	
	//Initialise the playlist
      	playlist = player.getPlaylist();

      	if ((playlist !== null) && (playlist.length > 0)) {
			//player.addControllerListener('PLAY', 'showHolding');
        	//player.addControllerListener('STOP', 'showHolding');

            player.addModelListener("STATE", "stateMonitor");

            //Meta data event
			//player.addModelListener('META', 'metaMonitor');
			
			//Time event (playhead)
			player.addModelListener('TIME', 'timeMonitor');
      	}
      	else {
		//Try adding again; player not ready.
        	setTimeout("addListeners();", 100);
      	}
    };
    
    function gid(name)
    {
      return document.getElementById(name);
    };   
    
    function time() {
    	alert(currentTime);
    }
    
//----------------------EVENT HANDLERS---------------------
    
    //For a time event (playhead)
    function timeMonitor(obj)  {
      for(var j in obj)
      {
      	if (j == 'position') {
      	    //document.getElementById('timedata').innerHTML = obj[j];
      	    
      	    //The position of the playhead
      	    var time = parseFloat(obj[j]);
      	    currentTime = time;
		}
      }
    };

//--------------------------------------------------------------
	
	//Ajax call to the find the url of the next slide
	function lookupSlide(timeIndex) {
        if (isNumeric(timeIndex) && videoId != -1) {

            //Parameters for the call
            var p = 'TimeIndex='+ timeIndex +'&VideoId='+ videoId;

            var options = {
                    method : 'get',
                    parameters : p,
                    onComplete : function(request) {
                        //Load the image
                        showSlide(request.responseText);
                    },
                    onFailure:function(t) {
                        //Something went wrong
                    }
            };
            
            new Ajax.Request('/OnDemandLookup.ashx?action=dbLookup', options);   
        }
    }
    
	//Ajax call to the find the url of the next slide
	function setSlideStart(id, pbID) {
	    var setTime = currentTime * 1000;
	    var setTimeSS = currentTime;
            toggleStatus(id);
            if (player) { player.sendEvent("PLAY"); }

        if (currentTime > -1 && isNumeric(id)) {

            //Parameters for the call
            var p = 'TimeIndex='+ setTime +'&SlideId='+ id;

            var options = {
                    method : 'get',
                    parameters : p,
                    onComplete : function(request) {
                        //Load the image
                        //performPostBack(pbID);
                        __doPostBack(pbID,'');
                        toggleStatus(id);
			UpdateNewTime(id, setTimeSS);
                        player.sendEvent("PLAY","True");
                    },
                    onFailure:function(t) {
                        toggleStatus(id);
                    }
            };
            
            new Ajax.Request('/OnDemandLookup.ashx?action=SetSlideStart', options);   
        }
    } 
    
         function performPostBack(hiddenFieldID) {
            var hiddenField = $get(hiddenFieldID);
            if (hiddenField) {
                hiddenField.value = (new Date()).getTime();
                __doPostBack(hiddenFieldID,'');
            }
         } 
    
    function toggleStatus(id) {
        var sb = document.getElementById('SetStatus'+ id);
	    
	    if (sb) { 
	        if (sb.style.display != 'none') {
	            sb.style.display = 'none';
	        }
	        else {
	            sb.style.display = 'block';
	        }
	   }
    }

    function UpdateNewTime(id, value) {
        var sb = document.getElementById('NewSetTime'+ id);
	    
	    if (sb) { 
	        sb.innerHTML = '<b>' + value +'</b>';
	   }
    }

    //Fill the html container with the slide image
    function showSlide(url) {
        var sc = document.getElementById(htmlId);	
        
		if (sc && url != '') { 
		    //If the slides have changed
		    if (currentImg != url) {
		        //currentImage = new Image();
		        sc.innerHTML = '<img src="'+ url +'" height="428" width="590"/>';
		        currentImg = url;
		    }
		}
	}
	
	//Show a holding slide
	function showHolding(obj) {
      for(var j in obj) {
      	//if (j == 'newstate') {
      	    //alert(j +' '+ obj[j]);
      	    //}
      }
	}

//----------------------------------------------------------

//Function to check if a given value is numeric or not
function isNumeric(sText) {
   var ValidChars = "0123456789.";
   var IsNumber=true;
   var Char;
 
   for (i = 0; i < sText.length && IsNumber == true; i++) 
      { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         IsNumber = false;
         }
      }
   return IsNumber;
   
}


