var isFromAnadin=false;
var isDownload = false;
var sExit = "";

var FlagAr = sFlag.split("");
if(FlagAr[1]!="1")
{
	var x = window.screen.availWidth;
	var y = window.screen.availHeight;
	window.moveTo(0,0);
	window.resizeTo(x,y);
}
if(FlagAr[2]=="1")
{
	self.blur();
}	

var SesClose = false;
var srChanged = false;
	
function SesCloseWin()
{
	SesClose = true;
}

function goToLang(lang)
{
    var currentDomain = location.host;
    var ipPattern = /\d+\.\d+\.\d+\.\d+/;
    var devDomainPattern = /\.dev:\d{4}$/; 
    window.onbeforeunload = function() {return;}       
       
    
    if (lang == "fr" || lang=="en" || lang=="de" || lang=="es" || lang=="it" || lang=="kr" || lang=="nl" || lang=="pt" || lang=="sv" || lang=="cns" || lang=="cnt" || lang=="da" ||  lang=="tr" || lang=="el" || lang=="pl" || lang=="ro" || lang=="ru" || lang=="bg" || lang=="hu" || lang=="lv" || lang=="cz" || lang=="lt" || lang=="ee") 
    {
        if (sGoPage == "bbc_bonus")
        {
             if (lang == "da" || lang == "de" || lang == "es" || lang == "fr" || lang == "sv" || lang == "nl" || lang == "pt" || lang == "el")
             {
                if(currentDomain.indexOf("stage.") == -1 &&  !ipPattern.test(currentDomain) && !devDomainPattern.test(currentDomain))
                {
                    currentDomain = lang + currentDomain.substr(currentDomain.indexOf("."));
                    window.location.href = "http://" + currentDomain +"?" + sCutLang + "&page=bbc_bonus&lang=" + lang;
                }
                else
                    window.location.href = "/?" + sCutLang + "&page=bbc_bonus&lang=" + lang;
             }
             else if(lang == "en")
             {
                if(currentDomain.indexOf("stage.") == -1 &&  !ipPattern.test(currentDomain) && !devDomainPattern.test(currentDomain))
                {
                    currentDomain = "www" + currentDomain.substr(currentDomain.indexOf("."));
                    window.location.href = "http://" + currentDomain + "/?" + sCutLang + "&page=bbc_bonus&lang=en";
                }
                else
                window.location.href = "/?" + sCutLang + "&page=bbc_bonus&lang=" + lang;
             }
             else if(lang == "it")
             {
                if(currentDomain.indexOf("stage.") == -1 &&  !ipPattern.test(currentDomain) && !devDomainPattern.test(currentDomain))
                {
                    window.location.href = "http://" + ItalyDomain + "/?" + sCutLang + "&page=bbc_bonus&lang=it";
                }
                else
                    window.location.href = "/?" + sCutLang + "&page=bbc_bonus&lang=" + lang;
             }
             else
             {
                if(currentDomain.indexOf("stage.") == -1 &&  !ipPattern.test(currentDomain) && !devDomainPattern.test(currentDomain))
                {
                    currentDomain = lang + currentDomain.substr(currentDomain.indexOf("."));
                    window.location.href = "http://" + currentDomain +"?" + sCutLang + "&lang=" + lang;
                }
                else
                {
                     window.location.href = "http://" + currentDomain + "/?" + sCutLang + "&lang=" + lang;
                }
             }
        }
        else         
        {            
            if(currentDomain.indexOf(ItalyDomain) == -1 && currentDomain.indexOf("stage.") == -1 &&  !ipPattern.test(currentDomain) && !devDomainPattern.test(currentDomain))
            {
                // for live
                    if(lang=="en" || lang=="pt" || lang=="es")
                    {
                        currentDomain = "www" + currentDomain.substr(currentDomain.indexOf("."));
                        window.location.href = "http://" + currentDomain + "/?" + sCutLang + "&lang=" + lang;
                    }
                    else  if(lang=="it")
                    {
                        window.location.href = "http://" + ItalyDomain + "/?" + sCutLang;
                    }
                    else
                    {                   
                        currentDomain = lang + currentDomain.substr(currentDomain.indexOf("."));
                        window.location.href = "http://" + currentDomain +"?" + sCutLang +  "&lang=" + lang;
                    }
                    
            }
            else if(currentDomain.indexOf(ItalyDomain) > -1)
            {
                if (lang=="en" || lang=="pt" || lang=="es")
                    window.location.href = "http://www.888.com/?" + sCutLang +  "&lang=" + lang;
                else
                {
                    currentDomain = lang + ".888.com";
                    window.location.href = "http://" + currentDomain +"?" + sCutLang +  "&lang=" + lang;
                }
            }
  
            else
            {
                window.location.href = "http://" + currentDomain + "/?" + sCutLang + "&lang=" + lang;
            }

        }
        return;
    }
    else if (lang=="jp")
    {
              if(currentDomain.indexOf("stage.") == -1 &&  !ipPattern.test(currentDomain) && !devDomainPattern.test(currentDomain))
            {
                currentDomain = lang + ".888.com";
                window.location.href = "http://" + currentDomain +"?" + sCutLang + "&lang=" + lang;
            }
            else
            {
                 window.location.href = "http://" + currentDomain + "/?" + sCutLang + "&lang=" + lang;
            }
          return;
    }      
}

