
/* auto set close overall */

window.onload = function () { document.getElementById('wrapper').onclick = selectHidePulldown; };

/* select vars */

var select_current_select_id = 0;
var select_current_option_id = new Array();
var select_block             = false;

/* select functions */

function selectHidePulldown()
{
    var objPulldown = document.getElementById('select_pulldown_' + select_current_select_id );
    
    if (objPulldown) {
        if (select_block) {
            select_block = false;
        } else {
            select_current_select_id  = 0;
            objPulldown.style.display = 'none';
        }  
    }   
}

function selectShowPulldown(select_id)
{  
    var objCurrent  = document.getElementById('select_current_' + select_id);
    var objPulldown = document.getElementById('select_pulldown_' + select_id);
    
    if (!objCurrent || !objPulldown) {
        return false;
    }
    
    // close last pulldown
    if (select_current_select_id) {
        var objPrev = document.getElementById('select_pulldown_' + select_current_select_id);
        if (objPrev) {
            if (select_current_select_id && select_current_select_id == select_id) {
                select_current_select_id  = 0;
                objPrev.style.display = 'none';
                return false;
            } else {
                select_current_select_id  = 0;
                objPrev.style.display = 'none';
            }
        }
    }

    objPulldown.style.left    = (showPosLeft(objCurrent.id))                          + 'px';
    objPulldown.style.top     = (showPosTop(objCurrent.id) + objCurrent.offsetHeight) + 'px';
    objPulldown.style.display = 'block';
    objPulldown.style.width   = objCurrent.offsetWidth + 'px';
    
    select_current_select_id  = select_id;
    select_block              = true;
}

function selectChangeOption(select_id, option_id, select_name, select_form, submit)
{
    var objContent = document.getElementById('select_content_' + select_id);
    var objOption  = document.getElementById('select_option_' + select_id + '_' + option_id);
    
    if (!objContent || !objOption) {
        return false;
    }
    
    // unmark last option
    if (select_current_option_id) {
        var objPrev = document.getElementById('select_option_' + select_id + '_' + select_current_option_id[select_id]);
        if (objPrev) {
            objPrev.className = '';
        }
    }

    objOption.className  = 'active';
    objContent.innerHTML = objOption.innerHTML;
    
    select_current_option_id[select_id] = option_id;
    
    // optional form
    if (select_name && select_form) {
        if (document.forms[select_form] && document.forms[select_form].elements[select_name]) {
            document.forms[select_form].elements[select_name].value = option_id;
            if (submit) {
                document.forms[select_form].submit();
            }
        } else {
            alert('error!')
        }
    }
}

/* default functions required (optional load) */

if (!window.showPosLeft) { // function not exists
    function showPosLeft(id) 
    {
        x   = 0;
        pos = document.getElementById(id);
        x   = pos.offsetLeft;
        while (pos.offsetParent != null) {
            pos = pos.offsetParent;
            if (pos.style.position == 'absolute') {
                break;
            }
            x   = x + pos.offsetLeft;
        }
        return x;
    }
}

if (!window.showPosTop) { // function not exists
    function showPosTop(id) 
    {
        y   = 0;
        pos = document.getElementById(id);
        y   = pos.offsetTop;
        while (pos.offsetParent != null) {
            pos = pos.offsetParent;
            if (pos.style.position == 'absolute') {
                break;
            }
            y   = y + pos.offsetTop;
        }
        return y;
    }
}