// Cross Browser Support for Namespaces
if("undefined" === typeof registerNamespace){
    registerNamespace = function(ns){
        var rootObject = window;
        var namespaceParts = ns.split(".");
        for(var index=0; index<namespaceParts.length; index++){
            var currentPart = namespaceParts[index];
            if(!rootObject[currentPart]) rootObject[currentPart] = new Object();
            rootObject = rootObject[currentPart];
        }
    }
}

registerNamespace('GoPromotions.Player')
// preload images
var playerpre1 = new Image();
playerpre1.src = '/app_themes/all/players/skin1/playerstarting.gif'
var playerpre2 = new Image();
playerpre2.src = '/app_themes/all/players/skin1/playerstopout.gif'
var playerpre3 = new Image();
playerpre3.src = '/app_themes/all/players/skin1/playerstopover.gif'
var playerpre4 = new Image();
playerpre4.src = '/app_themes/all/players/skin1/playerplayingout.gif'
var playerpre5 = new Image();
playerpre5.src = '/app_themes/all/players/skin1/playerplayingover.gif'
var playerpre6 = new Image();
playerpre6.src = '/app_themes/all/players/skin1/playerpausedout.gif'
var playerpre7 = new Image();
playerpre7.src = '/app_themes/all/players/skin1/playerpausedover.gif'
var playerpre8 = new Image();
playerpre8.src = '/app_themes/all/players/skin1/playerstoppedout.gif'
var playerpre9 = new Image();
playerpre9.src = '/app_themes/all/players/skin1/playerstoppedover.gif'
var playerpre10 = new Image();
playerpre10.src = '/app_themes/all/players/skin1/playermuteout.gif'
var playerpre11 = new Image();
playerpre11.src = '/app_themes/all/players/skin1/playermuteover.gif'
var playerpre12 = new Image();
playerpre12.src = '/app_themes/all/players/skin1/playermutedout.gif'
var playerpre13 = new Image();
playerpre13.src = '/app_themes/all/players/skin1/playermutedover.gif'

GoPromotions.Player.ConsoleEvent = function(objConsole){
    var param = objConsole.id.substring(7).split('_');        
    var index = param[0];
    var button = param[1];
    
    var objVideo =  document.getElementById('video' + index);
    
        if (objVideo != null){
        var currentStatus = GoPromotions.Player.GetPlayerStatus(index);
        switch (button){
            case 'start':
                switch (currentStatus){
                    case 'stopped':
                    case 'paused':
                        if (typeof(GoPromotions.Tracking) != 'undefined') GoPromotions.Tracking.TrackEvent('Video', 'Play', objVideo.id)
                        GoPromotions.Player.ChangePlayerState(index, 'start');
                        break;
                    case 'playing':
                        GoPromotions.Player.ChangePlayerState(index, 'pause');
                        break;
                }
                break;
            case 'stop':
                switch (currentStatus){
                    case 'playing':
                    case 'paused':
                        GoPromotions.Player.ChangePlayerState(index, 'stop');
                        break;
                }
                break;
            case 'mute':
                var isMuted = null;
                var playerType = GoPromotions.Player.GetPlayerType(index);
                switch (playerType){
                    case 'mediaPlayer' : 
                        isMuted = objVideo.settings.mute;
                        objVideo.settings.mute = ! isMuted;
                        break;
                    case 'realPlayer' :
                    case 'quickTime' :
                        isMuted = objVideo.GetMute();
                        objVideo.SetMute(! isMuted);
                        break;
                } 
                if (isMuted) GoPromotions.Player.SetImage(objConsole, '/app_themes/all/players/skin1/PlayerMuteOut.gif', '/app_themes/all/players/skin1/PlayerMuteOver.gif');
                else GoPromotions.Player.SetImage(objConsole, '/app_themes/all/players/skin1/PlayerMutedOut.gif', '/app_themes/all/players/skin1/PlayerMutedOver.gif');
                break;
            default:
                break;
            
        }
    }
}
GoPromotions.Player.ProcessConsoleEvent = function(index, button){
    var objVideo =  document.getElementById('video' + index);
    
    if (objVideo != null){
        var playerType = GoPromotions.Player.GetPlayerType(index);
        switch (button){
            case 'mute' :
                var isMuted = null;
                switch (playerType){
                    case 'mediaPlayer' : 
                        isMuted = objVideo.settings.mute;
                        objVideo.settings.mute = ! isMuted;
                        break;
                    case 'realPlayer' :
                    case 'quickTime' :
                        isMuted = objVideo.GetMute();
                        objVideo.SetMute(! isMuted);
                        break;
                } 
                if (isMuted) GoPromotions.Player.ConsoleChangeImage('mute', index, 0);
                else GoPromotions.Player.ConsoleChangeImage('mute', index, 2);
                break;
            default :
                GoPromotions.Player.ChangePlayerState(index, button);
                break;
        }
    }
}

