﻿function DropDownManager() 
{
    this.init();
}

DropDownManager.prototype.Refresh = function() {
    $(".ddList").find("a").hover(function(e) { $(this).addClass("ddListHover"); }, function(e) { $(this).removeClass("ddListHover"); });
}

DropDownManager.prototype.init = function() {
    //Add styles
    var styleHtml = $("head > style").html();

    if (styleHtml == null) styleHtml = "";

    //background-color:Transparent; width:181px;	border:0; line-height:14px;	vertical-align:middle; height:14px;	background: url(/_graphics/gel/gelDropDown.png) no-repeat; padding:4px 0 4px 4px;

    styleHtml += ".ddSelector { background: url(/_graphics/gel/gelDropDown.png) no-repeat; display:none; background-color:Transparent; width:181px; border:0; line-height:14px; height:14px; font-size:12px;	padding:4px 0 4px 6px; cursor:pointer; }";
    styleHtml += ".ddSmall { background: url(/_graphics/gel/gelDropDown60.png) no-repeat; width:60px; }";
    styleHtml += ".dd90 { background: url(/_graphics/gel/gelDropDown90.png) no-repeat; width:90px; }";
    styleHtml += ".dd110 { background: url(/_graphics/gel/gelDropDown110.png) no-repeat; width:110px; }";
    styleHtml += ".dd130 { background: url(/_graphics/gel/gelDropDown130.png) no-repeat; width:130px; }";
    styleHtml += ".dd360 { background: url(/_graphics/gel/gelDropDown360.png) no-repeat; width:360px; }";
    styleHtml += ".ddList { position:absolute; z-index:19; list-style-type:none; font-size:12px; max-height:200px; overflow:auto; text-align:left; padding:0; margin:0; line-height:14px; display:block; width:180px; border:solid 1px #ababab; background:#fff; } ";
    styleHtml += ".ddListSmall { width:60px; }";
    styleHtml += ".ddList90 { width:90px; }";
    styleHtml += ".ddList110 { width:110px; }";
    styleHtml += ".ddList130 { width:130px; }";
    styleHtml += ".ddList360 { width:360px; }";
    styleHtml += ".ddList li { padding:0;	margin:0; }";
    styleHtml += ".ddList li a { padding:2px;	display:block; text-decoration:none; color:#000; }";
    //styleHtml += ".ddList li a:hover, .ddSelected { background:#3399ff; color:#fff; cursor:pointer; }";
    styleHtml += ".ddListHover { background:#3399ff; color:#fff; cursor:pointer; }";

    var style = $("head > style")[0];

    if (!style) {
        $("head").append("<style></style>");
        style = $("head > style")[0]
    }

    if (style.styleSheet)//because IE has been developed by idiots!
        style.styleSheet.cssText = styleHtml;
    else
        $("head > style").html(styleHtml);


    $(".ddList").find("a").each(function(i, el) {
        this.tabIndex = 99;
        if (this.className != "runats") {
            this.href = "#";
            this.onclick = function() { return false; };
        }
    });

    //$(".ddList").find("a").live("click", this.ItemClicked); // .click(this.ItemClicked);

    //$(".ddList").find("a").bind("mousedown", this, this.ItemClicked);
    $(".ddList").find("a").live("mousedown", this.ItemClicked);
    $(".ddList").find("a").live("click", this.ItemClicked);

    $(".ddList").bind("scroll", this, this.ListScroll);
    $(".ddList").bind("blur", this, this.Blur);
    $(".ddSelector").bind("click", this, this.Clicked);
    $(".ddSelector").focus(this.Focus);
    $(".ddSelector").bind("blur", this, this.Blur);
    $(".ddSelector").bind("keydown", this, this.KeyDown);

    $(".ddList").find("a").hover(function(e) { $(this).addClass("ddListHover"); }, function(e) { $(this).removeClass("ddListHover"); });

    $(".ddList").each(function(i, el) {
        var val = $(this).prev(".ddSelector").val();
        var found = false;

        $(this).find("a").each(function(i, el) {
            if (this.className == "runats") {
                found = true;
                $(".ddList").hide();
                return false;
            }

            if (this.innerHTML == val) {
                found = true;
                $(this).mousedown(); //click();
                return false;
            }
        });

        if (!found)
            $(this).find("a:first").mousedown(); //.click();
    });

    $(".ddSelector").show();
}

