﻿// JScript File

if (!Array.prototype.indexOf) {
    Array.prototype.indexOf = function(item, startIndex) {
        var length = this.length;
        if (length != 0) {
                        startIndex = startIndex || 0;
                        if (startIndex < 0) {
                startIndex = Math.max(0, length + startIndex);
            }
            for (var i = startIndex; i < length; i++) {
                if (this[i] == item) {
                    return i;
                }
            }
        }
        return -1;
    }
}
if (!Array.prototype.remove) {
    Array.prototype.remove = function(item) {
        var index = this.indexOf(item);
        if (index >= 0) {
            this.splice(index, 1);
        }
        return (index >= 0);
    }
}
if (!Array.prototype.contains) {
    Array.prototype.contains = Array.prototype.exists = function(item) {
        var index = this.indexOf(item);
        return (index >= 0);
    }
}

function clickIE4(){if (event.button==2){return !hideContext;}}
function clickNS4(e){if (document.layers||document.getElementById&&!document.all){if (e.which==2||e.which==3){return !hideContext;}}}
if (document.layers){document.captureEvents(Event.MOUSEDOWN);document.onmousedown=clickNS4;}
else if (document.all&&!document.getElementById){document.onmousedown=clickIE4;}
document.oncontextmenu=new Function("return !hideContext;");


function GetEventX(event){
    if(theClientInfo.family=="ie4") return event.x; else return event.layerX; 
}

function GetEventY(event){ 
    if(theClientInfo.family=="ie4") return event.y; else return event.layerY; 
}
  
function DoMouseMove(event){ 
    if(!UseSilverlight && theTimeSlider.IsDragging())
        theTimeSlider.Drag( GetEventX(event) ); 
}
function DoMouseUp(event){ 
    if(!UseSilverlight && theTimeSlider.IsDragging()){ 
        theVideoPlayer.SetPosition(
            theTimeSlider.GetPosition()*theVideoPlayer.GetDuration());
            theTimeSlider.EndDrag();
    }
}

function DoResize(){
    if(pageInitialized){
        clearTimeout(resizeTimer);
        resizeTimer = setTimeout("theView.Paint();",250);
    }
}

function DoDebugAsync(aVal, aColor, callId) {
    if (debugMode && aVal != undefined) {
        if (Addon_Debug_Initialized) Addon_Debug_DoDebug_Async(aVal, aColor, callId);
    }
}

function DoDebugCallback(aVal, aColor, callId) {
    if (debugMode && aVal != undefined) {
        if (Addon_Debug_Initialized) Addon_Debug_DoDebug_Callback(aVal, aColor, callId);
    }
}

function DoDebug(aVal, aBgColor, aIsMoreEvents) {
    if (debugMode && aVal != undefined) {
        if(aIsMoreEvents==undefined)aIsMoreEvents=false;
        if(aBgColor==undefined)aBgColor="transparent";
        if(Addon_Debug_Initialized) Addon_Debug_DoDebug(aVal, aBgColor, aIsMoreEvents); 
    }
}

function DoPlayerStateChange(lOldState, lNewState){
    //if(theLiveStatusId==4 &&
    if(lNewState==2){
        playStarted=true;
    }
    if (lOldState != lNewState) {
        var stateArr = new Array("stopped", "paused", "playing", "waiting", ">>", "<<", "next", "previous", "closed");
        DoDebug("PlayState:" + stateArr[lNewState] ,"#cccccc");
    }
}

function FindInDoubleArray(aId,aArr,aIndex){
    for(var i=0;i<aArr.length;i++)
        if(aArr[i][0]==aId)return aArr[i][aIndex];
    return "";
}

function FindArrayInDoubleArray(aId,aArr){
    for(var i=0;i<aArr.length;i++)
        if(aArr[i][0]==aId)return aArr[i];
    return "";
}
function FindIndexInDoubleArray(aId,aArr){
    for(var i=0;i<aArr.length;i++)
        if(aArr[i][0]==aId)return i;
    return -1;
}

function Addon_SYSTEM_doEvent(){}

function SetMediaPos(aValue){
    theVideoPlayer.SetPosition(theVideoPlayer.GetPosition()+aValue);
}

function ShowFase(){
    theView.HideAllTabs();
    for(var i=0;i<theAddonFases.length;i++){
        if(theLiveStatusId<=theAddonFases[i][1].length){
            if(theAddonFases[i][1].substr(theLiveStatusId-2,1)=="1"){
                if(theAddonFases[i][2]){
                    theView.ShowTabHead(theAddonFases[i][0]);
                }
            }
        }
    }
    theView.ShowFirstTab();
}

function DoPlayList(){
    LiveClip=theLiveClipUrl+"_"+getCurrentBitrate()+"_"+getCurrentLanguage();
    ArchiveClip=theArchiveClipUrl+"/"+getCurrentBitrate()+"_"+getCurrentLanguage()+".wmv";
    
    ShowFase();
    
    switch(theLiveStatusId)
    {
        case LS_OFF:
            break    
        case LS_READY:
            try{eval(theCustomScripts[theLiveStatusId]);}catch(e){DoDebug("CUSTOM_SCRIPT "+theLiveStatusId+" error: " +e.description);}
            theVideoPlayer.LoadMovie(theTestClipUrl);
            break;
        case LS_PRELIVE:
            try{eval(theCustomScripts[theLiveStatusId]);}catch(e){DoDebug("CUSTOM_SCRIPT "+theLiveStatusId+" error: " +e.description);}
            theVideoPlayer.LoadMovie(LiveClip);
            break;
        case LS_LIVE:
            try{eval(theCustomScripts[theLiveStatusId]);}catch(e){DoDebug("CUSTOM_SCRIPT "+theLiveStatusId+" error: " +e.description);}
            theVideoPlayer.LoadMovie(LiveClip);
            //UpdateEvents();
            break;
        case LS_PREINDEXED:
            for(var j=0;j<addonsArr.length;j++)try{
                eval("Addon_"+addonsArr[j]+"_Clear();"); 
            }catch(ex){}
            try{eval(theCustomScripts[theLiveStatusId]);}catch(e){DoDebug("CUSTOM_SCRIPT "+theLiveStatusId+" error: " +e.description);}
            theVideoPlayer.LoadMovie(LiveClip);
            break;
        case LS_INDEXED:
            try{eval(theCustomScripts[theLiveStatusId]);}catch(e){DoDebug("CUSTOM_SCRIPT "+theLiveStatusId+" error: " +e.description);}
            theVideoPlayer.LoadMovie(ArchiveClip);
            break;
        case LS_ARCHIVE:
            try{eval(theCustomScripts[theLiveStatusId]);}catch(e){DoDebug("CUSTOM_SCRIPT "+theLiveStatusId+" error: " +e.description);}
            theVideoPlayer.LoadMovie(ArchiveClip);
            break;
        default:
           theLiveStatusId=LS_OFF;
    }
    if(demoMode){
        theView.HideTabs(new Array(''));
        theView.ShowTab('Welcome');
    }
    /*for(var j=0;j<addonsArr.length;j++)try{
        eval("Addon_"+addonsArr[j]+"_LiveStatusChange();"); 
    }catch(ex){}*/        
}

function getCurrentBitrate(){
    return FindInDoubleArray(theBitrateId,theBitrates,1);
}

function getCurrentLanguage(){
    return FindInDoubleArray(theLanguageId,theLanguages,1);
}
