﻿/**************************************************
 * tv.blog.js
 * 03.04.2006
 * www.TvBlogclub.com
 **************************************************/
 
var isIE = (navigator.userAgent.toLowerCase().indexOf("msie") >=0 );

/********** TV.BLOG ************************************************************************************************************************************/
var TvBlog = {
    id: 0,
    preload: '',
    playlist: '',
    filename: '',
    basepath: 'http://stat.tvblogclub.com/tv.blog/',
    skin: 'default',
    color: {},
    colors: '',
    file: '',
    width: '',
    height: '',
    status: 'minimize',
    filetype: '',
    save: new Array,
    debug: 0,

    trace: function(id, msg) { //debug
    	var debugField = document.getElementById('debug_'+id);
     	debugField.style.display = "";
    	debugField.innerHTML = msg;
    },

    createPlayer: function() {

		// SET COLORS
        var val;
        var colors = '&colors=';
        for (val in TvBlog.color) {
            colors += val+':'+this.color[val]+';';
        }

        // SET PATH
        var paths;
        if (this.playlist != '') paths = this.playlist; 
        if (this.file != '') paths = this.file; 

        // SET AUTOPLAYS (autoplay is executed only if at least one of this parameters is send : trackmod, title, tracknum) 
        var val;
        var autoplays = '';
        for (val in this.autoplay) {
            if(this.autoplay[val] != '') autoplays += '&'+val+'='+this.autoplay[val]+'';
        }

        // SET SIZE
        var controlHeight = '45px';
        var controlWidth = '100%';
        var playerHeight = parseInt(TvBlog.height) - parseInt(controlHeight)+'px';
        var playerWidth = '100%';

        if (this.status == 'maximize') {
            playerHeight = parseInt(TvBlog.playerHeight)+'px';
            playerWidth = '100%';
            controlHeight = parseInt(TvBlog.height) - parseInt(TvBlog.playerHeight)+'px';
            controlWidth = '100%';
        }

        if (this.status == 'megamize') {
            playerHeight = '100%';
            playerWidth = '69%';
            controlHeight = '100%';
            controlWidth = '30%';
        }

        // CREATE TV.BLOG
        if(this.ads == 'expand') { var content = '<div id="tvblog_playerdiv_'+this.id+'" style="display:none; background:'+TvBlog.color.body+'; border: solid 1px '+TvBlog.color.border+'; -moz-box-sizing: border-box; width:'+playerWidth+'; height:'+playerHeight+'; float:left;">'; }  
        else { var content = '<div id="tvblog_playerdiv_'+this.id+'" style="background:'+TvBlog.color.body+'; border: solid 1px '+TvBlog.color.border+'; -moz-box-sizing: border-box; width:'+playerWidth+'; height:'+playerHeight+'; float:left;">'; }
        content += this.preload;
        content += '</div>';
        if(this.status != 'megamize') content += '<div style="clear: both; line-height: 0; height: 0; margin: 0; font-size: 0; height:5px;">&nbsp;</div>';

        var flashVars = 'id='+this.id+'&filename='+paths+'&status='+this.status+colors;
        var controlsPath = this.basepath+'/skins/'+this.skin+'/controls.swf';

        content += '<div id="tvblog_controlsdiv_'+this.id+'" style="width:'+controlWidth+'; height:'+controlHeight+'; float:right;">';
        content += '<object id="tvblog_controls_'+this.id+'" '
        if (!isIE) content += 'type="application/x-shockwave-flash" data="'+controlsPath+'" ';
        if (isIE) content += 'classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" ';
        content += 'width="100%" height="100%" >' 
        +'<param name="movie" value="'+controlsPath+'">' 
        +'<param name="allowScriptAccess" value="always">' 
        +'<param name="flashvars" value="'+flashVars+'">' 
        +'</object>'; 
        content += '</div>';
                
        if (this.debug == '1') alert(content);
        return content;
    },
    
    start: function(where) {
        TvBlog.id++;
        if (typeof where == 'undefined') {
            document.write('<div id="tvblog_'+this.id+'" style="width:'+this.width+'; height:'+this.height+';"></div>');
            where = document.getElementById('tvblog_'+this.id);
        }

        where.innerHTML =  this.createPlayer();
    },

    resize: function(id,size) {
         var TvBlogControls = document.getElementById('controls_'+id);
         TvBlogControls.style.height = size;
         TvBlog.status = ('maximize' ? 'minimize' : 'maximize');
    },

    info: function(id,info) {
        var rbObj = document.getElementById('tvblog_'+id);
        if (info == 'null') rbObj.title = '';
        else rbObj.title = info;
    },
 
    gototvblogclub: function() {
        top.location = 'http://www.tvblogclub.com/';
    },

    isPlaying: function(title) {
    }

}