DropDownManager.prototype.Clicked = function(e) {
    var sel = $(e.target).next(".ddList").show();
    var curVal = $(e.target).val();

    //$(".ddSelected").removeClass("ddSelected");

    var pos = $(e.target).position();
    sel.css("top", pos.top + 22);
    sel.css("left", pos.left);

    sel.find("a").each(function(i, el) {
        if (this.innerHTML == curVal) {
           // $(this).addClass("ddListHover"); 
            //$(this).parent().addClass("ddSelected");
        }
    });
}

DropDownManager.prototype.timeout = null;
DropDownManager.prototype.ticks = null;
DropDownManager.prototype.scrollTimeout = null;

DropDownManager.prototype.ListScroll = function(e) {
    var ticks = (new Date()).getTime();
    e.data.ticks = ticks;
    clearTimeout(e.data.timeout);
    clearTimeout(e.data.scrollTimeout);
    $(e.target).prev(".ddSelector").focus();
}

DropDownManager.prototype.Blur = function(e) {
    //Due to focus being lost in IE when scrolling
    if (e.data.ticks != null) {
        var ticks = (new Date()).getTime();
        if ((ticks - e.data.ticks) < 50)
            return;
    }

    //otherwise click event fails.
    e.data.timeout = setTimeout(function() { e.data.ConfirmBlur(e) }, 100);
}

DropDownManager.prototype.ConfirmBlur = function(e) {
    //alert(document.activeElement.className);
    $(e.target).next(".ddList").keydown().hide();
    clearTimeout(this.timeout);
    this.timeout = null;
}

DropDownManager.prototype.Focus = function(e) {
    $(this).click();
}

DropDownManager.prototype.ItemClicked = function(e) {
    //Occurs on mousedown as otherwise if on click and user holds mouse button down, blur event fires first.
    //clearTimeout(e.data.timeout);
    //e.data.timeout = null;
    if (this.className.indexOf("runats") > -1) {
        var s = this.hash.substr(1).split("|");

        $("#" + s[0]).val(s[1]);
    }

    $(this).parent().parent().prev(".ddSelector").val(this.innerHTML);
    $(".ddList").hide();
    $(this).parent().parent().prev(".ddSelector").change();

    if (!$.browser.msie) {
        try {
            var pb = $(this).parent().parent().attr("onkeydown");
            if (jQuery.isFunction(pb))
                pb();
        }
        catch (ex) { }
    }
}

DropDownManager.prototype.KeyDown = function(e) {
    e.stopImmediatePropagation();

    if (e.keyCode == 38) { //move up
        e.data.MoveSelection(e, true);
    }
    else if (e.keyCode == 40) { //move down
        e.data.MoveSelection(e, false);
    }

    if (e.keyCode != 9) { //tab
        e.preventDefault();
    }
}

DropDownManager.prototype.MoveSelection = function(e, moveUp) {
    var sel = $(e.target).next(".ddList").show();
    var curVal = $(e.target).val();
    var cidx = 0;

    var vals = sel.find("a");

    vals.each(function(i, el) {
        if (this.innerHTML == curVal) {
            cidx = i;
        }
    });

    var newSelect = null;

    if (moveUp && cidx > 0) newSelect = vals[cidx - 1];
    else if (cidx < (vals.length - 1) && !moveUp) newSelect = vals[cidx + 1];
    else if (cidx == 0) newSelect = vals[0];

    if (newSelect != null) {
        vals.removeClass("ddListHover");
        $(newSelect).addClass("ddListHover");
        $(e.target).val(newSelect.innerHTML);

        if (newSelect.className.indexOf("runats") > -1) {
            var s = newSelect.hash.substr(1).split("|");

            $("#" + s[0]).val(s[1]);
        }
        
        $(e.target).change();
    }

    this.Clicked(e);
}

var ddMgr = null;

$(document).ready(function() {
    ddMgr = new DropDownManager();
});

function getDropDownListValue(ddlControlId) {

    var retval = "";

    try {
        var idx = parseInt($(ddlControlId).val());
        var lst = $(ddlControlId).prev(".ddList");

        var ss = lst.children().eq(idx).children()[0].href.split("|");

        retval = ss[2];
    }
    catch (e) { }
    
    return retval;
}
