﻿///<reference path="~/static/scripts/Jquery.IntelliSense.js" />
///<reference path="~/static/Main.js" />

function isArray(obj) {
    return obj.constructor == Array;
}

var Players=[];
if(!sStaticURL){sStaticURL='/';}
if(!View.PlayerSize) View.PlayerSize = {width:319,height:239};
Players.New = function(video, TVZone, size, uimode) {
    if (!TVZone) { TVZone = [-2, -2] /*230*/; } else if (!isArray(TVZone)) { TVZone = [TVZone, TVZone]; } if (!size) size = View.PlayerSize;
    var id = Players.length;
    var p = new Player(id, video, size, false, uimode);
    Players.push(p);
    p.TVZone = TVZone;
    p.fA = Player.prototype.fA;
    return p;
}


function Player(id,video,size,DontWriteCSS,mp_uimode)
{
	this.video = video;
	this.size = size;
	this.id=id;
	this.AdIn=false;
	var sPlayer = '<div style=color:white;>לא ניתן לנגן את הוידאו. נסה <a style=color:white href='+video.url+'>לפתוח אותו בנגן חיצוני</a>';
	var sl=this.CheckSL();
	var uimode='full';
	if(!!mp_uimode)
	{
	uimode=mp_uimode;
	}
	if (!$.browser.msie&&!sl)
		{this.Silverlight=true; this.html = "<div style=direction:ltr;background-color:black;height:"+(size.height-20)+"px;padding:10px;color:white;width:"+(size.width-40)+"px>You need the Silverlight plugin for Firefox.<br><a href=\"http://go.microsoft.com/fwlink/?LinkID=124807\" style=color:white>Click here to install it</a></div>"; this.Writed=DoNothing; return false;}
		else if (!$.browser.msie && sl)
		//   else if (sl)
		{ sPlayer = '<object type="application/x-silverlight-2" id="Player' + id + '" src="/static/images/objects/a7player.xap?v=2" 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 /><a href="http://go2.microsoft.com/fwlink/?LinkID=114576&amp;v=2.0"><img src="http://go2.microsoft.com/fwlink/?LinkID=108181" alt="Get Microsoft Silverlight" style="border-width:0;" /></a></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="'+uimode+'"><PARAM NAME="autoSize" VALUE="false"><PARAM NAME="autoStart" VALUE="false"></OBJECT><script for="Player' + id + '" event="playStateChange">Players['+id+ '].Changed();</script>'; }
	
	this.html = this.GraphIt(sPlayer);
	if(DontWriteCSS){this.WriteCSS=false;}
}
function GetImage(imageid,width,height)
{
if(imageid==0){return '/static/images/objects/emptyad.gif'}
return "/static/pictures/resized/319-239/"+Math.floor(imageid/1000)+"/"+imageid+".jpg";
}
Player.prototype = { Volume: 100, Silverlight: false, lastState: -1, IsWrited: false, IsSLLoaded: false, currImageClickUrl: "", WriteCSS: true,
    GraphIt: function(playerobj) {

        var sImg = "<img id=imgVideo" + this.id + " style=position:Absolute; src='" + GetImage(this.video.image, (this.size.width - 20), this.size.height) + "' />";
        var sHTML = [];
        sHTML.push('<div class=VVideo style=width:' + this.size.width + 'px;><div class=VVideoT></div><div id=divVideoBG' + this.id + ' style="margin:0 10px;width:' + (this.size.width - 20) + 'px;height:' + this.size.height + 'px;direction:ltr;overflow:hidden;cursor:pointer;" onclick=Players[' + this.id + '].Play()>' + sImg + '<img class=VidLargePlay id=VidLargePlay' + this.id + ' style=position:absolute;top:' + (this.size.height - 74) / 2 + 'px;left:' + (this.size.width - 84) / 2 + 'px src=' + sStaticURL + 'images/objects/play.png>' + playerobj + '<div class=VideoLoadingOut style=top:' + ((this.size.height / 2) + 60) + 'px dir=rtl> <span style=display:none id=divLoading' + this.id + '>Loading...</span></div></div><div onmouseup=Players[' + this.id + '].ChangePos(($.getMouseCoordsWithinEvent(arguments[0]).x/this.offsetWidth)*100) class=VPos><div id=VideoPosition' + this.id + '></div></div>');
        sHTML.push('<div class=VLine style="background-position:' + (this.size.width / 2 - 20) + 'px -79px">\
	<div onclick=Players[' + this.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[' + this.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' + this.id + '></span>&nbsp;</div>\
	<span class="VMute" id="VMute' + this.id + '" onclick="Players[' + this.id + '].Mute()"></span>\
	<span class="VButton VPlay" id=VPlay' + this.id + ' onclick="Players[' + this.id + '].VPlayPause();"><span></span></span><span class="VButton VStop" onclick="Players[' + this.id + '].Command(\'Stop\');"><span></span></span><span class="VButton VFS" onclick="Players[' + this.id + '].FullScreen()"><span></span></span>\
	</div> <div class=VText ><span class=VMarker id=VMarker' + this.id + '></span><div id=divVidTitle' + this.id + '>' + this.video.title + '</div><img src="/static/images/objects/emptyad.gif" class="playerAdImage" id="playerAdImage' + this.id + '" onclick="Players[' + this.id + '].ImageClicked();" src=""/></div> </div>')
        return sHTML.string();
    }, ShowVolume: function(value) {
        _('spnVolume' + this.id).style.left = ((value - 6) / 2) + "px";
    }, ShowMute: function(IsMute) {
        _("VMute" + this.id).style.backgroundPosition = '0 -' + (IsMute ? 10 : 23) + 'px';
    }, ShowPauseButton: function(state) {
        $("#VPlay" + this.id + " span").css("backgroundPosition", "-" + (state ? 110 : 2) + "px -43px");
    }, ShowPosition: function(posString) {
        _("VMarker" + this.id).innerHTML = posString;
    }, ShowTitle: function(text) {
        _("divVidTitle" + this.id).innerHTML = text;
    }, ShowWaitingImage: function(src) {
        _("imgVideo" + this.id).src = src;
    }, ShowPositionBar: function(value) {
      try{
        _("VideoPosition" + this.id).style.width = (value / 100) * (this.size.width - 20) + "px";
        }
        catch(err){
            _("VideoPosition" + this.id).style.width = (1 / 100) * (this.size.width - 20) + "px";
        }
    }, 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"); }, Stop: function() { this.Command("Stop"); },
    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; }
        this.ShowMute(this.IsMute);
    },
    VPlayPause: function() { if (this.playState == 3) this.Command("Pause"); else this.Play(); if (this.AdIn) ExectueUrl(this.a.logurl); },
    UpdateMP: function() {
        var MP = _('Player' + this.id), x, iPos, sPos, playState;
        if (this.Silverlight) {
            if (!this.IsSLLoaded) { return; }
            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.currentMedia ? MP.currentMedia.durationString + " / " : "") + MP.controls.currentPositionString; playState = MP.playState; iPos = (MP.currentMedia ? Math.floor((MP.controls.currentPosition / MP.currentMedia.duration) * 100 + 0.999999999) + 1 : 0); }
        if (playState == 2 || playState == 3) {
            if (!this.Silverlight && !isNaN(iPos) && iPos != Infinity && MP.currentMedia.duration || iPos > 0) { this.ShowPositionBar(iPos); };
        };
        this.playState = playState;
        this.ShowPauseButton(playState == 3);
        this.ShowPosition(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; this.ShowVolume(value);
    },
    UpdateImageClickUrl: function(url) {
        this.currImageClickUrl = url;
    }, ImageClicked: function() {
        if (this.currImageClickUrl != '')
        { this.Clicked(this.currImageClickUrl); }
    },
    UpdateImageAd: function(imagename) {
        var AdImage = _("playerAdImage" + this.id), p = _('Player' + this.id);
        if (AdImage != null) {
            AdImage.src = 'http://194.90.181.246/banners-en-new/' + imagename;
        }
    },
    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 = $("#imgVideo" + this.id + ', #VidLargePlay' + this.id);
        if ((this.playState > 1 || (!this.Silverlight && this.playState == 0)) && this.playState < 5) { load.hide(); p.style.visibility = this.size.height ? '' : 'hidden'; }
        else { if (this.Silverlight && this.playState != 5 && this.playState != 0 || !this.Silverlight && this.playState != 1 && this.playState != 8 && this.playState != 10) 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";
    }, WriteCSS: function() {
        var headID = document.getElementsByTagName("head")[0];
        var cssNode = document.createElement('link');
        cssNode.type = 'text/css';
        cssNode.rel = 'stylesheet';
        cssNode.href = '/static/slplayer.css';
        cssNode.media = 'screen';
        cssNode.id = 'player_css';
        headID.appendChild(cssNode);
    }, onWrited: DoNothing, onMediaEnded: DoNothing,
    Writed: function() {
        if (this.WriteCSS) {
            if (_('player_css') != null) { } else {
                this.WriteCSS();
            }
        }
        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();
        var t = this; this.IsWrited = true;
        if (this.Silverlight) { jQuery(document).ready(function() { $(".VButton.VFS").hide(); }); }
        else {
            if (this.TVZone[0] > 0) this.Click = function() {
                if (Players[this.id].AdIn) {
                    x = window.open(Players[this.id].a.link);
                    if (!x)
                        location.href = Players[this.id].a.link
                }
            }
            if (this.TVZone[0] > 0) ('<script for="Player' + this.id + '" event="Click"> var p=_("Player' + this.id + '");\
   for(var i=0;i<p.currentMedia.attributeCount;i++){var s=p.currentMedia.getAttributeName(i);  if(s=="AdUrl") {s=p.currentMedia.getItemInfo(s);Players[' + this.id + '].Clicked(s);break;}}</script>').write();
        }
        this.onWrited();
    },
    Clicked: function(url) { var a = window.open(url, "icvtAD"); if (!a) location.href = url; },
    SetURL: function(origurl, notplay) {
        var url = origurl;
        if (url.indexOf(":") == -1 && url.indexOf(".aspx") == -1)
            url = "mms://media.israelnn.com/inntv-eng/" + url;
        this.ActiveUrl = url; this.UrlSetted = 0;
        if (notplay) return false;
        if (this.Silverlight) {
            _('Player' + this.id).Content.p.SetUrl(url);
        }
        else _('Player' + this.id).URL = url;
        this.UrlSetted = true;
    },
    fA: function(a) {
        if (a.url) { this.SetURL(a.url); this.a = a; this.AdIn = true; }
        else { this.SetURL(this.video.url); this.AdIn = false; }
    }, _media_ended: function() { this.onMediaEnded(); this.AdIn = false; },
    ReplaceUrl: function(id) {
        try { this.IsLoad() } catch (e) { }
        p = _("Player" + this.id);

        if (this.AdIn) {
            this.AdIn = false;
            this.SetVideo(this.video);
        }
    }, Changed: function() {
        p = _("Player" + this.id);
        if (p.Silverlight) { }
        else {
            if (p.playState == 8) { this._media_ended(); }
            else if (p.lastState == 9 && p.playState == 3) {
                var nextAdTest = false;
                for (var i = 0; i < p.currentMedia.attributeCount; i++) {
                    var s = p.currentMedia.getAttributeName(i);
                    if (s == "AdLog") {
                        nextAdTest = true;
                        s = p.currentMedia.getItemInfo(s);
                        ExectueUrl(s);
                    }
                    else if (s == "imageName") {
                        nextAdTest = true;
                        s = p.currentMedia.getItemInfo(s);
                        this.UpdateImageAd(s);
                    }
                    else if (s == "AdUrl") {
                        nextAdTest = true;
                        s = p.currentMedia.getItemInfo(s);
                        this.UpdateImageClickUrl(s);
                    }
                }
                this.AdIn = nextAdTest;
            }
        }
        p.lastState = p.playState;
    },
    SetVideo: function(video, notplay) {
        this.video = video;
        this.ShowTitle(video.title);
        this.ShowWaitingImage(
            GetImage(video.image, (this.size.width - 20), this.size.height));
        var url = '';
        if (!this.AdIn && (this.TVZone[0] > 0 || this.TVZone[1] > 0)) {
            url = '/tv/fullscreen2.aspx?time=' + new Date().getTime() + '&zones=' + this.TVZone[0] + ',' + this.TVZone[1];
            if (!!this.video.url) {
                url += '&file=' + this.video.url;
            }
        }
        else {
            url = this.video.url;
        }
        this.SetURL(url, notplay);
    },
    Dispose: function() { Players.remove(this.id); }
}
function CheckPState()
{
	var played=false;
	for (var i=0;i<Players.length;i++)
	  if (Players[i]&&Players[i].Writed) {
	    var player = _("Player"+i), p = Players[i], playState;

	    p.UpdateMP()
	    if(p.Silverlight) {}
		if (p.playState==8||(p.playState==4&&p.lastState==3)) {p.ReplaceUrl()}
		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);
}
setTimeout(CheckPState,1500);
$(document).ready(function () {$('.VVideo').disableSelection();})