GoPromotions.Player.ConsoleChangeImage = function(button, index, status){   
    var objButton = document.getElementById('console' + index + '_' + button);
    switch (status){
        case 0 :
            objButton.src = '/app_themes/all/console/'+button+'.gif'
            break;
        case 1 :
            objButton.src = '/app_themes/all/console/'+button+'_wait.gif'
            break;
        case 2 :
            objButton.src = '/app_themes/all/console/'+button+'_selected.gif'
            break;
    }
}
GoPromotions.Player.PlayerStateEvent = function(objVideo){
    var index = objVideo.id.substring(5);
    GoPromotions.Player.CheckPlayerState(index);
}
GoPromotions.Player.CheckPlayerState = function(index){
    //if (currentIndex == null || currentIndex == index){
        var objVideo = document.getElementById('video'+index);
        if (objVideo != null){
            var playerType = GoPromotions.Player.GetPlayerType(index);
            var currentStatus = GoPromotions.Player.GetPlayerStatus(index);
            var playerStatus = currentStatus;
            switch (playerType){
                case 'mediaPlayer' :
                    var playState = objVideo.playState;
                    switch (playState){ 
                        case 1: //stopped
                        case 8: //media ended
                            playerStatus = 'stopped'
                            break;
                        case 2: //paused
                            playerStatus = 'paused'
                            break;
                        case 3: //playing
                            playerStatus = 'playing'
                            break;
                        case 6: //buffering
                        case 7: //waiting
                        case 9: //transistioning
                        case 11: //reconnecting
                            playerStatus = 'starting'
                            break;
                   }
                   break;
                case 'realPlayer' :
                    var playState = objVideo.GetPlayState();
                    switch (playState){ 
                        case 0 : //stopped
                            playerStatus = 'stopped'
                            break;
                        case 4 : //paused
                            playerStatus = 'paused'
                            break;
                        case 3 : //playing
                            playerStatus = 'playing'
                            break;
                        case 1 : //contacting
                        case 2 : //buffering
                        case 5 : //seeking
                            playerStatus = 'starting'
                            break;
                    }
                    break;
                case 'quickTime' :
                    try {
                        var playState = objVideo.GetPluginStatus();
                        switch (playState.toLowerCase()){
                            case 'waiting' :
                            case 'loading' :
                                break;
                            case 'playable' :
                            case 'complete' : 
                                if (objVideo.GetTime() == 0){
                                    //if (playerStatus == null) objVideo.style.visibility ='hidden';
                                }
                                else {
                                    switch(playerStatus){
                                        case 'start' :
                                        case 'starting' :
                                            if (objVideo.GetTime() > 0){
                                                playerStatus='playing';
                                            }
                                            break;
                                        case 'playing' :
                                            if (objVideo.GetTime() == objVideo.GetDuration()){
                                                playerStatus='stopped';
                                            }
                                            break;
                                    }                            
                                }
                                break;
                        }
                    }
                    catch(ex){
                        alert('TODO: quicktime load error');
                    }
                    break;
                case 'flashPlayer' :
                    var playState = objVideo.IsPlaying();
                    if (playState) playerStatus='playing'
                    else playerStatus='stopped'
            }
            if (currentStatus != playerStatus){
                switch (playerStatus){
                    case 'starting':
                        GoPromotions.Player.ShowStatus(index, 'starting');
                        break;
                    case 'playing' :
                        GoPromotions.Player.ShowStatus(index, 'start')  
                        break;
                    case 'stopped':
                        GoPromotions.Player.ShowStatus(index, 'stop')
                        break;
                }
            }
            GoPromotions.Player.SetPlayerStatus(index, playerStatus);
            setTimeout('GoPromotions.Player.CheckPlayerState('+index+')',250);
        }
}
GoPromotions.Player.ChangePlayerState = function(index, newState){
    var playerStatus = GoPromotions.Player.GetPlayerStatus(index);
    switch (playerStatus){
        case 'starting' :
            switch (newState){
                case 'stop' :
                case 'pause' :
                    setTimeout("GoPromotions.Player.ChangePlayerState("+index+",'"+newState+"')",250);
                    break;
            }
            break;
        case 'playing' :
            switch (newState){
                case 'stop' :
                case 'pause' :
                    GoPromotions.Player.SetPlayerState(index, newState);
                    break;
                default :
                    break;
            }
            break;
        case 'paused' :
            switch (newState){
                case 'start' :
                case 'stop' :
                    GoPromotions.Player.SetPlayerState(index, newState);
                    break;
            }        
        default :
            switch (newState){
                case 'start' :
                    GoPromotions.Player.SetPlayerState(index, 'starting');
                    setTimeout('GoPromotions.Player.CheckPlayerState('+index+')',250);
                    break;
            }
            break;
    }
}
GoPromotions.Player.SetPlayerState = function(index, newState){
    var objVideo = document.getElementById('video'+index);
    if (objVideo != null) {
        switch (GoPromotions.Player.GetPlayerType(index)){
            case 'mediaPlayer' :
                switch (newState){
                    case 'starting' :
                        objVideo.controls.play();
                        break;
                    case 'pause' :
                        objVideo.controls.pause();
                        break;
                    case 'stop' :
                        objVideo.controls.stop();
                        break;
                }
                break;
            case 'realPlayer' :
                switch (newState){
                    case 'starting' :
                        //objVideo.SetSource(objects[index].path);
                        objVideo.DoPlay();
                        break;
                    case 'pause' :
                        objVideo.DoPause();
                        break;
                    case 'stop' :
                        objVideo.DoStop();
                        break;
                }
                break;
            case 'quickTime' :
                switch (newState){
                    case 'starting' :
                        objVideo.Play();
                        GoPromotions.Player.SetPlayerStatus(index, 'start'); //QuickTime player has no status
                        break;
                    case 'pause' :
                        objVideo.Stop();
                        GoPromotions.Player.SetPlayerStatus(index, 'paused'); //QuickTime player has no status
                        break;
                    case 'stop' :
                        objVideo.Stop();
                        objVideo.Rewind();
                        GoPromotions.Player.SetPlayerStatus(index, 'stopped'); //QuickTime player has no status
                        break;
                }
                break;
            case 'flashPlayer' :
                switch (newState){
                    case 'starting' :
                        //objVideo.SetSource(objects[index].path);
                        objVideo.Play();
                        break;
                    case 'pause' :
                        objVideo.StopPlay();
                        break;
                    case 'stop' :
                        objVideo.StopPlay();
                        objVideo.Rewind();
                        break;
                }
                break;
        }
        GoPromotions.Player.ShowStatus(index, newState);
    }
}

