﻿/* == Cbo JQuery == */
var gId = "";
function postField(hiddenFieldID, val) {
    if(hiddenFieldID != null && hiddenFieldID != "")
    {
        var hiddenField = document.getElementById(hiddenFieldID);
        if (hiddenField) {
            hiddenField.value = val;
            __doPostBack(hiddenFieldID,'');
        }
    }
}

function selectField(hiddenFieldID, val)
{
    if(hiddenFieldID != null && hiddenFieldID != "")
    {
        var hiddenField = document.getElementById(hiddenFieldID);
         if (hiddenField)
            hiddenField.value = val;
    }
}

function iniciaCombo() {
    
        //$('.select .pbActive .boxSelect').jScrollPane({scrollbarWidth:12, scrollbarMargin:10, showArrows:true, dragMaxHeight:8});
        
        comboBox();
}

function comboBox() {
    
    //$('.divConteudoSelect').hide().css({'top':(17)+'px'});
    $('div.select span.meio a').unbind("click");
    $('div.select span.meio a').click(function() {
		
    	/* Define o elemento pai .select */
    	var p = $(this).parent().parent();

    	/* Some com os abertos */
    	$('.divConteudoSelect').each(function(i) {
    		if ($(this).parent()[0] !== p[0]) {
    			$(this).slideUp();
    			$(this).parent().css({ "zIndex": 1 });
    			if ($(this).hasClass("left")) {
    				$(this).parent().find('span.esq').each(function() {
    					$(this).removeClass("esqOn");
    				});
    			}
    			else if ($(this).hasClass("right")) {
    				$(this).parent().find('span.dir').each(function() {
    					$(this).removeClass("dirOn");
    				});
    			}
    		}
    		$("body").unbind("click");
    		$(".divConteudoSelect").removeClass("pbActive");
    	});

    	p.find('.divConteudoSelect').each(function() {
    		var e = $(this).parent().css("zIndex");
    		if (e == 1) {
    			$(this).parent().css({ "zIndex": 502 });
    			if (p.find('.divConteudoSelect').hasClass("left")) {
    				$(this).parent().find('span.esq').each(function() {
    					$(this).addClass("esqOn");
    				});
    			}
    			else if (p.find('.divConteudoSelect').hasClass("right")) {
    				$(this).parent().find('span.dir').each(function() {
    					$(this).addClass("dirOn");
    				});
    			}
    		}
    		$(this).slideToggle("fast", function() {
    			if (e != 1) {
    				$(this).parent().css({ "zIndex": 1 });

    				if (p.find('.divConteudoSelect').hasClass("left")) {
    					$(this).parent().find('span.esq').each(function() {
    						$(this).removeClass("esqOn");
    					});
    				}
    				else if (p.find('.divConteudoSelect').hasClass("right")) {
    					$(this).parent().find('span.dir').each(function() {
    						$(this).removeClass("dirOn");
    					});
    				}
    				$(".divConteudoSelect").removeClass("pbActive");
    			}

    			$("body").bind("click", function(e) 
    			{
    				var fecha = true;
    				$('.divConteudoSelect *', p).each(function() 
    				{
    					if (e.target == $(this)[0]) 
    					{
    						fecha = false;
    					}
    				});

    				if (fecha) 
    				{
    					$(p).css({ "zIndex": 1 });

    					if ($('.divConteudoSelect', p).hasClass("left")) {
    						$('span.esq', $(this).parent().parent()).removeClass("esqOn");
    					}
    					else if ($('.divConteudoSelect', p).hasClass("right")) {
    						$('span.dir', $(this).parent().parent()).removeClass("dirOn");
    					}
    					p.find('.divConteudoSelect').hide();
    					$("body").unbind("click");
    					$(".divConteudoSelect").removeClass("pbActive");
    					return false;
    				}
    			});
    		});
    	});
    	$(".boxSelect", p).jScrollPane({ scrollbarWidth: 12, scrollbarMargin: 10, showArrows: true, dragMaxHeight: 8 });
    	return false;
    });

	$('.divConteudoSelect ul.lstSelect li ul li a').click(function() {
	    $("#" + $(this).attr("rel")).val($(this).attr("rev"));

	    var p = $(this).parent().parent().parent().parent().parent().parent().parent();

	    $(p.parent()).css({ "zIndex": 1 });

	    gId = p.parent().attr("id");

	    var redo = $("#" + gId);
	    var tam = Math.floor(($("span.meio:eq(0) a", redo).outerWidth()) / 6.78)-1;
	    var txt = $(this).text();
        if(txt.length > tam) {
            txt = txt.substring(0,tam);
        }
        $("span.meio:eq(0) a", redo).text(txt);
	    p.parent().attr("rel", tam);

	    p.hide();
	    if (p.hasClass("left")) {
	        p.parent().find('span.esq').each(function() {
	            $(this).removeClass("esqOn");
	        });
	    }
	    else if (p.hasClass("right")) {
	        p.parent().find('span.dir').each(function() {
	            $(this).removeClass("dirOn");
	        });
	    }

	    if ($(this).attr("lang") == "true") {
	        $(".divConteudoSelect", p).addClass("pbActive");
	        postField($(this).attr("rel"), $(this).attr("rev"));
	    }
	    else {
	        selectField($(this).attr("rel"), $(this).attr("rev"));
	    }

	    $("body").unbind("click");
	    if ($(this).attr("href") == "") return false;
	});
}

/* == Cbo JQuery == */
