﻿
var InterCodeSelector = function(parent,onload)
{
    this.interCodeContainer = parent;
    this.onload = onload;
    this.interCodePanel = document.createElement("div");
    this.interCodePanel.style.position = "absolute";
    this.interCodePanel.style.background = "none";
    this.interCodePanel.style.visibility = "hidden";
    this.selectedInterCodeContainer = document.createElement("div");
    this.selectedInterCodeContainer.className="selectedInterCodeContainer";
    
    this.interCodeList = document.createElement("div");
    var body = document.getElementsByTagName("body")[0];
    var me = this;
    body.addEventListener("mouseup",function(){me.hide()},true);
    this.interCodeList.className = "interCodeList";
    this.interCodePanel.appendChild(this.interCodeList); 
    this.interCodePanel.appendChild(this.selectedInterCodeContainer);
    this.interCodes = new Array();

    parent.appendChild(this.interCodePanel);
    Emedia.getInterCodes(function(data)
    {
        me.interCodeList.innerHTML = "";
        var lines = data.split("\n");
        var column = document.createElement("div");
        for(i=0;i<lines.length-1;i++)
        {
            if((i%8)==0)
            {
                //if(i>0)interCodeList.innerHTML+="</div>";
                //interCodeList.innerHTML+="<div class=\"interCodeColumn\">";
                column.className="interCodeColumn";
                me.interCodeList.appendChild(column);
                column = document.createElement("div");
            }
            var line = lines[i];
            var elements = line.split("|");
            me.interCodes[parseInt(elements[0])] = elements[2];
            column.innerHTML += "<a href=\"javascript:;\" title=\""+
                elements[1]+"\" onclick=\"interCodeSelector.select("+elements[0]+
                ");return false;\"><img align=\"absmiddle\" src=\"images/flags/"+elements[0]+
                ".png\" border=\"0\" alt=\""+elements[1]+"\" />"+elements[2]+"</a>";
        }
        me.interCodeList.innerHTML+="<div class=\"separator\"></div>";
        if(me.onload!=null)me.onload();
    });
}

InterCodeSelector.prototype.defaultCountry = 0;
InterCodeSelector.prototype.flag = null;
InterCodeSelector.prototype.text = null;
InterCodeSelector.prototype.selectedInterCode = 0;
InterCodeSelector.prototype.visible =false;

InterCodeSelector.prototype.show = function()
{
    if(this.visible)return;
    this.interCodePanel.style.visibility = "visible";
    ie6SelectBug(false);
}

InterCodeSelector.prototype.hide = function()
{
    this.interCodePanel.style.visibility = "hidden";
    this.visible = false;
    ie6SelectBug(true);
}

InterCodeSelector.prototype.select = function(flagid)
{
    this.selectedInterCode = flagid;
    //this.hide();
    if(this.flag!=null && this.text!=null)
    {
        this.interCodeContainer.removeChild(this.flag);
        this.interCodeContainer.removeChild(this.text);
    }
    else
    {
        this.flag = document.createElement("img");
        this.text=  document.createTextNode(" ");
    }   
    this.flag.width = "16";
    this.flag.height = "11";
    this.flag.align = "absmiddle";
    this.flag.src = "images/flags/"+flagid+".png";
    this.text.data = this.interCodes[flagid];
    /*this.flag = "<img src=\"images/flags/"+flagid+
        ".png\" width=\"16\" height=\"11\" align=\"absmiddle\" /> "+
        this.interCodes[flagid];*/
    this.interCodeContainer.appendChild(this.flag);
    this.interCodeContainer.appendChild(this.text);
    //this.interCodeContainer.innerHTML = this.flag;
    this.selectedInterCodeContainer.innerHTML = "<img src=\"images/flags/"+flagid+
        ".png\" width=\"16\" height=\"11\" align=\"absmiddle\" /> "+
        this.interCodes[flagid];//this.interCodeContainer.innerHTML;
}

function ie6SelectBug(visible)
{
    if(typeof(userMode)=="undefined")return;
    if(!userMode /*DEFINED IN CLIENTINFOS.ASPX*/)
    {
        //sp ClientInfos.aspx /!\ 
        //ces lignes corrigent un bug nul de IE6 
        //qui n'arrive pas gérer la superposition d'éléments
        //HTML sur un select...
        var iCountry = document.getElementById("iCountry");
        var iCity = document.getElementById("iCity");
        iCountry.style.visibility = visible?"visible":"hidden";
        iCity.style.visibility = visible?"visible":"hidden";
    }
}