function dwnldCookie()
{
    try{
        if(document.cookie.indexOf("dwnld") != -1)
        {
            return true;
        }
        else
            return false;
    }
    catch(err)
    {
        return false;
    }
    return false;
}


function homeLink(src)
{
    window.onbeforeunload = function() {return;}       
    window.location.href = "/?" + sCut;
    return;
}

function ChangeCssClass(id)
{

    var mainDivObject = top.main.document.lastChild.lastChild.lastChild;
    mainDivObject.className='mainWithOpacity';
    var outFrame = document.getElementById('main');
    var mainInner = outFrame.contentWindow.document.getElementById('bodyElement');

    if(mainInner != null){
        mainInner.style.backgroundColor="gray";
    }

}

function openExitPage()
{      
    if("cz" == sLang){
        return;
    }
    if(!document.layers && !isDownload)
    {               
             if(FlagAr[3]=="2" && ("en"==sLang || "de"==sLang || "es"==sLang  || "fr"==sLang  || "nl"==sLang || "sv"==sLang || "da"==sLang  || "el"==sLang || "pl"==sLang || "ro"==sLang || "ru"==sLang || "bg"==sLang || "hu"==sLang  || "ar"==sLang || "mx"==sLang || "la"==sLang || "it"==sLang || "cns"==sLang) )
            {
                    var w = 770;
                    var h = 485;
                    var l = (window.screen.width-w)/2;
                    var t = (window.screen.height-h)/2;
                    window.open('new888/lang/' + sLang + '/texts/exitpage.htm?' + sCut,'ExitPage',"toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,height=" + h + ",width=" + w + ",left="+l+",top="+t);
            }
            else if (FlagAr[3]=="2" && "jp" == sLang)
            {
                    var w = 770;
                    var h = 485;
                    var l = (window.screen.width-w)/2;
                    var t = (window.screen.height-h)/2;
                    window.open('/jp/exitpage.htm?' + sCut,'ExitPage',"toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,height=" + h + ",width=" + w + ",left="+l+",top="+t);
            }	
            else if (FlagAr[3]=="2" && "tr" == sLang)
            {
                    var w = 770;
                    var h = 485;
                    var l = (window.screen.width-w)/2;
                    var t = (window.screen.height-h)/2;
                    window.open('new888/lang/en/texts/exitpage.htm?' + sCut,'ExitPage',"toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,height=" + h + ",width=" + w + ",left="+l+",top="+t);
            }
             else if(FlagAr[3]=="3")
            {
                    var w = 800;
                    var h = 600;
                    var l = (window.screen.width-w)/2;
                    var t = (window.screen.height-h)/2;
                    window.open('/exitpage.htm?' + sCut,'ExitPage',"toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,height=" + h + ",width=" + w + ",left="+l+",top="+t);
            }	
            else if(FlagAr[3]=="4" && "it" != sLang)
            {
                    var w = 788;
                    var h = 364;
                    var l = (window.screen.width-w)/2;
                    var t = (window.screen.height-h)/2;
                    window.open('http://www.littlewoodscasino.com/webpagesincludes/exit.htm?'+sCut,'ExitPage',"toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,height=" + h + ",width=" + w + ",left="+l+",top="+t);
            }
            else if (sLang !="jp" && sLang !="de" && sLang != "pl" && sLang !="bg" && sLang !="ro" && sLang !="hu"  && sLang !="it")
            { //jp site handles his flags
                if(FlagAr[3]!="1" && showCurHP == "true")
                {                              
                            if(sLang == 'en')
                                ChangeCssClass('main');
                           if(sLang == 'cns'){
                            sExit = "\u7ACB\u5373\u4E86\u89E3\u4E3A\u4EC0\u4E48\u4EBA\u4EBA\u90FD\u5728\u592A\u5E73\u6D0B\u6251\u514B\u4E0A\u73A9\u6251\u514B\u7684\u539F\u56E0\uFF01";
                           }
                           if(sLang == 'ru'){
                            sExit = "\u0423\u0437\u043D\u0430\u0439\u0442\u0435 \u043F\u0440\u044F\u043C\u043E \u0441\u0435\u0439\u0447\u0430\u0441, \u043F\u043E\u0447\u0435\u043C\u0443 \u0432\u0441\u0435 \u0438\u0433\u0440\u0430\u044E\u0442 \u0432 \u043F\u043E\u043A\u0435\u0440 \u043D\u0430 \u0441\u0430\u0439\u0442\u0435 Pacific poker!";
                           }
                           
                            if(confirm(sExit))
                            {   
                                sleep(1000);
                                if(sLang == 'en')
                                {      

                                    try {ntptEventTag("ev=confirm_casino_en_exit_message");} catch(err) {}                                                                                                                          
                                     window.open('http://www.pacificpoker.com/?sr=' + sSerial + '&s=' + sSession + '&bc=110&st=' + sSiteTest);
                                }
                                else if(sLang == 'lv')
                                {
                                    try {ntptEventTag("ev=confirm_casino_"+sLang+"_exit_message");} catch(err) {}
                                    window.open('http://lv.pacificpoker.com/?sr=' + sSerial + '&s=' + sSession + '&bc=110&st=' + sSiteTest);
                                }
                                else
                                {
                                    try {ntptEventTag("ev=confirm_casino_"+sLang+"_exit_message");} catch(err) {}
                                    NoDownUK('24');
                                }
                        }
                        else  
                        {   
                            sleep(1000);        
                            try {ntptEventTag("ev=close_casino_"+sLang+"_exit_message");} catch(err) {}                                    
                        }
                }
            }
            else if( sLang == "de" )
            { //de site handles his flags
               if(FlagAr[3] !="1")
               {    
                   if(confirm('No-Download Version ausprobieren und sofort spielen!'))
                   {
                       sleep(1000);  
                       try {ntptEventTag("ev=confirm_casino_"+sLang+"_exit_message");} catch(err) {}
                       NoDownUK('24');
                   }
                   else
                   {
                       sleep(1000); 
                       try {ntptEventTag("ev=close_casino_"+sLang+"_exit_message");} catch(err) {}                                        
                   }                   
              }
            }               
    }
}

