$(function () {
    // -------------- Checkboxes ---------------------- 
    $("a.checkbox").each(function () {
        if ($(this).hasClass("checked")) { $("input", this).val(1); } else { $("input", this).val(0); }

        $(this).click(function () {
            if ($(this).hasClass("checked")) {
                $(this).removeClass("checked");
                $(this).addClass("no_checked");
                $("input", this).val(0);
            } else {
                $(this).removeClass("no_checked");
                $(this).addClass("checked");
                $("input", this).val(1);
            }
            return false;
        });
    });
    
 // -------------- Radios ---------------------- 

    $('#costo_total').click(function(){
   		if($(this).hasClass("no_checked"))
   		{
   			$(this).removeClass("no_checked");
            $(this).addClass("checked");
            $("input", this).val(1);
            
   			$('#costo_mensual').removeClass("checked");
   			$('#costo_mensual').addClass("no_checked");
   			$("input", '#costo_mensual').val(0);
    	}
   		
   		return false;
   	});
    
    $('#costo_mensual').click(function(){
    	if($(this).hasClass("no_checked"))
   		{
   			$(this).removeClass("no_checked");
            $(this).addClass("checked");
            $("input", this).val(1);
            
   			$('#costo_total').removeClass("checked");
   			$('#costo_total').addClass("no_checked");
   			$("input", '#costo_total').val(0);
    	}
    	return false;
   });

    // ----------- comboboxes  -------------------------
    $(".combobox a").live("click", function () {
        var obj = $("ul", $(this).parent());
        if ($(obj).is(":hidden")) {
            $(obj).slideDown("fast");
        } else {
            $(obj).slideUp("fast");
        }
        return false;
    });
    $(".combobox>span").live("click", function () {
        var obj = $("ul", $(this).parent());
        if ($(obj).is(":hidden")) {
            $(obj).slideDown("fast");
        } else {
            $(obj).slideUp("fast");
        }
        return false;
    });
    $(".combobox ul li").live("click", function () {
        $("input:hidden", $(this).parent().parent()).val($(this).text());
        $("span", $(this).parent().parent()).text($(this).text());
        $("a", $(this).parent().parent()).trigger("click");
    });

    // ----------- comboboxes2  -------------------------
    $(".combobox2 a").live("click", function (event) {
        var obj = $("ul", $(this).parent());
        if ($(obj).is(":hidden")) {
            $(obj).slideDown("fast");
        } else {
            $(obj).slideUp("fast");
        }


        return false;
    });

    $(".combobox2>span").live("click", function () {
        var obj = $("ul", $(this).parent());
        if ($(obj).is(":hidden")) {
            $(obj).slideDown("fast");
        } else {
            $(obj).slideUp("fast");
        }
        return false;
    });
    $(".combobox2 ul li").live("click", function () {
        $("input:hidden", $(this).parent().parent()).val($(this).text());
        $("span", $(this).parent().parent()).text($(this).text());
        $("a", $(this).parent().parent()).trigger("click");
    });




    // ----------- esquina expandible ------------------------
    $(".expandible div").each(function () {
        $(this).mousedown(function (event) {
            posicionar_caja("activar", $(this));
            if ($.browser.msie) {
                $(this).draggable("destroy");
            }
            return false;
        });
    });
    $(document).mousemove(function (e) {
        mouse(e.pageX, e.pageY);
    });
    function mouse(x, y) {

        if (typeof mouse.pos == "undefined") {
            mouse.pos = [0, 0];
        }
        if (typeof x == "undefined") {
            return mouse.pos;
        } else {
            mouse.pos[0] = x;
            mouse.pos[1] = y;
            posicionar_caja("mover");
        }
    }
    function posicionar_caja(orden, object) {
        if (typeof posicionar_caja.estado == "undefined") {
            posicionar_caja.estado = false;
            posicionar_caja.obj = null;

        }
        var Xmin = 100;
        var Ymin = 50;

        var pos = mouse();

        switch (orden) {
            case "activar":
                posicionar_caja.obj = $(object).parent();
                posicionar_caja.position = $(posicionar_caja.obj).position();
                $(document).one("mouseup", function () {
                    posicionar_caja("desactivar");
                    return false;
                });
                posicionar_caja.estado = true;
                break;
            case "desactivar":
                posicionar_caja.estado = false;
                posicionar_caja.obj = null;
                break;
            case "mover":
                if (posicionar_caja.estado) {
                    var Xmax = parseInt(($(posicionar_caja.obj).css("max-width") != "none") ? $(posicionar_caja.obj).css("max-width") : 400);
                    var Ymax = parseInt(($(posicionar_caja.obj).css("max-height") != "none") ? $(posicionar_caja.obj).css("max-height") : 400);

                    var incrementoX = ((pos[0] - posicionar_caja.position.left) > Xmin) ? pos[0] - posicionar_caja.position.left : Xmin;
                    if (incrementoX > Xmax) { incrementoX = Xmax };
                    var incrementoY = ((pos[1] - posicionar_caja.position.top) > Ymin) ? pos[1] - posicionar_caja.position.top : Ymin;
                    if (incrementoY > Ymax) { incrementoY = Ymax };

                    $(posicionar_caja.obj).css({ width: incrementoX + "px", height: incrementoY + "px" });
                    $("textarea", posicionar_caja.obj).css("height", $(posicionar_caja.obj).innerHeight() - 12 + "px");

                }
                break;
        }
    }
    // -----------------------------------------------------

});

