﻿///<reference path="Jquery.IntelliSense.js" />
///<reference path="Main.js" />

var Players=[];
if(!View.PlayerSize) View.PlayerSize = {width:340,height:240};
Players.New = function (video,TVZone,size) {
	TVZone =2; if (!size) size = View.PlayerSize;
	var id = Players.length;
	var p = new Player(id, video,size);
	Players.push(p);
	p.TVZone = TVZone;
	p.fA = Player.prototype.fA;
	return p;
}


function Player(id,video,size)
{	
	this.video = video;
	this.size = size;
	var sImg = "",sHTML = [];
	this.id=id;
	this.AdIn=false;
	var sl=this.CheckSL();
	if (!$.browser.msie&&!sl)
		{this.Silverlight=true;sPlayer = "<div style=direction:rtl;position:absolute;text-align:right;background-color:black;height:"+(size.height-20)+"px;padding:10px;color:white;width:"+(size.width-40)+"px>עליך להתקין תוסף Silverlight לדפדפן שלך.<br><a href=http://go2.microsoft.com/fwlink/?LinkID=114576&amp;v=2.0 style=color:white>לחץ כאן כדי להתקין את Silverlight</a><br><br>לחילופין נסה <a style=color:white onmouseover=this.href=Players["+id+"].video.url href="+video.url+">לפתוח אותו בנגן חיצוני</a></div>"; this.Writed=DoNothing;}
	if (!$.browser.msie&&sl){ sPlayer = '<object type="application/x-silverlight-2" id="Player'+id+'" src=/static/images/objects/a7player.xap style=width:'+(size.width-20)+'px;height:'+size.height+'px"><param name="MinRuntimeVersion" value="2.0.31005.0"></param><param name=Source value=/static/images/objects/a7player.xap /></object>'; this.Silverlight=true}
	else if ($.browser.msie) {sPlayer =  '<object id=Player'+id+' classid=CLSID:6BF52A52-394A-11d3-B153-00C04F79FAA6  style=width:'+(size.width-20)+'px;height:'+(size.height+65)+'px"><PARAM NAME="stretchToFit" VALUE="true"><PARAM NAME="uimode" VALUE="full"><PARAM NAME="autoSize" VALUE="false"><PARAM NAME="autoStart" VALUE="true"></OBJECT>'}

	sImg = size.height>30? "<img id=imgVideo"+id+" style=position:Absolute;"+(!this.size.height?'visibility:hidden':'')+" src="+GetImage(video.image,(size.width-20),size.height)+">":"";
	sHTML.push('<div class=VVideo style=width:'+size.width+'px;><div  id=divVidTitle'+id+' '+(Players.DisableTitle?'style=height:8px;font-size:1px;color:Black':"")+' class=VVideoT></div><div id=divVideoBG'+id+' style="margin:0 10px;width:'+(size.width-20)+'px;height:'+size.height+'px;direction:ltr;overflow:hidden;cursor:pointer;" onclick=Players['+id+'].Play()>'+sImg+'<img class=VidLargePlay id=VidLargePlay'+id+' style=position:absolute;top:'+(size.height-74)/2+'px;left:'+(size.width-84)/2+'px;'+(!this.size.height?'visibility:hidden':'')+' src='+sStaticURL+'images/objects/play.png>'+sPlayer+'<div class=VideoLoadingOut style=top:'+((size.height/2)+60)+'px dir=rtl> <span style=display:none id=divLoading'+id+'>טוען...</span></div></div><div onmouseup=Players['+id+'].ChangePos(($.getMouseCoordsWithinEvent(arguments[0]).x/this.offsetWidth)*100) class=VPos><div id=VideoPosition'+id+'></div></div>');
	sHTML.push('<div class=VLine style="background-position:'+(size.width/2-20)+'px -45px">\
	<div onclick=Players['+id+'].ChangeVolume(($.getMouseCoordsWithinEvent(arguments[0]).x/this.offsetWidth)*100) onmousedown=this.move=1;t=this;this.onclick.call();u=arguments[0]; onmouseup=this.move=0;clearTimeout(window.volto); onmousemove=if(this.move)Players['+id+'].ChangeVolume(($.getMouseCoordsWithinEvent(arguments[0]).x/this.offsetWidth)*100)  onmousedown=this.move=1 onmouseout=setTimeout("this.move=0;",100); class=VVol><div class=Line></div><span id=spnVolume'+id+'></span>&nbsp;</div>\
	<span class="VMute" id=VMute'+id+' onclick="Players['+id+'].Mute()"></span>\
	<span class="VButton VPlay" id=VPlay'+id+' onclick="Players['+id+'].VPlayPause();"><span></span></span><span class="VButton VStop" onclick="Players['+id+'].Command(\'Stop\');"><span></span></span><span'+(size.height<30?' style=visibility:hidden':'')+' class="VButton VFS" onclick="Players['+id+'].FullScreen()"><span></span></span>\
	</div> <div class=VText style="background-position:'+(size.width/2-140)+'px -82px" ><span class=VMarker id=VMarker'+id+'></span><div id=divVidInf'+id+'></div></div> </div>');
	if(size.width>380) if (Info.View.aClients.length==0) {Info.Add(211,"divVidInf"+id); setTimeout("Players["+id+"].UpdateInf()",25000);} else this.UpdateInf(1);
	this.html = sHTML.string();
}

