var sSeparatorSiteURL = "http://www.trilogycomms.com";

var sLocalSiteURL     = "http://www.trilogyus.com/";
var sGlobalSiteURL    = "http://www.trilogycomms.com/nc2_row/";


var sSiteCookieName = "CurrentSelectedSite";

var sIdPopupDialog     = "idPopupDialog";
var sIdPopupBackground = "idPopupBackground";

var dlgWidth  = 500;
var dlgHeight = 301;

function CheckCookie()
{
    var sSiteSelected = GetCookie(sSiteCookieName);

    if(sSiteSelected)
        GotoSite(sSiteSelected);
}

function GotoSite(site)
{
    var oSaveChoice = document.getElementById("idSaveChoice");

    if(oSaveChoice)
    {
        if(oSaveChoice.checked)
            SetCookie(sSiteCookieName, site, 365000);
        else
            ClearSiteCookie();

        window.setTimeout("window.parent.location.href = '" + site + "';", 500);

        window.parent.HideSiteSelecter();
    }
    else
        window.parent.location.href = site;
}

function ShowSiteSelecter()
{
    var xPos = (GetWidth() - dlgWidth)/2;
    var yPos = (GetHeight() - dlgHeight)/2;

    var oBackground = document.getElementById(sIdPopupBackground);

    if(oBackground == null)
    {
        document.body.innerHTML += "<div id='"+sIdPopupBackground+"' style='display: none; position: absolute; left: 0; top: 0; width: 100%; height: " + GetHeight() + "px; background: #000; filter: alpha(opacity=80); -moz-opacity: 0.8; -khtml-opacity: 0.8; opacity: 0.8; z-index: 10;' onClick='HideSiteSelecter();'>&nbsp;</div>";
        oBackground = document.getElementById(sIdPopupBackground);
    }

    var oDialog = document.getElementById(sIdPopupDialog);

    if(oDialog == null)
    {
        document.body.innerHTML += "<div id='"+sIdPopupDialog+"' style='display: none; position: absolute; left: " + xPos + "px; top: " + yPos + "px; height: " + dlgHeight + ";  width: " + dlgWidth + "; z-index: 20;'></div>";
        oDialog = document.getElementById(sIdPopupDialog);
    }

    oDialog = document.getElementById(sIdPopupDialog);

    var sDlgHTML = "<iframe src='" + sSeparatorSiteURL + "/country_selector/' width='" + dlgWidth + "' height='" + dlgHeight + "' frameborder='0'></iframe>";

    oDialog.innerHTML = sDlgHTML;

    if(document.getElementById(sIdPopupDialog))
        $( '#' + sIdPopupDialog ).fadeIn("slow");

    if(document.getElementById(sIdPopupBackground))
        $( '#' + sIdPopupBackground ).fadeIn("slow");

    UpdatePopupLocation();
}

function HideSiteSelecter()
{
    if(document.getElementById(sIdPopupBackground))
        $( '#' + sIdPopupBackground ).fadeOut("slow");

    if(document.getElementById(sIdPopupDialog))
        $( '#' + sIdPopupDialog ).fadeOut("slow");
}

function UpdatePopupLocation()
{
    var oDialog = document.getElementById(sIdPopupDialog);

    if(oDialog != null)
    {
        oDialog.style.left = "" + (GetWidth() - dlgWidth)/2 + "px";
        oDialog.style.top  = "" + (GetHeight() - dlgHeight)/2 + "px";
    }

    var oBackground = document.getElementById(sIdPopupBackground);

    if(oBackground)
    {
        oBackground.style.width  = GetWidth();
        oBackground.style.height = GetHeight();
    }
}

function ClearSiteCookie()
{
    SetCookie(sSiteCookieName, "", -1);
}

function SetCookie(sName, sValue, nTime)
{
    var time = new Date();
    time.setDate(time.getDate() + nTime);
    var sValue = escape(sValue) + ((time == null) ? "" : "; path=/; expires=" + time.toUTCString());

    document.cookie = sName + "=" + sValue;
}

function GetCookie(sName)
{
    var oCookies = document.cookie.split(";");

    for(var i = 0; i < oCookies.length; i++)
    {
        var tName  = oCookies[i].substr(0, oCookies[i].indexOf("="));
        var sValue = oCookies[i].substr(oCookies[i].indexOf("=") + 1);

        tName=tName.replace(/^\s+|\s+$/g,"");

        if (tName == sName)
            return unescape(sValue);
    }

    return null;
}

function GetWidth()
{
    if(document.all)
        return document.documentElement.clientWidth;
    else
        return window.innerWidth;
}

function GetHeight()
{
    if(document.all)
        return document.documentElement.clientHeight;
    else
        return window.innerHeight;
}

