var slPlayer = new slPlayer();

function slPlayer() 
{
    var gPlayer;
    var flmpMedp;
    var isPromo;

    var broadcastid;
    var datarateid;
    
    var logoPos = new Array('', 'topleft', 'topright', 'bottomleft', 'bottomright');

    this.attach = function()
    {
        try 
        {
            this.gPlayer.close();
        } catch(err)
        {
        }

        $('#popin-label').html('Player Pop Out');
        $('#popin-label').attr('title', 'Player Pop Out');
        
        
        try 
        {
            pwin_iframe.$('#popup').html('Player Pop Out');
            pwin_iframe.$('#popup').attr('title', 'Player Pop Out');
        } catch(err)
        {
        }
        
        PopUpState = '0';
        
        eraseCookie('popupState_' + $('#portalId').html() + '_' + $('#channelId').html());
        
        this.createPlayer();
        this.showBanners();
    }

    this.detach = function() 
    {
        var playerDiv;
        var detached = document.getElementById("detached");
        var playerHost = document.getElementById("playerHost");
        
        if (playerHost.innerHTML == '')
        {
            this.attach();
        
        }else
        {
        
            playerHost.innerHTML = "";
            this.hideBanners();
            try 
            {
                adjustCustomHtml();
            } catch(err) {
            }
    
            try 
            {
                $('#popin-label').html('Player Pop In');
                $('#popin-label').attr('title', 'Player Pop In');
                
                
                try 
                {
                    pwin_iframe.$('#popup').html('Player Pop In');
                    pwin_iframe.$('#popup').attr('title', 'Player Pop In');
                } catch(err)
                {
                }
                
                createCookie('popupState_' + $('#portalId').html() + '_' + $('#channelId').html(), '1', 3);
                PopUpState = '1';
                this.playerOpen();
            
            } catch(err) 
            {
                alert("Player could not be popup.\nEnable popup windows in your browser and try again.");
                $('#popin-label').html('Player Pop Out');
                $('#popin-label').attr('title', 'Player Pop Out');
                
                try 
                {
                    pwin_iframe.$('#popup').html('Player Pop Out');
                    pwin_iframe.$('#popup').attr('title', 'Player Pop Out');
                } catch(err)
                {
                }
                
                eraseCookie('popupState_' + $('#portalId').html() + '_' + $('#channelId').html());
                PopUpState = '0';
                this.createPlayer();
                this.showBanners();
            
        }
    }

    }
    
    this.hideBanners = function() 
    {
        try 
        {
            document.getElementById("bannerHost").style.display = "none";
        } catch(err) {
        }
        
        try 
        {
            document.getElementById("bannerHostTop").style.display = "none";
        } catch(err) {
        }
    }

    this.showBanners = function() 
    {
        try 
        {
            document.getElementById("bannerHost").style.display = "block";
        } catch(err) {
        }
        
        try 
        {
            document.getElementById("bannerHostTop").style.display = "block";
        } catch(err) {
        }
    }

    this.getVideoParams = function() 
    {
        this.broadcastid = document.getElementById('broadcastid').value;
        this.datarateid = document.getElementById('datarateid').value;
    }

    this.gAsxUrl = function() 
    {
        this.getVideoParams();
        var rooturl = document.location.href.split(/([\?])/)[0];
        if(rooturl.search(/index.php/) == -1) 
        {
            rooturl = rooturl + '/index.php';
        }
        
        //url = rooturl + "?module=slAsx&broadcastid=" + this.broadcastid + "&datarateid=" + this.datarateid;
        url = rooturl + "?module=playlist&type=slASX&broadcastid=" + this.broadcastid + "&datarateid=0";// + this.datarateid;
        if (this.isPromo == 1) url = url + '&promo=1';
        
        
        
        return url;
    }

    this.gPlayerUrl = function() 
    {
        this.getVideoParams();
        var rooturl = document.location.href.split(/([\?])/)[0];
        if(rooturl.search(/index.php/) == -1) 
        {
            rooturl = rooturl + '/index.php';
        }
        
        url = rooturl + "?module=player&broadcastid=" + this.broadcastid + "&datarateid=0";// + this.datarateid;
        if (this.isPromo == 1) url = url + '&promo=1';
        
        return url;
    }

    this.playerOpen = function(URL) 
    {
        var id = "Player";
        this.gPlayer = window.open(this.gPlayerUrl(), id, 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=1,width=610,height=460,left = 10,top = 10');
        this.gPlayer.focus();
    }

    this.createPlayer = function() 
    {    
        document.getElementById("playerHost").innerHTML = this.embedSilverlight(null, "flmpMediaPlayer", "playerRow");
        
        if (PopUpState == '0')
        {
            this.showBanners();
            try 
            {
            adjustCustomHtml();
            } catch(err) 
            {
            }
        }else
        {
            this.detach();        
        }
    }

    this.isPlayerCreated = function() 
    {
        if(document.getElementById("playerHost").innerHTML.length) 
        {
            return 1;
        } else 
        {
            return 0;
        }
    }

    this.embedSilverlight = function(parentElement, pluginId, userContext, mPosition) 
    {
        if (!mPosition) mPosition = 0;
        var altHtml = pluginId == "sl1" ? null : "<!--not installed-->";
        var params = "sourceUrl=" + this.gAsxUrl() + ",mediavolume=7,popin=0,isBannerAvailable=0,mposition="+mPosition;
        if (document.getElementById('oLogo').value > 0)
        {
            params = params + ",olaylogoimg=" + document.getElementById('oLogoPath').value + 
                    ",olaylogomarginX=" + document.getElementById('oLogoMargX').value +
                    ",olaylogomarginY=" + document.getElementById('oLogoMargY').value +
                    ",olaylogopos=" + logoPos[document.getElementById('oLogo').value] +
                    ",olaylogopopoutlink=" +  document.getElementById('oLogoUrl').value;
                    
        }else
        {
            params = params + ",olaylogoimg=" + 
                    ",olaylogomarginX=" +
                    ",olaylogomarginY=" + 
                    ",olaylogopos=" +
                    ",olaylogopopoutlink=";
        }
        return Silverlight.createObject("/data/xap/player.xap",
        parentElement,
        pluginId, 
        {
            width: "592",
            height: "332", 
            background: "black",
            version:"2.0.31005.0",
            autoUpgrade: "true",
            margin: "0px",
            border: "0px;",
            windowless: "true"
        },
        { onError: onSLError, onLoad: onSLLoad },
        params,
        userContext);
    }

    this.playAsx = function(urlString) 
    {
        
        this.flmpMedp = document.getElementById("flmpMediaPlayer");
        
        if(!this.flmpMedp && PopUpState == '1') 
        {
            try
            {
                if (!this.gPlayer.document)
                {
                    try 
                    {
                        this.playerOpen();
                    
                    }catch(err)
                    {
                        alert("Player could not be popup.\nEnable popup windows in your browser and try again.");
                    } 
                    
                }
                
            }catch(err)
            {
                try 
                {
                    this.playerOpen();
                
                }catch(err)
                {
                    alert("Player could not be popup.\nEnable popup windows in your browser and try again.");
                }
            }
            this.flmpMedp = this.gPlayer.document.getElementById("flmpMediaPlayer");
        }
        
        try
        {
            this.flmpMedp.content.flmpExtInterface.flmpSetNewMediaAsxUrl(urlString);
        } catch(err) 
        {
            
        }
    
        /*if(!this.flmpMedp) 
        {
            alert("Please wait while the player is opening in new window.");
        }*/
        
        try 
        {
            this.gPlayer.focus();
        
        } catch(err) {
        }
                                
    }
    
    this.stopAsx = function()
    {
        
        this.flmpMedp = document.getElementById("flmpMediaPlayer");
        
        if (!this.flmpMedp) 
        {
            this.flmpMedp = this.gPlayer.document.getElementById("flmpMediaPlayer");
        }
        
        try
        {
            this.flmpMedp.content.flmpExtInterface.flmpPauseThePlayer();
            
        } catch(err) 
        {                                  
            
        }                  
        
    }
    
    
    
    this.captureEvent = function(event)
    {
        switch(event)
        {
          case "stop":
            try
            {
                $('#login-box-content').load(ChannelId+'index.php?module=loginbox'); 
            
            } catch(err)
            {
                try
                {
                    window.parent.$('#login-box-content').load(ChannelId+'index.php?module=loginbox');
                
                } catch(err)
                {
                    
                }
            }
            break;
          
          case "play":
            break;
            
          default:
            break;
        }
    }
    
}      


function captureEvent(event)
{
    window.setTimeout('slPlayer.captureEvent("' + event + '")', 3000);
}


//backwards compability
function detach() 
{
    slPlayer.detach();
}