function NoDownUK(location)
{    	
	var windowY = ((screen.availHeight-561)/2);
        var windowX = ((screen.width-804)/2);
        if(sIC !="0")
        {
		if(UKWindow){UKWindow.close();}
		UKWindow = window.open(ver + "/ukpopup.htm?" + sCut ,"Offline","width=795,height=545,left="+windowX+",top="+windowY+",resizable=no,status=no,scrollbars=no,toolbar=no,menubar=no");
        }
        else
        {       
            openGame(location);
        }
}

/*
this is the function that opens the game each url with its properties. 
for en: flash.888.com 
for de: quickplay.888.com
for it, fr, nl ,de, es: flash.888.com
*/

function openGame(location)
{
        var w = 796;
	var h = 550;
	
        var params = sCut + "&dl=" + location;
        
        if (sLang=="ar")      
               params = sCutLang + "&lang=es&dl=" + location;         
        else if (sLang=="br")
               params = sCutLang + "&lang=pt&dl=" + location; 
        else if (sLang=="de" ||  sLang=="it" ||  sLang=="fr")
               params =sLang+"&"+ sCutLang + "&dl=" + location; 
        else if (sLang=="es" || sLang=="mx" || sLang=="la")
               params ="es&"+ sCutLang + "&dl=" + location;
        else
                params ="en&"+ sCutLang + "&dl=" + location;
        
       var LS = "http://ndl.888.com/casino/application/ViewActual.html?mode=3&LangID=" + params;
       
        if (sLang=="jp")
            LS = "http://quickplay.888.com/start.asp?mode=1&" + params;        
        else if (sLang=="de" || sLang=="es" || sLang=="mx" || sLang=="la" || sLang=="it" || sLang=="fr")
            LS = "http://ndl.888.com/casino/application/ViewActual.html?mode=3&LangID=" + params;
        else
            LS = "http://ndl.888.com/casino/application/ViewActual.html?mode=3&LangID=" + params;
       
        var l = (window.screen.width-w)/2;
        var t = (window.screen.height-h)/2;
        window.open(LS,"Offline","resizable=no,status=no,scrollbars=no,toolbar=no,menubar=no,left=" + l + ",top=" + t + ",width=" + w + ",height=" + h);
}

//this function used to postpone the open of the result of the exit message
function sleep(milliseconds) 
{
  var start = new Date().getTime();
  for (var i = 0; i < 1e7; i++) 
  {
    if ((new Date().getTime() - start) > milliseconds){break;}
  }
}
