var Flags = { 
    curtain: null, 
    dialog: null,  
    positionInterval: null, 
    languagesText: "Languages", 

    ShowLanguagesDialog: function() { 
        Flags.HideLanguagesDialog();
        
         var region = ReadCookie("regioncode");
	    if (region == null || region.length <= 0) region = "us";
        
        var dropbox = Get("EntityDropbox");
        Flags.curtain = document.createElement("div");
        Flags.dialog = document.createElement("div");
        
        var dialog1 = document.createElement("div");
        var dialog2 = document.createElement("div");
        var dialog3 = document.createElement("div");
        var dialog4 = document.createElement("div");
        var dialog5 = document.createElement("div");
        
        var pageWidth = document.body.clientWidth;
        var pageHeight = (document.height != undefined) ? document.height : document.body.offsetHeight;
        if (document.documentElement.clientHeight > pageHeight) pageHeight = document.documentElement.clientHeight;
        if (pageWidth < 980) pageWidth = 980;

        Flags.curtain.style.position = "absolute";
        Flags.curtain.style.width = pageWidth + "px";
        Flags.curtain.style.height = pageHeight + "px";
        Flags.curtain.style.zIndex = 610;
        Flags.curtain.style.backgroundColor = "#252038";
	    Flags.curtain.style.opacity = 0.85;
	    Flags.curtain.style.filter  = "alpha(opacity=85)";
        Flags.curtain.innerHTML = "&nbsp";

        Flags.dialog.style.position = "absolute";
        Flags.dialog.style.width = "575px";
        Flags.dialog.style.height = "300px";
        Flags.dialog.style.zIndex = 611;
        Flags.dialog.className = "Dialog";
        
        dialog1.className = "Dialog-tl";
        dialog2.className = "Dialog-tr";
        dialog3.className = "Dialog-bl";
        dialog4.className = "Dialog-br";
        dialog5.className = "Dialog-padding";
        
        Flags.Position();
        Flags.positionInterval = setInterval(function(){ Flags.Position(); }, 500);
        
        var heading = document.createElement("h2");
        heading.appendChild(document.createTextNode("Metrodate.com " + Flags.languagesText));
        
        var closeLinkContainer = document.createElement("div");
        var closeLinkElem = document.createElement("a");
        if (typeof(closeLinkContainer.style.cssFloat) != 'undefined') closeLinkContainer.style.cssFloat = "right";
        else closeLinkContainer.style.styleFloat = "right";
        closeLinkContainer.style.margin = "-10px -30px 0 0";
        closeLinkElem.className = "CloseLink";
        closeLinkElem.onclick = function() { Flags.HideLanguagesDialog(); return false; };
        closeLinkElem.href = "#";
        closeLinkElem.appendChild(document.createTextNode(Translation.Close));
        closeLinkContainer.appendChild(closeLinkElem);
        
        var list = document.createElement("table");
        list.cellspacing = "0";
        list.cellpadding = "0";
        list.className = "LanguagesList";
        
        var row1 = list.insertRow(list.rows.length);
        var row2 = list.insertRow(list.rows.length);
        var row3 = list.insertRow(list.rows.length);
        var row4 = list.insertRow(list.rows.length);
        var row5 = list.insertRow(list.rows.length);
        var row6 = list.insertRow(list.rows.length);
        var row7 = list.insertRow(list.rows.length);
        var row8 = list.insertRow(list.rows.length);
        var row9 = list.insertRow(list.rows.length);
        var row10 = list.insertRow(list.rows.length);
        var cell = null;
        
        cell = row1.insertCell(row1.cells.length);
        if (region == "us" || region == "en-us") cell.appendChild(Flags.GetLanguageListing("us", "English"));
        else cell.appendChild(Flags.GetLanguageListing("gb", "English"));
        cell.style.borderBottom = "1px solid #DBDAFC";
        cell.style.paddingBottom = "5px";
        cell.style.marginBottom = "2px";
        cell = row1.insertCell(row1.cells.length);
        cell.appendChild(Flags.GetLanguageListing("es", "Espa&ntilde;ol"));
        cell.style.borderBottom = "1px solid #DBDAFC";
        cell.style.paddingBottom = "5px";
        cell.style.marginBottom = "2px";
        cell = row1.insertCell(row1.cells.length);
        cell.appendChild(document.createTextNode(" "));
        cell.style.borderBottom = "1px solid #DBDAFC";
        cell.style.paddingBottom = "5px";
        cell.style.marginBottom = "2px";
        cell = row1.insertCell(row1.cells.length);
        cell.appendChild(document.createTextNode(" "));
        cell.style.borderBottom = "1px solid #DBDAFC";
        cell.style.paddingBottom = "5px";
        cell.style.marginBottom = "2px";
        
        cell = row2.insertCell(row2.cells.length);
        cell.appendChild(Flags.GetLanguageListing("af", "Afrikaans"));
        cell = row2.insertCell(row2.cells.length);
        cell.appendChild(Flags.GetLanguageListing("ar", "&#1575;&#1604;&#1604;&#1594;&#1577; &#1575;&#1604;&#1593;&#1585;&#1576;&#1610;&#1577;"));
        cell = row2.insertCell(row2.cells.length);
        cell.appendChild(Flags.GetLanguageListing("id", "Bahasa Indonesia"));
        cell = row2.insertCell(row2.cells.length);
        cell.appendChild(Flags.GetLanguageListing("bg", "&#1041;&#1098;&#1083;&#1075;&#1072;&#1088;&#1089;&#1082;&#1080; &#1077;&#1079;&#1080;&#1082;"));
        cell = row3.insertCell(row3.cells.length);
        cell.appendChild(Flags.GetLanguageListing("zh", "&#20013;&#25991;"));
        cell = row3.insertCell(row3.cells.length);
        cell.appendChild(Flags.GetLanguageListing("cs", "&#269;e&#353;tina"));
        cell = row3.insertCell(row3.cells.length);
        cell.appendChild(Flags.GetLanguageListing("fa", "&#1583;&#1585;&#1740;"));
        cell = row3.insertCell(row3.cells.length);
        cell.appendChild(Flags.GetLanguageListing("da", "Dansk"));
        cell = row4.insertCell(row4.cells.length);
        cell.appendChild(Flags.GetLanguageListing("de", "Deutsch"));
        cell = row4.insertCell(row4.cells.length);
        cell.appendChild(Flags.GetLanguageListing("et", "Eesti keelde"));
        cell = row4.insertCell(row4.cells.length);
        cell.appendChild(Flags.GetLanguageListing("fr", "Fran&#231;ais"));
        cell = row4.insertCell(row4.cells.length);
        cell.appendChild(Flags.GetLanguageListing("fi", "Suomi"));
        cell = row5.insertCell(row5.cells.length);
        cell.appendChild(Flags.GetLanguageListing("el", "&#949;&#955;&#955;&#951;&#957;&#953;&#954;&#942; &#947;&#955;&#974;&#963;&#963;&#945;"));
        cell = row5.insertCell(row5.cells.length);
        cell.appendChild(Flags.GetLanguageListing("he", "&#1506;&#1489;&#1512;&#1497;&#1514;"));
        cell = row5.insertCell(row5.cells.length);
        cell.appendChild(Flags.GetLanguageListing("hi", "Hind&#299;"));
        cell = row5.insertCell(row5.cells.length);
        cell.appendChild(Flags.GetLanguageListing("hr", "Hrvatski"));
        cell = row6.insertCell(row6.cells.length);
        cell.appendChild(Flags.GetLanguageListing("hu", "Magyar"));
        cell = row6.insertCell(row6.cells.length);
        cell.appendChild(Flags.GetLanguageListing("it", "Italiano"));
        cell = row6.insertCell(row6.cells.length);
        cell.appendChild(Flags.GetLanguageListing("ja", "&#26085;&#26412;&#35486;"));
        cell = row6.insertCell(row6.cells.length);
        cell.appendChild(Flags.GetLanguageListing("lt", "Lietuvi&#371; Kalb&#261;"));
        cell = row7.insertCell(row7.cells.length);
        cell.appendChild(Flags.GetLanguageListing("mt", "Malti"));
        cell = row7.insertCell(row7.cells.length);
        cell.appendChild(Flags.GetLanguageListing("nl", "Nederlands"));
        cell = row7.insertCell(row7.cells.length);
        cell.appendChild(Flags.GetLanguageListing("no", "Norsk"));
        cell = row7.insertCell(row7.cells.length);
        cell.appendChild(Flags.GetLanguageListing("pl", "Polski"));
        cell = row8.insertCell(row8.cells.length);
        cell.appendChild(Flags.GetLanguageListing("pt", "Portuguesa"));
        cell = row8.insertCell(row8.cells.length);
        cell.appendChild(Flags.GetLanguageListing("ro", "Romana"));
        cell = row8.insertCell(row8.cells.length);
        cell.appendChild(Flags.GetLanguageListing("ru", "&#1056;&#1091;&#1089;&#1089;&#1082;&#1080;&#1081; &#1103;&#1079;&#1099;&#1082;"));
        cell = row8.insertCell(row8.cells.length);
        cell.appendChild(Flags.GetLanguageListing("sl", "Sloven&#353;&#232;ina"));
        cell = row9.insertCell(row9.cells.length);
        cell.appendChild(Flags.GetLanguageListing("sv", "Svenska"));
        cell = row9.insertCell(row9.cells.length);
        cell.appendChild(Flags.GetLanguageListing("sk", "Sloven&#269;ina"));
        cell = row9.insertCell(row9.cells.length);
        cell.appendChild(Flags.GetLanguageListing("sr", "Srpski"));
        cell = row9.insertCell(row9.cells.length);
        cell.appendChild(Flags.GetLanguageListing("tr", "T&#252;rk&#231;e"));
        cell = row10.insertCell(row10.cells.length);
        cell.appendChild(Flags.GetLanguageListing("ur", "&#1575;&#1615;&#1585;&#1583;&#1608;"));
        cell = row10.insertCell(row10.cells.length);
        cell.appendChild(Flags.GetLanguageListing("vi", "Ti&#7871;ng Vi&#7879;t"));
        
        dialog5.appendChild(closeLinkContainer);
        dialog5.appendChild(heading);
        dialog5.appendChild(list);
        
        dialog4.appendChild(dialog5);
        dialog3.appendChild(dialog4);
        dialog2.appendChild(dialog3);
        dialog1.appendChild(dialog2);
        Flags.dialog.appendChild(dialog1);
        
        dropbox.appendChild(Flags.curtain);
        dropbox.appendChild(Flags.dialog);
        dropbox = null;
    }, 
    
    HideLanguagesDialog: function() { 
        var dropbox = Get("EntityDropbox");
        
        clearInterval(Flags.positionInterval);
        
        if (Flags.dialog) { try { dropbox.removeChild(Flags.dialog); } catch(e) {  } };
        if (Flags.curtain) { try { dropbox.removeChild(Flags.curtain); } catch(e) {  } };
        
        dropbox = null;
    }, 
    
    GetLanguageListing: function(langCode, langName) { 
        var wrapper = document.createElement("div");
        var link = document.createElement("a");
        link.href = "/languages/?langcode=" + langCode + "&redir=true";
        link.innerHTML = langName;
        wrapper.appendChild(link);
        return wrapper;
    }, 
    
    Position: function() { 
        Flags.dialog.style.left = GetScreenCenteredX(575) + "px";
        Flags.dialog.style.top = GetScreenCenteredY(300) + "px";
    }
};