Player.prototype = {Volume: 100,Silverlight:false,lastState:-1,IsWrited:false,
CheckSL: function(version) {
    try 
    {
        var control = null;
        try
        {
            control = new ActiveXObject('AgControl.AgControl');
            if ( version == null ) return true;
            control = null;
        }
        catch (e)
        {
            if (navigator.plugins["Silverlight Plug-In"]) return true;
        }
    }
    catch (e) 
    {
        return false;
    }
	},
Command: function(command) {var p= _("Player"+this.id); if(this.Silverlight) eval("p.Content.p."+command+'()'); else eval("p.controls."+command+"()");},
Play: function() {if (!this.UrlSetted) this.SetURL(this.ActiveUrl); this.Command("Play"); this.LogC()},Stop: function() {this.Command("Stop");},
LogC: function(){var t=this; if(typeof(this.video.type)=='number') setTimeout(function(){Info.LogC(t.video.type,0,t.video.id,(t.place?t.place:0)) },2000); },
Mute: function() {this.IsMute=!this.IsMute; var p= _("Player"+this.id); if(this.Silverlight) p.Content.p.Mute(this.IsMute); else  p.settings.mute=p.settings.mute==true?false:true; _("VMute"+this.id).style.backgroundPosition='0 -'+(this.IsMute?17:30)+'px'},
VPlayPause: function() {if (this.playState==3) this.Command("Pause"); else  this.Play(); if (this.AdIn) ExecuteUrl(this.a.logurl);},
UpdateMP: function()
	{ 
	var MP = _('Player'+this.id),x,iPos,sPos,playState;
	if (this.Silverlight) {var o=MP.Content.p;playState=o.PlayState();sPos=o.PositionString().split(",");iPos=o.PositionInt();
	sPos=View.AddZero(sPos[0])+":"+View.AddZero(sPos[1])+"/"+View.AddZero(sPos[2])+":"+View.AddZero(sPos[3])}
	else {sPos=MP.controls.currentPositionString+(MP.currentMedia?" / "+ MP.currentMedia.durationString:"");playState=MP.playState;iPos =(MP.currentMedia? Math.floor((MP.controls.currentPosition/(MP.currentMedia.duration>0?MP.currentMedia.duration:0))*100+0.999999999):0);}
	if(playState==2||playState==3&&iPos != Infinity)
		{
		    if (!this.Silverlight && !isNaN(iPos) && iPos != Infinity && MP.currentMedia.duration || iPos > 0) { _("VideoPosition" + this.id).style.width = (iPos/100) * (this.size.width - 20) + "px" };
		};
	this.playState=playState;
	$("#VPlay"+this.id+" span").css("backgroundPosition","-"+(playState==3?120:13)+"px -20px")
	_("VMarker"+this.id).innerHTML = sPos;
	},
ChangePos: function(value) {if (this.AdIn) return false; MP =_("Player"+this.id); if(!this.Silverlight) MP.controls.currentPosition=MP.currentMedia.duration*(value/100); else MP.Content.p.SetPosition(value);  },
ChangeVolume: function(value) {if (value<10||!value) return;value+=5; if (value>100)value=100;
	this.Volume=value; if(this.Silverlight)  _('Player'+this.id).Content.p.SetVolume(value); else  _('Player'+this.id).settings.volume=this.Volume; _('spnVolume'+this.id).style.left=((value-6)/2)+"px";},
FullScreen: function() {var MP =_("Player"+this.id);if (MP.playState== 3||MP.playState==2||MP.playState == 6) MP.fullScreen = true;},
IsLoad: function()
	{	var p=_('Player'+this.id),load = $('#divLoading'+this.id), other=$( (!this.audio?"#imgVideo"+this.id:"")+',#VidLargePlay'+this.id);
		if(this.playState>1&&this.playState<5) {load.hide();other.hide(); p.style.visibility= this.size.height&&!this.audio ?'':'hidden';}
		else {if((this.Silverlight&&this.playState!=5 || !this.Silverlight&&this.playState!=1&&this.playState!=8&&this.playState!=6&&this.playState!=10)&&this.playState)load.show(); else load.hide(); other.show(); p.style.visibility='hidden';}
		p.style.height=this.size.height+(this.Silverlight?0:65)+"px";p.style.width=(this.size.width-20)+"px";},
Writed: function() {
	this.IsSLLoad = function(){var t=this; if (this.IsWrited===true) return false; try {o =_('Player'+t.id).Content.p.SetVolume(1); this.Writed2()} catch(e){setTimeout("Players["+this.id+"].IsSLLoad()",1000);} }
	if (this.Silverlight)  this.IsSLLoad(); else this.Writed2() },
Writed2: function() { this.ChangeVolume(this.Volume); this.SetVideo(this.video,1); this.IsLoad();
	this.IsWrited=true;
	
	if (this.Silverlight) {}
	else if (this.TVZone>-1&&this.size.height>10) ('<script for="Player'+this.id+'" event="Click">Players['+this.id+'].Clicked();</script>').write();
	 },
Changed: function() {
	p = _("Player" + this.id);
	if (this.Silverlight) { }
	else {if (p.lastDur!=p.currentMedia.durationString) { this.AdUrl="";
		for (var i = 0; i < p.currentMedia.attributeCount; i++) {
			var s = p.currentMedia.getAttributeName(i),v=p.currentMedia.getItemInfo(s)
			if (s == "AdLog") ExecuteUrl(v+"&_"+new Date().getTime());
            else if (s == "AdUrl") this.AdUrl=v;
            }
		p.lastDur=p.currentMedia.durationString;
        }}
	},
Clicked: function (url) {if(!url)url=this.AdUrl;if(!url) return false; var a=window.open(url,"icvtAD"); if(!a) location.href=url;},
SetURL: function(url,notplay) {this.ActiveUrl=url; if(notplay) return false; this.LogC(); if(this.TVZone!=-1&&this.size.height>10) url="http://www.inn.co.il/TV/Playlist.ashx?Zones=190,191&File="+url; if(this.Silverlight) { _('Player'+this.id).Content.p.SetUrl(url);} else  _('Player'+this.id).URL=url;  this.UrlSetted=true},
	SetVideo: function (video,notplay) {this.UrlSetted=false; if (video.url.indexOf(":")==-1) video.url = "mms://media.a7.org/"+video.url.replaceStr("//","/"); this.video=video; _("divVidTitle"+this.id).innerHTML = video.title;if(this.size.height>30) _("imgVideo"+this.id).src=GetImage(video.image,(this.size.width-20),this.size.height); this.SetURL(this.video.url,notplay);},
	Dispose: function() {Players.remove(this.id); },
	UpdateInf: function(preload){setTimeout("Players["+this.id+"].UpdateInf()",15000); if(preload||this.playState==3) Info.LoadOne(211,"divVidInf"+this.id); }
}
function CheckPState()
{
	var played=false;
	for (var i=0;i<Players.length;i++)
	  if (Players[i]&&Players[i].IsWrited) {
	    var player = _("Player"+i), p = Players[i], playState;
	    p.UpdateMP()
	    if(p.Silverlight) {}
		
		if (p.playState==3) {p.Changed()}
		if(p.playState>3&&p.bIsChanged) p.bIsChanged=false;
		if(p.lastState*1 != p.playState*1) p.lastState = p.playState;
		p.IsLoad()
		
		if (p.playState>1&&p.playState<8) played=true;
	}
	setTimeout(CheckPState,played?200:1000);
}

$(document).ready(function () {$('.VVideo').disableSelection();setTimeout(CheckPState,1500);})