GoPromotions.Player.ShowStatus = function(index, newState){
    objStart = document.getElementById('console' + index + '_start');     
    objStop = document.getElementById('console' + index + '_stop');     
    switch (newState){
        case 'starting' :
            GoPromotions.Player.SetImage(objStart, '/app_themes/all/players/skin1/playerstarting.gif', null);
            GoPromotions.Player.SetImage(objStop, '/app_themes/all/players/skin1/playerstopout.gif', null);
            GoPromotions.Player.PostChangeEvent(index, false)
            break;
        case 'start' :
            GoPromotions.Player.SetImage(objStart, '/app_themes/all/players/skin1/playerplayingout.gif', '/app_themes/all/players/skin1/playerplayingover.gif');
            GoPromotions.Player.SetImage(objStop,'/app_themes/all/players/skin1/playerstopout.gif', '/app_themes/all/players/skin1/playerstopover.gif');
            GoPromotions.Player.PostChangeEvent(index, false)
            break;
        case 'pause' :
            GoPromotions.Player.SetImage(objStart, '/app_themes/all/players/skin1/playerpausedout.gif', '/app_themes/all/players/skin1/playerpausedover.gif');
            GoPromotions.Player.SetImage(objStop,'/app_themes/all/players/skin1/playerstopout.gif', '/app_themes/all/players/skin1/playerstopover.gif');
            GoPromotions.Player.PostChangeEvent(index, true)
            break;
        case 'stop' :
            GoPromotions.Player.SetImage(objStart, '/app_themes/all/players/skin1/playerstoppedout.gif', '/app_themes/all/players/skin1/playerstoppedover.gif');
            GoPromotions.Player.SetImage(objStop,'/app_themes/all/players/skin1/playerstopout.gif', null);
            GoPromotions.Player.PostChangeEvent(index, true)
            break;
    }
}
GoPromotions.Player.MouseOver = function(objImage, imageURL){
    objImage.style.cursor = 'hand';
    objImage.src = imageURL;
}
GoPromotions.Player.MouseOut = function(objImage, imageURL){
    objImage.style.cursor = 'default';
    objImage.src = imageURL;
}
GoPromotions.Player.SetImage = function(objImage, imageOut, imageOver){
    if (imageOver != null){
        objImage.onmouseover = function(){GoPromotions.Player.MouseOver(objImage,imageOver);};
        objImage.onmouseout = function(){GoPromotions.Player.MouseOver(objImage,imageOut);};   
    }
    else {
        objImage.onmouseover = null;
        objImage.onmouseout = null;
    }
    objImage.src = imageOut;
}

