//init
var menuTO;
var fairForm;

$(document).ready(function() { 
    //tableSorter
    $("#exhibitorsListTable").tablesorter();

    //lightbox
    $("a[@rel*=lightbox]").lightbox({
        overlayOpacity:             0.5,
		fileLoadingImage :          'libs/jquery/jquery.lightbox/images/loading.gif',
		fileBottomNavCloseImage :   'libs/jquery/jquery.lightbox/images/closelabel.gif'
    });

    //main menu
    if (!$.browser.msie || ($.browser.msie && parseInt($.browser.version) >= 7)) {
        $("#seguiment").show();
        $("#menu > ul > li > a").each(function(i) {
            $(this).mouseover(function(){
                clearTimeout(menuTO);
                $("#seguiment").stop();
                $("#seguiment").animate({ 
                    width: $(this).width() + ($.browser.msie ? 14 : 16),
                    marginLeft: $(this).position().left - $(this).parent().parent().position().left
                    }, 100);
            });

            $(this).mouseout(function(){
                menuTO = setTimeout(function() { $("#seguiment").animate({ 
                                        width: $("#menu > ul > li.current > a").width() + ($.browser.msie ? 14 : 16),
                                        marginLeft: $("#menu > ul > li.current > a").position().left - $("#menu > ul > li.current > a").parent().parent().position().left
                                        }, 100)}, 200);
            });
        });

        $("#seguiment").animate({ 
            width: $("#menu > ul > li.current > a").width() + ($.browser.msie ? 14 : 16),
            marginLeft: $("#menu > ul > li.current > a").position().left - $("#menu > ul > li.current > a").parent().parent().position().left
            }, 10);
    }
});

function updateFairFormAmounts() {
    jQuery.each(fairForm, function(group, options) {
        count = 0;
        price = 0;

        jQuery.each(options, function() {
            if ($("#" + this).val()) {
                if (isNaN($("#" + this + "Price").val())) {
                    $("#" + this + "Amount").val($("#" + this + "Price").val());
                } else {
                    $("#" + this + "Amount").val(number_format($("#" + this + "Qty").val() * $("#" + this + "Price").val(), 2, "", "."));
                }
                count++;
            }

            if ($("#" + this + ":checked").val()) {
                if (isNaN($("#" + this + "Price").val())) {
                    price += 0;
                } else {
                    price += parseFloat($("#" + this + "Qty").val() * $("#" + this + "Price").val());
                }
            }
        });

        if (count > 0) {
            $("#" + group + "Subtotal").val(number_format(price, 2, "", "."));
            $("#" + group + "Tax").val(number_format(price * 7 / 100, 2, "", "."));
            $("#" + group + "Total").val(number_format(price + (price * 7 / 100), 2, "", "."));
        }
    });
}

function keyDownFairForms(event) {
    var keyCode = (event.keyCode ? event.keyCode : event.wich);
    
    if (keyCode == 13) {
        this.blur();
        return cancelEvent(event);
    }
}

function updateFairFormCheckboxs() {
    $("form#two input[type=checkbox]").each(function(i) {
        if ($(this).attr("checked")) {
            $(this).val("1");
        } else {
            $(this).val("");
        }

        $(this).attr("checked", "checked");
    });
}

function fairFormCatalogAdd(strCode, objSrc, e) {
    newObj = $(strCode);
    newObj.hide();

    $(objSrc).parent().parent().parent().parent().before(newObj);
    newObj.slideDown(200);

    return cancelEvent(e);
}

/* api */
function cancelEvent(objEvent) {
    objEvent.cancelBubble = true;
    objEvent.returnValue = false;

    return false;
}

function number_format(strValue, intDecimals, strGroupSep, strDecSep) {
    strValue = Math.round(strValue * Math.pow(10, intDecimals)) / Math.pow(10, intDecimals);

    if (parseFloat(strValue) >= 0) {
        e = strValue + '';
        n = false;
    } else {
        e = (strValue * -1) + '';
        n = true;
    }
    
    f = e.split('.');

    if (!f[0]) { f[0] = '0';}
    if (!f[1]) { f[1] = ''; }
    if (f[1].length < intDecimals) {
        g = f[1];

        for (i=f[1].length + 1; i <= intDecimals; i++) {
            g += '0';
        }

        f[1] = g;
    }

    if(strGroupSep != '' && f[0].length > 3) {
        h = f[0];
        f[0] = '';

        for(j = 3; j < h.length; j+=3) {
            i = h.slice(h.length - j, h.length - j + 3);
            f[0] = strGroupSep + i +  f[0] + '';
        }

        j = h.substr(0, (h.length % 3 == 0) ? 3 : (h.length % 3));
        f[0] = j + f[0];
    }

    strDecSep = (intDecimals <= 0) ? '' : strDecSep;

    if (n) { f[0] = "-" + f[0]; };
    return f[0] + strDecSep + f[1];
}
