//$(document).ready(function() {
function customSelectFunction(){
    $('select.customselect').css('visibility','hidden');
    $('select.customselect').each(function() {
        var currentObject = $(this);
        var cID = currentObject.attr('id') != "" ? currentObject.attr('id') : '_cust_'+$('div.customselect').length;
        var cW = currentObject.width() || currentObject.css('width');
        var cPL = currentObject.css('padding-left');
        var cPR = currentObject.css('padding-right');
        var cML = currentObject.css('margin-left');
        var cMR = currentObject.css('margin-right');        
        var cFloat = currentObject.css('float');
        var isMul = currentObject.attr('multiple');        
        var currentOptions = currentObject.find("option");
        var parentNode = currentObject.parent();
        var cWNoPx = cW.toString().replace(/px/,"");
        var cOffWid = parseInt(cWNoPx)+parseInt(cPL.split("px")[0])+parseInt(cPR.split("px")[0]);
        var bGP = (cOffWid - 18) + "px 1px";
        var currentText = "Select ";
        var cName = currentObject.attr("name");
        var cPName = currentObject.attr('pName') || cName;
        if(isMul) { 
            currentText+= "up to "+ currentOptions.length + " " + cPName;
        }
        else { 
            currentText += "One "+ cName;
        }
        currentObject.remove();
        parentNode.append("<div class='customselect'><div class='customselect-label'></div><div class='customselect-container'></div></div>");
        var container = $('div.customselect:last');
        container.attr('name',cName).attr('pName',cPName);
        var lContainer = $(container.find('div.customselect-label'));
        lContainer.css({
            'background-position':bGP,
            'width':cOffWid
        }).hover(function(){
            $(this).addClass('customeHover');
        },function(){
            $(this).removeClass('customeHover');
        }).autoEllipsis(currentText,{
            'width':cOffWid - 20
        });
        if(cID != "") container.attr('id',cID);
        container.css({
            'width':cW,
            'padding-left':cPL,
            'padding-right':cPR,
            'margin-left':cML,
            'margin-right':cMR,
            'float':cFloat
        });
        if(!isMul){
            var hidden_html = "<input type='hidden' id='hidden_"+cID+"' name='"+cID+"' value=''"; //hidden variable for single select to send as params in form submit
            container.append(hidden_html);
        }

        var mContainer = $(container.find('div.customselect-container'));
        var dropDownList = $(document.createElement("div"));
        dropDownList.addClass('menu');
        var widthCalculator = $(document.createElement("span"));
        var maxContainerWidth = 0;
        widthCalculator.attr("id","widthCalculator");
        $('body').append(widthCalculator);
        for (var x =0 ; x < currentOptions.length; x++) {
            var listOption = $(document.createElement("div"));
            dropDownList.append(listOption);
            if($(currentOptions[x]).attr("selected")){ //set default value for hidden variables.
                $("#hidden_"+cID).val($(currentOptions[x]).val());
            }
            listOption.attr("id","option"+x).attr("index",x).attr("name", $(currentOptions[x]).val()).addClass('item');
            var inHTML = "";
            if(isMul){
                // for condition & auction type list box the param name sent condition_USED (REFUR etc..) & type_REG (FIX etc..)
                // hence the below condition
                if(cID=="condition" || cID=="type")
                {
                    inHTML = "<input class='inputClass' type='checkbox' name='"+cID+"_"+$(currentOptions[x]).val()+"' value='"+$(currentOptions[x]).val()+"' id='"+cID+"option"+x+"'><span class='spanClass'><label class='labelClass'>"+$(currentOptions[x]).html()+"</label></span>";
                }
                else 
                {
                    inHTML = "<input class='inputClass' type='checkbox' name='"+cID+"' value='"+$(currentOptions[x]).val()+"' id='"+cID+"option"+x+"'><span class='spanClass'><label class='labelClass'>"+$(currentOptions[x]).html()+"</label></span>";
                }
            } else {
                inHTML = "<span class='spanClass'><label class='labelClass'>"+$(currentOptions[x]).html()+"</label></span>";
            }
            widthCalculator.html(inHTML);
            listOption.html(inHTML);
            listOption.css({
                'background-color':'white'
            }).hover(function(){
                $(this).css({
                    'background-color':'#adc3e0'
                });
            },function(){
                $(this).css({
                    'background-color':'white'
                });
            });
            if(isMul){
                $(listOption.find('label')).attr('for',listOption.find('input:checkbox').attr('id'));
                $(listOption).find('input:checkbox').bind('click',{
                    con:container
                },function(event){
                    var con = event.data.con;
                    var sO = con.find('input:checkbox:checked').length;
                    if(sO == 0 ) {
                        con.find('div.customselect-label').autoEllipsis("Select up to " + con.find('div.item').length + " "+ con.attr('pName'),{
                            'width':con.attr('offsetWidth') - 20
                        });
                    } else if(sO == 1) {
                        con.find('div.customselect-label').html("1 "+con.attr('name')+" selected");
                    } else {
                        con.find('div.customselect-label').html(sO+" "+con.attr('pName')+" selected").autoEllipsis(sO+" "+con.attr('pName')+" selected",{
                            'width':con.attr('offsetWidth') - 30
                        });
                    }
                });
            /*  $(listOption).find("div.spanClass").bind('click',{con:container},function(event){
					var checked = $(this).parent().find('input:checkbox').attr("checked")?false:true;
					$(this).parent().find('input:checkbox').attr("checked",checked);
                    var con = event.data.con;
                    var sO = con.find('input:checkbox:checked').length;
                    if(sO == 0 ) {
                        con.find('div.customselect-label').autoEllipsis("Select up to " + con.find('div.item').length + " "+ con.attr('pName'),{'width':con.attr('offsetWidth') - 20});
                    } else if(sO == 1) {
                        con.find('div.customselect-label').html("1 "+con.attr('name')+" selected");
                    } else {
                        con.find('div.customselect-label').html(sO+" "+con.attr('pName')+" selected");
                    }
                });*/
            }
            else {
                $(listOption).bind('click', function(){
                    var cS = $(this);
                    var cP = cS.parents('div.customselect');
                    var sValue = cS.attr('name');
                    var sHTML =  cS.find('label').html();
                    $("#hidden_"+cP.attr("id")).val(sValue);
                    cP.attr('val',sValue).find('div.customselect-label').autoEllipsis(sHTML,{
                        'width':cP.attr('offsetWidth') - 20
                    });
                    cP.find('div.customselect-container').hide();
                });
            }
            if($(currentOptions[x]).attr('selected')) {
                if(isMul)
                {
                    listOption.find('input:checkbox').attr('checked','true');
                }
                else {
                    lContainer.autoEllipsis($(currentOptions[x]).html(),{
                        'width':cOffWid -20
                    });
                    container.val('attr',$(currentOptions[x]).val());
                }
            }
            if(maxContainerWidth < widthCalculator.attr('offsetWidth')) {
                maxContainerWidth = widthCalculator.attr('offsetWidth');
            }
        }
        widthCalculator.remove();
        maxContainerWidth = maxContainerWidth > cOffWid ? maxContainerWidth : cOffWid;
        mContainer.css({
            'width':(maxContainerWidth+20)+"px",
            'height':(dropDownList.attr('offsetHeight'))+"px"
        }).append(dropDownList);
        $('div.header').find('div#location').each(function(){
            $(this).find('div.menu').css({
                "height":"255px",
                "width":"150px",
                "overflow":"auto"
            }) ; //add overflow for location select alone.
            $(this).find('div.customselect-container').css({
                "width":"150px"
            });
        });
        $('div.advancedSearch').find('div#location').each(function(){
            $(this).find('div.menu').css({
                "height":"237px",
                "width":"195px",
                "overflow":"auto"
            }) ; //add overflow for location select alone.
            $(this).find('div.customselect-container').css({
                "width":"195px"
            });
        });
        $('div.header').find('div#item_condition').each(function(){
            $(this).find('div.menu').css({
                "width":"148px",
                "overflow":"auto"
            }) ; //add overflow for condition select alone.
            $(this).find('div.customselect-container').css({
                "width":"148px"
            });
        });
        $('div.advancedSearch').find('div#match').each(function(){
            //add overflow for match select alone.
            $(this).find('div.customselect-container').css({
                "width":"195px"
            });
        });
        $('div.advancedSearch').find('div#size').each(function(){
            $(this).find('div.customselect-container').css({
                "width":"177px"
            });
            $(this).find('div.deselectMiddle').css({
                "width":"167px"
            });
        });
        $('div.advancedSearch').find('div#condition').each(function(){
            $(this).find('div.customselect-container').css({
                "width":"177px"
            });
            $(this).find('div.deselectMiddle').css({
                "width":"167px"
            });
        });
        $('div.advancedSearch').find('div#type').each(function(){
            $(this).find('div.customselect-container').css({
                "width":"177px"
            });
            $(this).find('div.deselectMiddle').css({
                "width":"167px"
            });
        });
        $('div.advancedSearch').find('div#perPage').each(function(){
            $(this).find('div.customselect-container').css({
                "width":"171px"
            });
            $(this).find('div.deselectMiddle').css({
                "width":"171px"
            });
        });
        $('div.advancedSearch').find('div#sort').each(function(){
            $(this).find('div.customselect-container').css({
                "width":"187px"
            });
            $(this).find('div.deselectMiddle').css({
                "width":"187px"
            });
        });
		
        //deselect bar at the bottom of multiselect
        if(isMul){
            var deselectContWidth = mContainer.css('width');
            deselectContWidth = (deselectContWidth.substring(0,deselectContWidth.length-2));
            var deselectContainerHTML = "<div class='deselectContainer'><div class='deselectLeft'/><div class='deselectMiddle'  style='width:"+(deselectContWidth-10)+"px'><a class='leftLink'>Deselect all</a><span style='float: right;'><a class='rightLink'><div id='ddcbl_close'></div></a></span></div><div class='deselectRight'></div>";
            mContainer.append(deselectContainerHTML);
        }
        //hide the container when close link in deselect bar is clicked
        $('a.rightLink').bind("click",function(){
            $('div.customselect-container').hide();
        });
        //deselect all checkbox when deselect all link in deselect bar is clicked
        $('a.leftLink').bind("click",function(){
            $(this).parents('div.customselect-container').find('input:checkbox').each(function(){
                if(this.checked = true)
                {    
                    this.checked = false;
                }
            });
            var container = $(this).parents('div.customselect');
            container.find('div.customselect-label').autoEllipsis("Select up to " + container.find('div.item').length + " "+ container.attr('pName'),{
                'width':container.attr('offsetWidth') - 20
            });
        });
        lContainer.bind('click',{
            con:mContainer
        },clickOperation);
    });
    function clickOperation(event) {
        if (event.data.con.css('display') == 'none') {
            event.data.con.css('display','block');
            $(document).bind('click', {
                con:event.data.con
            }, function(event) {
                if (($(event.target).parents('div.customselect').length) == 0 || $(event.target).parents('div.customselect').attr('id') != event.data.con.parents('div.customselect').attr('id')) {
                    $(document).unbind('click',clickOperation);
                    event.data.con.hide();
                }
            });
        } else {
            event.data.con.css('display','none');
            $(document).unbind('click',clickOperation);            
        }
    }
//});
}

$(document).ready(function(){
    //customSelectFunction();
})

