$(document).ready(function(){

    $("input[type='submit']").addClass("input-type-submit");
    $("input[type='text']").addClass("input-type-text");
    $("input[type='password']").addClass("input-type-text");

    $(this).bind("contextmenu", function(e) {
        e.preventDefault();
    });

    Main.bind();

    if (LANGUAGE == 'lt')
    {
        var days = ['Sk', 'Pr', 'An', 'Tr', 'Kt', 'Pn', 'Št'];
        var months = ['Sausis', 'Vasaris', 'Kovas', 'Balandis', 'Gegužė', 'Birželis', 'Liepa', 'Rugpjūtis', 'Rugsėjis', 'Spalis', 'Lapkritis', 'Gruodis'];
        $("#offer-date_end, .datepicker").datepicker({
            dateFormat: 'yy-mm-dd',
            dayNamesMin: days,
            dayNamesShort: days,
            monthNames: months
        });
    }
    else
    {
        $("#offer-date_end, .datepicker").datepicker({
            dateFormat: 'yy-mm-dd'
        });
    }
    
});


var Class_Main = function(){
}

Class_Main.prototype = {
    bind: function()
    {
        $(".table-list tr").hover(function(){
            $(this).addClass("tr-hover");
        },function(){
            $(this).removeClass("tr-hover");
        });

        $(".latest-box li").hover(function(){
            $(this).addClass("hover");
        },function(){
            $(this).removeClass("hover");
        });

        // disables L, H, B, T fields by metal form
        // the same is in the frontend
        $("#template-param_product").change(function(){
            var value = $(this).val();
            $.get('/catalog/index/getvailabletemplatesparams/', {
                product_id: value
            }, function(response){

                if (response.status == 'success')
                {
                    $("#template-t_mm").attr("disabled", false);
                    if (response.data.width == 0)
                    {
                        $("#template-t_mm").attr("disabled", true);
                    }

                    $("#template-h_mm").attr("disabled", false);
                    if (response.data.height == 0)
                    {
                        $("#template-h_mm").attr("disabled", true);
                    }

                    $("#template-l_mm").attr("disabled", false);
                    if (response.data.length == 0)
                    {
                        $("#template-l_mm").attr("disabled", true);
                    }

                    $("#template-b_mm").attr("disabled", false);
                    if (response.data.border == 0)
                    {
                        $("#template-b_mm").attr("disabled", true);
                    }
                }
            }, 'json');
        });
        $("#template-param_product").change();

        $("input[type='submit']").addClass("input-type-submit");
        $("input[type='text']").addClass("input-type-text");
        $("input[type='password']").addClass("input-type-text");
        
        $("a.action-submit").click(function(){
            $(this).parents("form").submit();
            return false;
        });

        $("a[href='#go-back']").click(function(){
            history.back(-1);
            return false;
        });

        $(".action-change-category").change(function(){
            window.location = $(this).val();
        });

        $(".table-list tr:odd").addClass("odd");

        $(".action-confirm-delete").click(function(){

            var result = confirm("Delete?") ;

            return result;
        });


        $("#profile-country").live('change', function(){
            $("#profile-city").load(BASE_URL + '/default/params/tooptions/parent_id/' + $(this).val());
        });
        
        $.each($(".zend_form .errors"), function(k, item){
            $(item).parents("dd").prev("dt").addClass('error');
            $(item).remove();
        });
    }
}

var Main = new Class_Main();
