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)

{

    if(GetHostFromURL(site) !== GetHostFromURL(window.parent.location.href))

        window.setTimeout("window.parent.location.href = '" + site + "';", 500);



    window.parent.HideSiteSelecter();

}



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/redirect.php?source=" + GetHostFromURL(window.parent.location.href) + "' 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 GetHostFromURL(url)

{

    if(url.indexOf(sGlobalSiteURL) >= 0)

        return sGlobalSiteURL;



    if(url.indexOf(sSeparatorSiteURL) >= 0)

        return sSeparatorSiteURL;



    if(url.indexOf(sLocalSiteURL) >= 0)

        return sLocalSiteURL;



    return url;

}



function GetURL(url)

{

    var idx = url.indexOf('?');



    if(idx < 0)

        return url;



    return url.substring(0, idx);

}



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;

}



function getHostFromUrl(url)

{

    var obj = new RegExp('^(?:f|ht)tp(?:s)?\://([^/]+)', 'im');

    return url.match(obj)[1].toString();

}


