
window.onunload = WonUnload;
window.onresize = WonResize;
window.onload = WonLoad;
  
var scheduleCalendar;
var scheduleTab;
var refreshActive;
var timeoutId;

var innerContent = new ajaxContent();
var innerContentRequest;
var scrollInnerContent;
var PopUpState = '0';

var slPlayer = new slPlayer();

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

    var broadcastid;
    var datarateid;

    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) {
            }
    }

    this.showBanners = function() 
    {
        try 
        {
            document.getElementById("bannerHost").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;
        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=" + 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=495,height=378,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;
        return Silverlight.createObject("/data/xap/player.xap",
        parentElement,
        pluginId, 
        {
            width: "480",
            height: "333", 
            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)
            {
                
            }
            break;
          
          case "play":
            break;
            
          default:
            break;
        }
    }
    
}      


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


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

// contains all URL ang GET properties; GET is stored php-like style (.get[value]=key);

function urlInfo()
{
    this.protocol = document.location.protocol;
    this.hash = document.location.hash.replace('#', '');
    this.host = document.location.host;
    this.path = document.location.pathname.replace('/index.php', '');
    
    while (this.path[0]=='/') this.path=this.path.substr(1);
    while (this.path[this.path.length-1]=='/') this.path=this.path.substr(0, this.path.length-1); 
    
    this.get = new Array();
    
    tmp = document.location.search.replace('?', '');
    
    if (tmp.length>0)
    {
        if (tmp.indexOf('&')>-1)
        {
            tmp = tmp.split('&');
            c = tmp.length;
            
            for (i=0; i<c; i++)
            {
                tmp1 = tmp[i].split('=');
                this.get[tmp1[0]]=tmp1[1];
            }
            
        }else
        {
            tmp = tmp.split('=');
            this.get[tmp[0]]=tmp[1];
        }
    }
    
}

function minWinSize() 
{
    var myWidth = 0, myHeight = 0;
    if(typeof(window.innerWidth) == 'number') {
        myWidth = window.innerWidth;
        myHeight = window.innerHeight;
    } 
    else if(document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) {
        myWidth = document.documentElement.clientWidth;
        myHeight = document.documentElement.clientHeight;
    } 
    else if(document.body && (document.body.clientWidth || document.body.clientHeight)) {
        myWidth = document.body.clientWidth;
        myHeight = document.body.clientHeight;
    }
        
        var x = document.getElementById("flmpMediaPlayer");
        if(document.getElementById("widebanner")) {
        x.style.height = myHeight - 65 + "px";
    } else {
        x.style.height = myHeight - 20 + "px";
    }
}


function WonLoad() 
{

    scheduleCalendar = new scheduleCalendar();
    try 
    {
        scheduleCalendar.init();
    } catch(err) {
    }
    scheduleTab = new scheduleTab();
    scheduleTab.init();

    try {
        slPlayer.createPlayer();
    
    } catch(err) {
    }

    try 
    {
        dT();
        
    } catch(err) {
    }

    try 
    {
        adjustCustomHtml();
        
    } catch(err) {
    }
    
}

function WonResize() 
{
    try 
    {
        dT();
        
    } catch(err) {
    }

    try 
    {
        adjustCustomHtml();
        
    } catch(err) {
    }
}

function WonUnload() 
{
    try 
    {
        slPlayer.gPlayer.close();
               
    } catch(err) {
    }
}


function showHide(id) 
{
    id = "news" + id;
    
    $('#'+id).toggle('slow');
    
}

function getQueryVariable(variable) 
{
    var query = window.location.search.substring(1);
    var vars = query.split("&");
    
    for (var i=0;i<vars.length;i++) 
    {
      var pair = vars[i].split("=");
      if (pair[0] == variable) 
      {
          return pair[1];
      }
    }
    //alert('Query Variable ' + variable + ' not found');
}



function smoothScroll(eID, add) 
{
    if (!add) add=0;
    
    try
    {  
        var targetOffset = $('#'+eID).offset().top+add; 
    
    } catch (err)
    {
        var targetOffset = add;
    }
         
      if($.browser.safari || $.browser.webkit) 
      {
          $('body').animate({scrollTop: targetOffset}, 700);
      }else
      {
          $('html').animate({scrollTop: targetOffset}, 700);
      }                                            
}

var playVideoData_bid;
var playVideoData_dr;
var openUrlData;
var isPopUp;