/********** PLAYER CONTROLS ****************************************************************************************************************************/
TvBlog.controls = {

	debug: function(text) {
        var obj = document.getElementById('debug');
        obj.value += text;
	},
		
    getPlayer: function(id) {
        var obj = document.getElementById('tvblog_movie_'+id);
        return obj;
    },

    getControler: function(id) {
        var obj = document.getElementById('tvblog_controls_'+id);
        return obj;
    },
    
    create: function(id, filename, filetype) {
        filename = unescape(filename);
        this.filetype = filetype;

        // CREATE PLUGIN
        this.plugin(id,filename);
		
        return true;
    },

    plugin: function(id, filename) {

        if (this.filetype == 'flv') {
            if (TvBlog.detect.plugin('application/x-shockwave-flash','ShockwaveFlash.ShockwaveFlash')) {
                var content = '<object id="tvblog_movie_'+id+'" '
                if (!isIE) content += 'type="application/x-shockwave-flash" data="'+TvBlog.basepath+'/skins/'+TvBlog.skin+'/player.swf" ';
                if (isIE) content += 'classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" ';
                content += 'bgcolor="000000"  width="100%" height="100%" >' 
                +'<param name="movie" value="'+TvBlog.basepath+'/skins/'+TvBlog.skin+'/player.swf">' 
                +'<param name="allowScriptAccess" value="always">' 
                +'<param name="flashvars" value="id='+id+'&autoplay=1&filename='+filename+'">' 
                +'</object>'; 
            }
        }
    
        if (this.filetype == 'wmv') {
            if (TvBlog.detect.plugin('application/x-oleobject','MediaPlayer.MediaPlayer.1')) {
                var content = '<object id="tvblog_movie_'+id+'" '
                if (!isIE) content += 'type="application/x-oleobject" ';
                content += 'classid="clsid:6BF52A52-394A-11d3-B153-00C04F79FAA6" ';
                content += 'bgcolor="000000"  width="100%" height="100%" >' 
                +'<param name="URL" value="'+filename+'">' 
                +'<param name="uiMode" value="none">' 
                +'<param name="stretchToFit" value="true">' 
                +'</object>'; 
            }
            else  {
                 var content = 'Firefox version 1.5 users need to install the following ActiveX compatibility plug-in:<br>'
                +'<a href="javascript:TvBlog.install.activeX();">Install Now</a><br>'
                +'To begin the installation, you may need to add tvblogclub.com to your list of trusted sites. Installing the plugin will require you to restart your browser.';
            }
        }    

        if (this.filetype == 'mov') {
            if (TvBlog.detect.plugin('video/quicktime','QuickTimeCheckObject.QuickTimeCheck.1')) {
                 var content = '<embed id="tvblog_movie_'+id+'" type="video/x-quicktime" width="100%" height="100%" '
               	+' id="tvblog_movie_'+id+'" src="'+filename+'" '
                +' autoplay="true" controller="false" '
                +' scale="aspect" bgcolor="#000000" '
                +' enablejavascript="true" ' 
                +' />';
            }
        }

        if (this.filetype == 'divx') {
            if (TvBlog.detect.plugin('video/divx','npdivx.DivXBrowserPlugin.1')) {
                 var content = '<embed id="tvblog_movie_'+id+'" type="video/divx" src="'+filename+'" '
                +'width="100%" height="100%" '
                +'statusCallback="TvBlog.divx.DivXstatusCallback" timeCallback="TvBlog.divx.DivXtimeCallback" downloadCallback="TvBlog.divx.DivXdownloadCallback" '
                +'mode="null" loop="false" autoPlay="true" '
                +'pluginspage="http://go.divx.com/plugin/download/">'
                +'</embed>';
            }
        }

        if (this.filetype == 'html') {
                 var content = '<iframe id="tvblog_movie_'+id+'" src="'+filename+'" '
                +'width=300 height=415 marginwidth=0 marginheight=0 vspace=0 hspace=0 scrolling=no frameborder=0 allowtransparency=true>'
                +'</iframe>';
        }

        if (content) {
            var target = document.getElementById('tvblog_playerdiv_'+id);
            if (TvBlog.debug == '1') this.debug(content);
            target.innerHTML = content;
        }
    },

    isPlugin: function(id) {
        var obj = this.getPlayer(id);
        if(!obj) return false;
        if(this.filetype == 'wmv') if(obj.settings) return true;
        if(this.filetype == 'mov') if(obj.GetPluginStatus != undefined) return true;
        if(this.filetype == 'flv') if(obj.setVolumeFlv) return true;
        if(this.filetype == 'divx') if(obj.getVolume) return true;
        return false;
    },

    isPlaying: function(id) {
        if(TvBlog.ads == 'expand') { 
            document.getElementById('adshome').style.display = 'none';
            document.getElementById('tvblog_playerdiv_'+id).style.display = 'block';
        }
        var obj = this.getPlayer(id);
        if(!obj) return false;
        if(!this.isPlugin(id)) {  return false; }
        if(this.filetype == 'mov') if (obj.GetPluginStatus() != 'Playable') return true;
        if(this.filetype == 'wmv') if (obj.playState == 3) return true;   //PLAYING
        if(this.filetype == 'flv') if (obj.getPlayingFlv()) return true;
        if(this.filetype == 'divx') if (TvBlog.divx.status == 10 || TvBlog.divx.status == 16 || TvBlog.divx.status == 19)  return true; //Playing, Playable, Complete
        return false;
    },

    play: function(id) {
		var obj = this.getPlayer(id);
        if(!obj) return;
        if(this.filetype == 'mov') { obj.Play(); }
        if(this.filetype == 'wmv') obj.controls.play();
        if(this.filetype == 'flv') obj.playFlv();
        if(this.filetype == 'divx') obj.Play();
    },
    
    pause: function(id) {
		var obj = this.getPlayer(id);
        if(!obj) return;
        if(this.filetype == 'mov') obj.Stop();
        if(this.filetype == 'wmv') obj.controls.pause();
        if(this.filetype == 'flv') obj.pauseFlv();
        if(this.filetype == 'divx') obj.Pause();
    },

    stop: function(id) {
		var obj = this.getPlayer(id);
        if(!obj) return;
        if(this.filetype == 'mov') obj.Stop();
        if(this.filetype == 'wmv') obj.close();
        if(this.filetype == 'flv') obj.stopFlv();
        if(this.filetype == 'divx') obj.Stop();
    },
    
    getSound: function(id) {
		var obj = this.getPlayer(id);
        if(!obj) return;
        if(this.filetype == 'mov') { var movieVolume = obj.GetVolume(); movieVolume = (movieVolume/256)*100; }
        if(this.filetype == 'wmv')  var movieVolume = obj.settings.volume;
        if(this.filetype == 'flv') var movieVolume = obj.getVolumeFlv();
        if(this.filetype == 'divx') var movieVolume = obj.getVolume();
        return movieVolume;
    },
    
    setSound: function(id,val) {
		var obj = this.getPlayer(id);
        if(!obj) return;
        if(this.filetype == 'mov') { val = (val/100)*256; obj.SetVolume(val); }
        if(this.filetype == 'wmv') { obj.settings.volume = val; }
        if(this.filetype == 'flv') obj.setVolumeFlv(val);
        if(this.filetype == 'divx') obj.SetVolume(val);
    },

    getPosition: function(id) {
		var obj = this.getPlayer(id);
        if(!obj) return;
        if(this.filetype == 'mov')  var movieNow = obj.GetTime()/obj.GetTimeScale()*1000;
        if(this.filetype == 'wmv') var movieNow = obj.controls.currentPosition*1000;
        if(this.filetype == 'flv') var movieNow = obj.getPositionFlv()*1000;
        if(this.filetype == 'divx') var movieNow = TvBlog.divx.time*1000;
        return movieNow;
    },
    
    setPosition: function(id,val) {
		var obj = this.getPlayer(id);
        if(!obj) return;
        if (this.filetype == 'mov')  obj.SetTime(val/1000*obj.GetTimeScale());
        if (this.filetype == 'wmv') obj.controls.currentPosition = val/1000;
        if (this.filetype == 'flv') obj.setPositionFlv(val);
        if(this.filetype == 'divx') obj.Seek(Math.round(val*100/(obj.GetTotalTime()*1000)));
    },
    
    getDuration: function(id) {
		var obj = this.getPlayer(id);
        if(!obj) return;
        if (this.filetype == 'mov')  var movieLenght = parseInt(obj.GetDuration()/obj.GetTimeScale()*1000);
        if (this.filetype == 'wmv') var movieLenght = obj.currentMedia.duration*1000;
        if (this.filetype == 'flv') var movieLenght = obj.getDurationFlv()*1000;
        if (this.filetype == 'divx') var movieLenght = obj.GetTotalTime()*1000;
        return movieLenght;
    },
    
    getLoaded: function(id) {
		var obj = this.getPlayer(id);
        if(!obj) return;
        if (this.filetype == 'mov')   var movieLoaded = obj.GetMaxBytesLoaded()/obj.GetMovieSize()*100;
        if (this.filetype == 'wmv')  var movieLoaded = obj.Network.downloadProgress;
        if (this.filetype == 'flv')  var movieLoaded = obj.getLoadedFlv();
        if (this.filetype == 'divx') var movieLoaded = TvBlog.divx.currentDownload / TvBlog.divx.totalDownload * 100;
        return movieLoaded;
    },
    
    isBuffering: function(id) {
        var obj = this.getPlayer(id);
    },
    
    status: function(id) {
		var obj = this.getPlayer(id);
        if(!obj) return;
        if (this.filetype == 'mov')  var movieStatus = obj.GetPluginStatus();
        if (this.filetype == 'wmv') var movieStatus = TvBlog.wmp.getStatus(obj.playState);
        if (this.filetype == 'flv') var movieStatus = obj.getStatusFlv();
        if (this.filetype == 'divx') var movieStatus = TvBlog.divx.getStatus();
        return movieStatus;
    },

    isStreaming: function(id) { 
		var obj = this.getPlayer(id);
        if(!obj) return;
        if (this.filetype == 'mov')  var isStream = !(obj.GetDuration() > 0); 
        if (this.filetype == 'wmv') var isStream = !(obj.currentMedia.duration > 0); 
        if (this.filetype == 'flv') var isStream = obj.isStreamingFlv(); 
        if (this.filetype == 'divx') var isStream = true; 
        return isStream; 
    }, 
 
    isEnded: function(id) { 
		var obj = this.getPlayer(id);
        if(!obj) return;
        if (this.filetype == 'mov')  var isEnd = (obj.GetRate() == 0 && obj.GetTime() == obj.GetDuration() && obj.GetTime() > 0);  
        if (this.filetype == 'wmv') var isEnd = (TvBlog.wmp.getStatus(obj.playState) == "MediaEnded"); 
        if (this.filetype == 'flv') var isEnd = (obj.isEndedFlv()); 
        if (this.filetype == 'divx') var isEnd = (TvBlog.divx.getStatus() == "VIDEO_END");
        return isEnd; 
    }

}

