function copyFunction(func, arg)
{
 return function()
 {
 func(arg);
 }
}


var Opinion_SELECT_ID_NAME = 'categorySELECT_';
var Opinion_DIV_ID_NAME = 'categoryDIV_';
var Opinion_initContendIDs = new Object();

function clearSelect(select) {
    while (select.options.length > 0)
        select.options[0] = null;
    // lerejti vagy megjeleniti azt amit kell
    id = select.id.split('_')[1];

    DIV = document.getElementById(Opinion_DIV_ID_NAME + id);
    if (DIV.style.display == 'none') {
        //DIV.style.display = 'block';
    }
    else {
        for (var i = parseInt(id); i < 4; i++) {
            subDIV = document.getElementById(Opinion_DIV_ID_NAME + i);
            subDIV.style.display = 'none';
            
            removeSelect(Opinion_SELECT_ID_NAME + i + '_js');
        }   
    }
}


function Opinion_updateSelect(select, contentid) {
    clearSelect(select);
    select.onchange = Opinion_onSelectionChanged;
    onReady = function(request) {
        xml = request.responseXML;
        types = xml.getElementsByTagName('type');

        if(types.length > 1)
        {
            for (var i = 0; i < types.length; i++) {
                attributes = types[i].attributes;
                oValue = null;
                oText = null;
                for (a = 0; a < attributes.length; a++) {
                    if (attributes[a].name == 'contentid')
                        oValue = attributes[a].value;
                    else if (attributes[a].name == 'name')
                        oText = attributes[a].value;
                }
                
                select.options[i] = new Option(oText, oValue);
            }
            
            id = parseInt(select.id.split('_')[1]);
            // ha meg van init contentid, akkor arra meghivja a beallitast
            if(Opinion_initContendIDs[id] != null && Opinion_initContendIDs[id] != undefined)
            {           
                contentid = Opinion_initContendIDs[id];
                Opinion_initContendIDs[id] = null;
                   // ha az elso, akkor csak az erteket kell beallitani
                   Opinion_setSelectedOption(select, contentid, id);
            }
            id = parseInt(select.id.split('_')[1]);
            subDIV = document.getElementById(Opinion_DIV_ID_NAME + id);
            subDIV.style.display = 'block';
            
            select.setAttribute('name', select.id);
            
            new jsselect(select.id, select.id + '_js');
        }
        else
        {
            id = parseInt(select.id.split('_')[1]);
            subDIV = document.getElementById(Opinion_DIV_ID_NAME + id);
            subDIV.style.display = 'none';
        }
    }
    req = ponte.Services.AjaxService.createRequest(Opinion_URL + '&contentid=' + contentid, onReady, null, 'GET', null);
    req.makeRequest(null);
}

function Opinion_setSelectedOption(select, contentid, id)
{
    for(var o = 0; o < select.options.length; o++)
    {
        opt = select.options[o];
        if(opt.value == contentid)
        {
            select.selectedIndex = o;
            document.getElementById(Opinion_DIV_ID_NAME + id).style.display='block';
            //Opinion_updateSelect(document.getElementById(Opinion_SELECT_ID_NAME + (id + 1)), contentid);
        }
    }
    Opinion_updateSearchText();
}

function Opinion_onSelectionChanged(select) {
    contentid = select.value;
    id = parseInt(select.id.split('_')[1]) + 1;
    select = document.getElementById(Opinion_SELECT_ID_NAME + id);
    if(select != null)
    {
        Opinion_updateSelect(select, contentid);
    }
    Opinion_updateSearchText();
}

function Opinion_onLoad() {
    // beallitja a keresett ertekre
    contentIDs = Opinion_selectedContents.split(';');

    for(var i = 0; i < contentIDs.length; i++)
    {
        if(contentIDs[i].length > 0)
            Opinion_initContendIDs[i + 1] = contentIDs[i];
    }       
    Opinion_updateSelect(document.getElementById(Opinion_SELECT_ID_NAME + '1'), Opinion_rootContentID);
}

function Opinion_updateSearchText()
{
    searchText = '';
    selectedContents = '';
    for(var i = 0; i < 4; i++)
    {
        select = document.getElementById(Opinion_SELECT_ID_NAME + i);
        try{
            selected = select.options[select.selectedIndex];
            if(selected.value != null && selected.value.length > 0)
            {
                searchText = selected.value;
                selectedContents += selected.value + ';';
            }
        }catch(e){}
    }
    // ha nincs kivalasztva semmi, akkor a root-ot teszi bele
    if(searchText.length == 0)
        searchText = Opinion_rootContentID;

    document.getElementById('heading_input').setAttribute('value', searchText);
    document.getElementById('selectedcontents').setAttribute('value', selectedContents);
}

addLoadEvent(Opinion_onLoad);


/**
JavaScript based SELECT tag

JSSelect
*/

jsOptions = new Array();