function playPromo(broadcastid, datarateid, popup) 
{
    
    var rooturl = document.location.href.split(/([\?])/)[0];
    
        if(rooturl.search(/index.php/) == -1) 
        {
            rooturl = rooturl + '/index.php';
        }
        var url = rooturl + "?module=slAsx&broadcastid=" + broadcastid + "&datarateid="+datarateid + '&promo=1';
            x = document.getElementById("broadcastid");
            y = document.getElementById("datarateid");
            x.value = broadcastid;
            y.value = datarateid;
                 
        slPlayer.isPromo = 1;
        slPlayer.playAsx(url);
        
        if(slPlayer.isPlayerCreated()) 
        {
            smoothScroll("pageTop");
            if (popup == 1)
            {
                $('#pwin').dialog('close');
                isPopUp = 0;
            }
        }
}


function playVideo(broadcastid, datarateid, popup) 
{

    isPopUp = 0;    
    
    $.get('index.php', 
      {module: 'ppvItemBilling', action: 'checkWallet', broadcast: broadcastid, datarate: datarateid}, 
      function (data) 
      {
        
        if (data == '-1')  // free to view
        {
            var rooturl = document.location.href.split(/([\?])/)[0];
        
            if(rooturl.search(/index.php/) == -1) 
            {
                rooturl = rooturl + '/index.php';
            }
            var url = rooturl + "?module=slAsx&broadcastid=" + broadcastid + "&datarateid="+datarateid;
                x = document.getElementById("broadcastid");
                y = document.getElementById("datarateid");
                x.value = broadcastid;
                y.value = datarateid;
                
            slPlayer.isPromo = 0;                       
            slPlayer.playAsx(url);
            
            if(slPlayer.isPlayerCreated()) 
            {
                smoothScroll("pageTop");
                if (popup == 1)
                {
                    $('#pwin').dialog('close');
                    isPopUp = 0;
                }
            }
            
        }else if (data == '0')  // Pay per Item - ask for payment
        {
            playVideoData_bid = broadcastid;
            playVideoData_dr = datarateid;
            
            if (popup == 1)
            {
                $('#pwin').dialog('close');
                isPopUp = 1;
            }
            
            sup('index.php?module=ppvItemBilling&action=confirmPayment&callBack=playConfirmed&broadcast='+broadcastid+'&datarate='+datarateid, 'center');
            
        }else  // must top-up
        {
            var tmp = data.split(',');
            playVideoData_bid = broadcastid;
            
            var cur = tmp[0] - 1;
            openUrlData = 'index.php?module=userAccount&action=buyCredits&page=buyCredit&subscriptionCurrency=' + cur + '&creditAmount=' + tmp[2];
            
            if (popup == 1)
            {
                $('#pwin').dialog('close');
                isPopUp = 1;
            }
            
            sup('index.php?module=ppvItemBilling&action=confirmTopup&callBack=topupConfirmed&broadcast='+broadcastid+'&datarate='+datarateid, 'center');
    
        }
        
    });
    
   
}


function playConfirmed(result)
{
    
    var broadcastid = playVideoData_bid;
    var datarateid = playVideoData_dr;
    
    $('#pwin').dialog('close');
    
    if (result == 1)
    {
    
        
        $.get('index.php', 
          {module: 'ppvItemBilling', action: 'Pay', broadcast: broadcastid, datarate: datarateid}, 
          function (data) 
          {
            $('#login-box-content').load(ChannelId+'index.php?module=loginbox'); 
            
            innerContent.getLink('');
            
            if (data == 'fail') return false;
            
            var rooturl = document.location.href.split(/([\?])/)[0];
        
            if(rooturl.search(/index.php/) == -1) 
            {
                rooturl = rooturl + '/index.php';
            }
            var url = rooturl + "?module=slAsx&broadcastid=" + broadcastid + "&datarateid="+datarateid;
                x = document.getElementById("broadcastid");
                y = document.getElementById("datarateid");
                x.value = broadcastid;
                y.value = datarateid;
                                        
            slPlayer.playAsx(url);
            
            if(slPlayer.isPlayerCreated()) 
            {
                smoothScroll("pageTop");                
            }else
            {
                if (isPopUp == 1)
                {
                    sup('index.php?module=moreInfo&broadcastid=' + broadcastid);
                    slPlayer.gPlayer.focus();
                    isPopUp = 0;
                }
            }
            
          });
    }else
    {
        if (isPopUp == 1)
        {
            sup('index.php?module=moreInfo&broadcastid=' + broadcastid);
            isPopUp = 0;
        }
        
    }
    
}


