
/**
 * Funkce pro posouvani videa na uvodni strance
 */
$(document).ready(function() {
    page_init();
});
 
function page_init(){
    init_menu();
    init_form_errors();
    init_mouse();
}

function init_menu() {    
    // scroll
    var height = 616;
    $('#manufacturers_parfumes, #manufacturers_cosmetics').each(function(i) {
        var cont = $(this).find('ul');
        $(this).find('div.prev').click(function() {
            var top = cont.position().top * (-1);
            if (top > 0) {
                cont.animate({'top': Math.min(Math.floor((top / height) - 1) * height * (-1), 0)}, 200);
            }
        });
        $(this).find('div.next').click(function() {
            var top = cont.position().top * (-1);
            var h = cont.height();
            if (top + height <= h) {
                cont.animate({'top': Math.floor((top / height) + 1) * height * (-1)}, 200);
            }
        });
    });

    // tread switching as tabs
    $("#sidemenu").tabs({
        cookie: { expires: 1 },
        select: function(event, ui) {
            change_groupmenu(ui.index);
        }
    });    
    // check if there is some active comestics
    var cosmetics = $('#parameterized_tree li.active').size();
    if (cosmetics > 0) {
        $('#sidemenu').tabs('select', 1); // select cosmetics
    }
    change_groupmenu($("#sidemenu").tabs("option", "selected"));
    
    // alphabetics
    $('#parfums ul.alphabetics a').click(function() {
        var letter = $(this).attr('rel');
        $("#parfums ul.manufacturers li").show();
        $('#parfums ul.manufacturers').css('top', 0);
        if (letter == 'all') return false;
        $("#parfums ul.manufacturers li:not(.letter_" + letter + ")").hide();
        return false;
    });
    $('#cosmetics ul.alphabetics a').click(function() {
        var letter = $(this).attr('rel');
        $("#cosmetics ul.manufacturers li").show();
        $('#cosmetics ul.manufacturers').css('top', 0);
        if (letter == 'all') return false;        
        $("#cosmetics ul.manufacturers li:not(.letter_" + letter + ")").hide();
        return false;
    });
}

function change_groupmenu(selected) {
    if (selected == 0) {
        $('#groupmenu div.parfumes').show();
        $('#groupmenu div.cosmetics').hide();
    }
    else {
        $('#groupmenu div.parfumes').hide();
        $('#groupmenu div.cosmetics').show();
    }
}

function init_form_errors() {
    var form = $('div.module_bsOrder_action_order form, div.module_bsUserProfile_action_register form');
    if (form.length > 0) {
        // delete doble errors
        form.find('td').each(function(i) {
            $(this).find('ul.error_list:gt(0)').remove();
        });
        // fix errors to input
        form.find('ul.error_list').each(function(i) {
            var sibling = $(this).siblings('input[type=text], input[type=password], select, textarea');
            if (sibling.length > 0) {
                sibling.addClass('input_error');
                $(this).addClass('input_error');
                $(this).width(sibling.width() - 2);
            }
        });
    }
}

function init_mouse() {
    $('#manufacturers_parfumes, #manufacturers_cosmetics').mousewheel(function(event, delta) {
        //alert(delta);
        var height = 616;
        var cont = $(this).find('ul');
        var top = cont.position().top * (-1);
        // prev
        if (delta > 0) {
            if (top > 0) {                
                cont.css({'top': Math.min(top * (-1) + 44, 0)});
            }
        }
        // next
        else if (delta < 0) {            
            var h = cont.height();
            if (top + height <= h) {
                cont.css({'top': top * (-1) - 44});
            }
        }
        return false;
    })
}
