﻿function tamingselect(p_dropDownListID, p_scrollRecCount, p_widthPoint) {
    if (!document.getElementById && !document.createTextNode) { return; }
    
    // Classes for the link and the visible dropdown
    var ts_selectclass = 'turnintodropdown'; 	// class to identify selects
    var ts_listclass = 'turnintoselect'; 	// class to identify ULs

    var ts_boxclass = 'dropcontainer'; 		// parent element
    var ts_triggeron = 'activetrigger'; 		// class for the active trigger link
    var ts_triggeroff = 'trigger'; 		// class for the inactive trigger link
    var ts_triggerBtnoff = 'triggerBtn'; 		// class for the inactive trigger link
    var ts_dropdownclosed = 'dropdownhidden'; // closed dropdown
    var ts_dropdownopen = 'dropdownvisible'; // open dropdown
    var ts_table = 'dropdownTable';
    var ts_cell = 'dropdownCell';
    var isOnContainer = false;
    /*
    Turn all selects into DOM dropdowns
    */
    var count = 0;
    var toreplace = new Array();
    var sel = document.getElementById(p_dropDownListID);
    var maxItemLength = 0;

    if (ts_check(sel, ts_selectclass)) {
        var hiddenfield = document.createElement('input');
        hiddenfield.name = sel.name;
        hiddenfield.type = 'hidden';
        hiddenfield.id = sel.id;
        hiddenfield.value = sel.options[0].value;
        sel.parentNode.insertBefore(hiddenfield, sel)

        var trigger = document.createElement('a');
        ts_addclass(trigger, ts_triggeroff);
        trigger.href = '#';
//        $(trigger).css('height', '22px');
 //       $(trigger).css('padding-top', '3px').css('padding-left', '5px').css('margin', '0px 0px 0px 2px');
        trigger.onclick = function() {
            f_closeAllDropDown(this);
            ts_swapclass(this, ts_triggeroff, ts_triggeron)
            ts_swapclass(this.parentNode.parentNode.parentNode.parentNode.parentNode.getElementsByTagName('ul')[0], ts_dropdownclosed, ts_dropdownopen);
            return false;
        }

        trigger.onblur = function() {
            $(this).next()
            if (!isOnContainer) {
                f_closeAllDropDown(null);
            }
        }
        var triggerBtn = document.createElement('div');
        var arrowImgUrl = g_root + 'image/arrow.gif';
        ts_addclass(triggerBtn, ts_triggerBtnoff);    
        
        $(triggerBtn).css('background', 'url("' + arrowImgUrl + '")');        
        $(triggerBtn).css('background-repeat', 'no-repeat');
//        $(triggerBtn).css('height', '22px');
        
        triggerBtn.onclick = function() {
            f_closeAllDropDown($(this).parents('tr:first').find('a').get(0));
            ts_swapclass($(this).parents('tr:first').find('a').get(0), ts_triggeroff, ts_triggeron);
            ts_swapclass(this.parentNode.parentNode.parentNode.parentNode.parentNode.getElementsByTagName('ul')[0], ts_dropdownclosed, ts_dropdownopen);
            try
            {
            $(this).parents('tr:first').find('a').get(0).focus();
            }
            catch(ex)
            {
            
            }
            return false;
        }
        triggerBtn.onmouseover = function() {
            isOnContainer = true;
        }
        triggerBtn.onmouseout = function() {
            isOnContainer = false;
        }

        trigger.appendChild(document.createTextNode(sel.options[0].text));

        var ta1 = document.createElement('table');
        ts_addclass(ta1, ts_table);
        ta1.insertRow(0);
        ta1.rows[0].insertCell(0);
        ta1.rows[0].insertCell(1);
        ta1.rows[0].cells[0].appendChild(trigger);
        ta1.rows[0].cells[1].appendChild(triggerBtn);
        ts_addclass(ta1.rows[0].cells[0], ts_cell);
        ts_addclass(ta1.rows[0].cells[1], ts_cell);
        
        sel.parentNode.insertBefore(ta1, sel);
        
//        sel.parentNode.insertBefore(trigger, sel);
//        sel.parentNode.insertBefore(triggerBtn, sel);
        var replaceUL = document.createElement('ul');

        for (var j = 0; j < sel.getElementsByTagName('option').length; j++) {
            var v = sel.getElementsByTagName('option')[j].text.length * p_widthPoint;
            if (maxItemLength < v) {
                maxItemLength = v;
            }
        }

        for (var j = 0; j < sel.getElementsByTagName('option').length; j++) {
            var newli = document.createElement('li');
            var newa = document.createElement('a');

            newli.v = sel.getElementsByTagName('option')[j].value;
            newli.elm = hiddenfield;
            newli.istrigger = trigger;
            newa.href = '#';
            newa.appendChild(document.createTextNode(
				sel.getElementsByTagName('option')[j].text));

            $(newa).css('width', maxItemLength + 'px');

            newli.onclick = function() {
                this.elm.value = this.v;
                ts_swapclass(this.istrigger, ts_triggeron, ts_triggeroff);
                ts_swapclass(this.parentNode, ts_dropdownopen, ts_dropdownclosed)
                this.istrigger.firstChild.nodeValue = this.firstChild.firstChild.nodeValue;
                return false;
            }

            newli.appendChild(newa);
            replaceUL.appendChild(newli);
        }

        if (sel.getElementsByTagName('option').length >= p_scrollRecCount) {
            $(replaceUL).css('height', '240px');
        }
        
//        $(ta1).mouseout(function() {
//            ts_swapclass(this.parentNode.parentNode.parentNode.parentNode.parentNode.getElementsByTagName('ul')[0], ts_dropdownclosed, ts_dropdownopen);
//        });
        
        ts_addclass(replaceUL, ts_dropdownclosed);
        var div = document.createElement('div');
        
        div.appendChild(replaceUL);
        div.onmouseover = function() {
            isOnContainer = true;
        }
        div.onmouseout = function() {
            isOnContainer = false;
        }
        
        ts_addclass(div, ts_boxclass);
        $(div).css('overflow', 'visible');
        sel.parentNode.insertBefore(div, sel)
        toreplace[count] = sel;
        count++;
    }

    /*
    Turn all ULs with the class defined above into dropdown navigations
    */

    var uls = document.getElementsByTagName('ul');
    for (var i = 0; i < uls.length; i++) {
        if (ts_check(uls[i], ts_listclass)) {
            var newform = document.createElement('form');
            var newselect = document.createElement('select');
            for (j = 0; j < uls[i].getElementsByTagName('a').length; j++) {
                var newopt = document.createElement('option');
                newopt.value = uls[i].getElementsByTagName('a')[j].href;
                newopt.appendChild(document.createTextNode(uls[i].getElementsByTagName('a')[j].innerHTML));
                newselect.appendChild(newopt);
            }
            newselect.onchange = function() {
                window.location = this.options[this.selectedIndex].value;
            }
            newform.appendChild(newselect);
            uls[i].parentNode.insertBefore(newform, uls[i]);
            toreplace[count] = uls[i];
            count++;
        }
    }
    for (i = 0; i < count; i++) {
        toreplace[i].parentNode.removeChild(toreplace[i]);
    }
    function ts_check(o, c) {
        return new RegExp('\\b' + c + '\\b').test(o.className);
    }
    function ts_swapclass(o, c1, c2) {
        var cn = o.className
        o.className = !ts_check(o, c1) ? cn.replace(c2, c1) : cn.replace(c1, c2);
    }
    function ts_addclass(o, c) {
        if (!ts_check(o, c)) { o.className += o.className == '' ? c : ' ' + c; }
    }

    function f_closeAllDropDown(p_element) {
        $('[class$=activetrigger]').each(
        function() {
        if (this != p_element) {
            ts_swapclass(this, ts_triggeroff, ts_triggeron)
            ts_swapclass(this.parentNode.parentNode.parentNode.parentNode.parentNode.getElementsByTagName('ul')[0], ts_dropdownclosed, ts_dropdownopen);
            }
        }
        );
    }

}
//Drop Down Menu
var timeout = 100;
var closetimer = 0;
var ddmenuitem = 0;

function jsddm_open() {
    jsddm_canceltimer();
    jsddm_close();
    ddmenuitem = $(this).find('div').eq(0).css('visibility', 'visible');
}

function jsddm_close()
{ if (ddmenuitem) ddmenuitem.css('visibility', 'hidden'); }

function jsddm_timer()
{ closetimer = window.setTimeout(jsddm_close, timeout); }

function jsddm_canceltimer() {
    if (closetimer) {
        window.clearTimeout(closetimer);
        closetimer = null;
    } 
}

$(document).ready(function() {
    $('[class=jsddm] > li').bind('mouseover', jsddm_open);
    $('[class=jsddm] > li').bind('mouseout', jsddm_timer);
});

document.onclick = jsddm_close;