GoPromotions.Player.PostChangeEvent = function(index, show){
    try{
        GoPromotions.Player.PostChangeFunction(index, show);
    }
    catch(ex){}  
}
GoPromotions.Player.GetPlayerStatus = function(index){
    var objPlayerType = document.getElementById('video'+index+'_PlayerStatus');
    return objPlayerType.value;
}
GoPromotions.Player.SetPlayerStatus = function(index, value){
    var objPlayerType = document.getElementById('video'+index+'_PlayerStatus');
    objPlayerType.value = value;
}
GoPromotions.Player.GetPlayerType = function(index){
    var objVideo = document.getElementById('video'+index);
    if (objVideo.classid){
        switch(objVideo.classid){
            case 'CLSID:6BF52A52-394A-11D3-B153-00C04F79FAA6' : 
                return 'mediaPlayer';
            case 'CLSID:CFCDAA03-8BE4-11CF-B84B-0020AFBBCCFA' :
                return 'realPlayer';
            case 'CLSID:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B' :
                return 'quickTime';
            case 'CLSID:D27CDB6E-AE6D-11CF-96B8-444553540000' :
                return 'flashPlayer';
        }
    }
    else{
        switch(objVideo.type){
            case 'application/x-ms-wmp' :
                return 'mediaPlayer';
            case 'audio/x-pn-realaudio-plugin' :
                return 'realPlayer';
            case 'video/quicktime' :        
                return 'quickTime';
            case 'application/x-shockwave-flash' :        
                return 'flashPlayer';
        }
    }
}