function downloadTopup(broadcastid)
{
    $('#pwin').dialog('close');
    sup('index.php?module=ppvItemBilling&action=confirmTopup&download=1&callBack=topupConfirmed&broadcast='+broadcastid+'&datarate=1', 'center');
}

function topupConfirmed(result)
{
    
    $('#pwin').dialog('close');
    
    if (result == 1)
    {
        sup(openUrlData);
    }else
    {
        if (isPopUp == 1)
        {
            sup('index.php?module=moreInfo&broadcastid=' + playVideoData_bid);
            isPopUp = 0;
        }
    }
}


function downloadClip(broadcastid) 
{
    $('#pwin').dialog('close');
    sup('index.php?module=ppvItemBilling&action=confirmPayment&callBack=downloadConfirmed&broadcast='+broadcastid+'&datarate=1&download=1', 'center');
}

function downloadConfirmed() 
{
    document.location.href = openUrlData;
    captureEvent('stop');
}



function playTimeline(broadcastid, datarateid, timelineid) {
    var rooturl = document.location.href.split(/([\?])/)[0];
    var url = rooturl + "?module=slAsx&broadcastid=" + broadcastid + "&datarateid="+datarateid+"&timelineid="+timelineid;
        x = parent.document.getElementById("broadcastid");
        y = parent.document.getElementById("datarateid");
        x.value = broadcastid;
        y.value = datarateid;                        
    slPlayer.playAsx(url);
    if(slPlayer.isPlayerCreated()) {
        smoothScroll("pageTop");
    }
}

function handleEnter(field, event) {
    var keyCode = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
    if(keyCode == 13) {
    var i;
    for (i = 0; i < field.form.elements.length; i++)
        if(field == field.form.elements[i])
        break;
        i = (i + 1) % field.form.elements.length;
        field.form.elements[i].focus();
        return false;
    } else return true;
}
/* Adult Warning Message */

function enter() {
    window.location = 'index.php?module=main&enter=true';
}

function leave() {
    window.location = 'http://www.wikipedia.org';
}

//currency selector
function changeCurrencyIndex(s) 
{   
    url = new urlInfo();
   
    if (url.path.indexOf('/')>-1)
    {
        tmp = url.path.split('/');
        ChannelId = tmp[0]+'/';
        
    }else
    {
        ChannelId = url.path+'/';
    }
                        
    
    if (ChannelId != '/') currencyURL = '/' + ChannelId + "index.php?update=1&changeCurrency=" + s.options[s.selectedIndex].value
       else currencyURL = "index.php?update=1&changeCurrency=" + s.options[s.selectedIndex].value;
                                                                                    
    document.location = currencyURL;
}


/* CLOCK */

var days = new Array('Sun','Mon','Tue','Wed','Thu','Fri','Sat');
var months = new Array('Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec');
var currMs = 0;
var startMs = 0;
var clockTime = 0;

function checkClock(addMs)
{   
    try 
    {
        ms = $('#msTime').html();
        
        if (ms > startMs)
        {
            msDiffv = $('#msDiff').html();
            startMs = parseFloat(ms) + parseFloat(msDiffv);
            currMs = startMs;
            showDate(1);
            
        }else
        {
            currMs = parseFloat(currMs) + parseFloat(addMs);
            showDate(0);
        }
        
    } catch(err) 
    {
        clearTimeout(clockTime);
    }
}

function showDate(x)
{
    var d = new Date(parseFloat(currMs));
    
    tp_txt = addZero(d.getUTCHours()) + ':' + addZero(d.getUTCMinutes()) + ' GMT &nbsp; ';
    tp_txt = tp_txt + days[d.getUTCDay()] + ' ' + d.getUTCDate() + ' ' + months[d.getUTCMonth()] + ' ' + d.getUTCFullYear();
    
    try
    {
        $('#tp').html(tp_txt);
        
        if (x == 0)
        {
            clearTimeout(clockTime);
            clockTime = setTimeout('checkClock(60000)',60000);
        }else
        {
            clearTimeout(clockTime);
            sec = 60 - d.getUTCSeconds();
            setTimeout('checkClock(' + sec * 1000 + ')', sec * 1000);
        }
        
    } catch(err) 
    {
        clearTimeout(clockTime);
    }
}

function addZero(x)
{ 
    return (x>9)  ?x : '0' + x; 
}