function jsselect(selectNodeID, selectID)
{
try{
    removeSelect(selectID);

    this.options = new Array();
    
    this.selectNode = document.getElementById(selectNodeID);
       
    var canvas = document.createElement('div');   
    canvas.id = selectID;
    this.node = document.createElement('div');
    this.node.style.display = 'block';
    this.node.style.position = 'relative';
    this.node.style.width = '100%';
    this.node.style.height = '100%';
    this.node.onmouseover = copyFunction(this.onmouseover, this);
    this.node.onmouseout = copyFunction(this.onmouseout, this);
    
    this.selectedTextNode = document.createElement('div');
    this.selectedTextNode.setAttribute('style','float:left;cursor:default;padding-left:3px;');
    this.selectedTextNode.style.border = '1px solid #ABADB3';
    this.selectedTextNode.style.cursor = 'default';
    this.selectedTextNode.style.styleFloat = 'left';
    this.selectedTextNode.style.paddingLeft = '3px';
    
    if(this.selectNode.style.width != undefined)
        this.selectedTextNode.style.width = (parseInt(this.selectNode.style.width) - 20) + 'px';
    this.selectedTextNode.onclick = copyFunction(this.expand, this);
    this.node.appendChild(this.selectedTextNode);
    
    this.expandNode = document.createElement('div');
    this.expandNode.setAttribute('style','width:18px;cursor:default;height:20px;float:left;background:transparent url(/sites/pszaf/images/select_out.png) no-repeat scroll left bottom');
    this.expandNode.style.width = '18px';
    this.expandNode.style.cursor = 'default';
    this.expandNode.style.height = '20px';
    this.expandNode.style.styleFloat = 'left';
    this.expandNode.style.background = 'transparent url(/sites/pszaf/images/select_out.png) no-repeat scroll left bottom';
    this.expandNode.onclick = copyFunction(this.expand, this);
    this.node.appendChild(this.expandNode);
    
    this.optionsNode = document.createElement('div');

    this.optionsNode.style.position = 'absolute';
    this.optionsNode.style.border = '1px solid gray';
    this.optionsNode.style.display = 'none';
    this.optionsNode.style.top = '20px';
    this.optionsNode.style.left = '0px';
    this.optionsNode.style.background = 'white';
    this.optionsNode.style.zIndex = '1000';
    if(this.selectNode.style.width != undefined)
        this.optionsNode.style.width = (parseInt(this.selectNode.style.width) - 20) + 'px';
    this.node.appendChild(this.optionsNode);
        
    for(var i = 0; i < this.selectNode.options.length; i++)
    {
        var option = this.selectNode.options[i];
        var jsOption = this.addOption(option.innerHTML, option.value);
        if(i == this.selectNode.selectedIndex)
            this.setSelectedValue(jsOption);
    }    
    this.selectNode.style.display = 'none';
    canvas.appendChild(this.node)
    this.selectNode.parentNode.appendChild(canvas);
    
    jsOptions.push(this);
}catch(e){alert(e);};
    return this;   
}

removeSelect = function(selectID)
{
    try{
        var jsSelect = document.getElementById(selectID);
        if(jsSelect != null)
        {
            jsSelect.parentNode.removeChild(jsSelect);     
        }
    }catch(e){alert(e);}
}

jsselect.prototype.onmouseover = function(select)
{

    select.expandNode.style.background = 'transparent url(/sites/pszaf/images/select_over.png) no-repeat scroll left bottom';
    select.selectedTextNode.style.border = '1px solid #C7E2F1';
}

jsselect.prototype.onmouseout = function(select)
{
    select.expandNode.style.background = 'transparent url(/sites/pszaf/images/select_out.png) no-repeat scroll left bottom';
    select.selectedTextNode.style.border = '1px solid #ABADB3';
}

jsselect.prototype.addOption = function(text, value)
{
    var option = new jsoption(text, value, this.onSelectionChanged, this);
    this.options.push(option);
    this.optionsNode.appendChild(this.options[this.options.length - 1].node);
    return option;
};
    
jsselect.prototype.onSelectionChanged = function(option)
{
    option.select.setSelectedValue(option);
};

jsselect.prototype.expand = function(select)
{
    if(select.optionsNode.style.display == 'none')
    {
        for(var i = 0; i < jsOptions.length; i++)
        {
            if(select != jsOptions[i])
                jsOptions[i].collapse(jsOptions[i]);
        }
        select.optionsNode.style.display = 'block';
        select.node.style.position = 'relative';        
    }
    else
    {
       select.optionsNode.style.display = 'none';
       select.node.style.position = '';
    }
};

jsselect.prototype.collapse = function(select)
{
    select.optionsNode.style.display = 'none';
    select.node.style.position = '';
};

jsselect.prototype.setSelectedValue = function(option)
{
    this.selectedTextNode.innerHTML = option.text;
    this.collapse(this);
    for(var i = 0; i < this.selectNode.options.length; i++)
    {
        var optionE = this.selectNode.options[i];
        if(optionE.value == option.value)
        {
            this.selectNode.selectedIndex = i;
            break;
        }
    }
    this.selectNode.onchange(this.selectNode);
}

var jsoption = function(optionText, optionValue, onSelectionChanged, select)
{
    this.select = select;
    this.selected = false;
    this.text = optionText;
    this.value = optionValue;
    this.node = document.createElement('div');
    this.node.onmouseover = copyFunction(this.onmouseover, this);
    this.node.onmouseout = copyFunction(this.onmouseout, this);
    this.node.setAttribute('style', 'cursor:default; background:white; padding-bottom:3px; padding-left:3px; padding-right:3px;');
    this.node.style.cursor = 'default';
    this.node.style.background = 'white';
    this.node.style.paddingBottom = '3px';
    this.node.style.paddingLeft = '3px';
    this.node.style.paddingRight = '3px';
    this.node.style.zIndex = 1000;
    this.node.innerHTML = optionText;
    this.node.onclick = copyFunction(onSelectionChanged, this);
}

jsoption.prototype.onmouseover = function(option)
{
    option.node.style.background = '#3399FF';
    option.node.style.color = 'white';
}

jsoption.prototype.onmouseout = function(option)
{
    option.node.style.background = 'white';
    option.node.style.color = 'black';
}