TvBlog.onPlaying = function (Title) {
}

TvBlog.onCreateError = function () {
	var obj = this.getControler(id);
	obj.onCreateError();
}



/**********  WMP CALLBACKS *****************************************************************************************************************************/
TvBlog.wmp = {
	getStatus: function (state) {
		switch (state) {
		   case 0 : return 'undefined';
		   case 1 : return 'Stopped';
		   case 2 : return 'Paused';
		   case 3 : return 'Playing';
		   case 4 : return 'ScanForward';
		   case 5 : return 'ScanReverse';
		   case 6 : return 'Buffering';
		   case 7 : return 'Waiting';
		   case 8 : return 'MediaEnded';
		   case 9 : return 'Transitioning';
		   case 10 : return 'Ready';
		   case 11 : return 'Reconnecting';
		   default  : return 'unknow';
		}
	},

	onStatusChange: function(state) { //IE only :'(
		TvBlog.trace(TvBlog.id, "Nouvel état : "+TvBlog.wmp.getStatus(state)+" [code:"+state+"]");
	}
}

/********** DIVX CALLBACKS *****************************************************************************************************************************/
TvBlog.divx = {
	status:'',
	time:0,
	currentBuffer:0,
	totalBuffer:0,
	currentDownload:0,
	totalDownload:0,

	getStatus: function () {
		switch (TvBlog.divx.status) {
		   case '0' : return 'INIT_DONE';
		   case '1' : return 'OPEN_DONE';
		   case '2' : return 'VIDEO_END';
		   case '3' : return 'SHUT_DONE';
		   case '4' : return 'EMBEDDED_START';
		   case '5' : return 'EMBEDDED_END';
		   case '6' : return 'WINDOWED_START';
		   case '7' : return 'WINDOWED_END';
		   case '8' : return 'FULLSCREEN_START';
		   case '9' : return 'FULLSCREEN_END';
		   case '10' : return 'Playing';
		   case '14' : case '11' : return 'Paused';
		   case '12' : return 'STATUS_FF';
		   case '13' : return 'STATUS_RW';
		   case '15' : return 'BUFFERING_START';
		   case '16' : return 'Playable';
		   case '17' : return 'DOWNLOAD_START';
		   case '18' : return 'DOWNLOAD_FAILED';
		   case '19' : return 'Complete';
		   default : return 'unknowState';
		}
	},
	
	DivXstatusCallback: function (divx_event) {
		TvBlog.divx.status = divx_event;
    },
    
	DivXtimeCallback: function (divx_time) {
		TvBlog.divx.time = divx_time;
  },
    
	DivXbufferCallback: function (divx_current, divx_total) {
		TvBlog.divx.currentBuffer = divx_current;
		TvBlog.divx.totalBuffer = divx_total;
  },
    
  DivXdownloadCallback: function (divx_current, divx_total) {
		TvBlog.divx.currentDownload = divx_current;
		TvBlog.divx.totalDownload = divx_total;
  } 
}

/********** DETECT BROWSER + PLUG-INS ******************************************************************************************************************/
TvBlog.detect = {

    browser: function() {
        var agt = navigator.userAgent.toLowerCase();
        var ie  = (agt.indexOf("msie") != -1);
        var ns  = (navigator.appName.indexOf("Netscape") != -1);
        var win = ((agt.indexOf("win")!=-1) || (agt.indexOf("32bit")!=-1));
        var mac = (agt.indexOf("mac")!=-1);

    },

	plugin: function(mimeType, ProgID) {
		// IE
		if (isIE && ProgID) {
            try {
    		  if (window.ActiveXObject)	{
    		    if(new ActiveXObject(ProgID))
    		    	return true;
    		  }
    		} catch(e) {}
        }

		// FF
        if (mimeType) {
        	if (navigator.mimeTypes[mimeType]) {
            	return true;
        	}
        }
	}
}

/********** INSTALL PLUG-INS ***************************************************************************************************************************/
TvBlog.install = {

    activeX: function() { 
        xpi = { 'Mozilla Firefox 1.5 ActiveX Plug-in':'http://stat.tvblogclub.com/plugins/mozactivex-ff-15.xpi' }
        InstallTrigger.install(xpi);
    }
}

/********** UTILS **************************************************************************************************************************************/
function addParam(obj, name, value) {
    var param = document.createElement('param');
    param.setAttribute('name',name);
    param.setAttribute('value',value);
    obj.appendChild(param);
}

var vitesse = 0.1;
var yPos = 0;
var yDest = 0;

function moveTo(parentId,id) {
	var obj  = document.getElementById(id);

	yPos = parseInt(document.getElementById(parentId).style.top);
	yDest = obj.offsetTop*-1;
	move(parentId);
}

function move(parentId)
{
	clearTimeout(moveID);
	yPos += (yDest -  yPos) * vitesse;
	document.getElementById(parentId).style.top = yPos+"px";

	if (yPos != yDest) {
		moveID = setTimeout("move("+parentId+")",delay);
	}

}