jQuery.fn.slideFadeToggle = function(speed, easing, callback) {
    return this.animate({opacity: 'toggle', height: 'toggle'}, speed, easing, callback);
};
jQuery.fn.inputHints = function() {
    $(this).each(function(i) { $(this).val($(this).attr('title')).css('color', '#999'); });
    $(this).focus(function() {
        if ($(this).val() == $(this).attr('title')) $(this).val('').css('color', '#000');
    }).blur(function() {
        if ($(this).val() == '') $(this).val($(this).attr('title')).css('color', '#999');
    });
};
jQuery.fn.vAlign = function() {
    $(this).css('margin-top', '0');
    var img_h;
    var cont_h = $(this).parent().height();
    if ($(this).height() == 0)
    {
        $(this).load(function() {
            img_h = $(this).height();
            if (img_h < cont_h)
            {
                $(this).css('margin-top', ((cont_h - img_h) / 2));
            }
        });
    }
    else
    {
        img_h = $(this).height();
        if (img_h < cont_h)
        {
            $(this).css('margin-top', ((cont_h - img_h) / 2));
        }
    }
};

$(function() {

    // input hints
    $('input[title]').inputHints();

    if ($('#product .scrollable').length > 0)
    {
        var scrollable_size = 3;
        $("div.scrollable").scrollable({
            size: scrollable_size
        });
        $('.items div').click(function() {
            var url = $(this).children('img').attr('src').replace('tiny', 'medium');
            var wrap = $('.main_pic').fadeTo('medium', 0.5);
            var img = new Image();
            img.onload = function() {
                wrap.fadeTo('fast', 1);
                wrap.find('img').attr('src', url);
                $('.main_pic img').vAlign();
            };
            img.src = url;
        }).filter(':first').click();

        var total_img = $('.items img').length;
        if (total_img == scrollable_size)
        {
            $('.nextPage').addClass('disabled');
        }
        else if (total_img < scrollable_size)
        {
            var new_width = total_img*(60+2+4); // img container + borders + margins + prev/next containers
            $('.scrollable').css('width', new_width);
            $('.other').css('width', new_width+24); // prev/next containers
        }
    }

    /* LOGO */
    $('#logo').click(function() {
        window.location.href = './';
    });

    /* NAV TOP */
    $('#nav_top li.root, #nav_top li.root ul li').click(function() {
        window.location.href = $(this).children('a').attr('href');
    });
    var config_hoverIntent = {
        sensitivity: 7, // number = sensitivity threshold (must be 1 or higher)
        interval: 0, // number = milliseconds for onMouseOver polling interval
        over: function() { // function = onMouseOver callback (REQUIRED)
            $(this).addClass('hover');
            var $sub = $(this).children('ul');
            if ($sub.length > 0)
            {
                if (jQuery.browser.msie) $sub.get(0).style.removeAttribute('filter');
                $sub.show();
            }
        },
        timeout: 50, // number = milliseconds delay before onMouseOut
        out: function() { // function = onMouseOut callback (REQUIRED)
            $(this).removeClass('hover');
            var $sub = $(this).children('ul');
            if ($sub.length > 0)
            {
                $sub.slideFadeToggle(200);
            }
        }
    }
    $('#nav_top li.root').hoverIntent(config_hoverIntent);
    $('#nav_top li.root ul li').hover(function() {
        $(this).addClass('hover');
    }, function() {
        $(this).removeClass('hover');
    });

    /* SEARCH */
    $('.search_results .pagination a').click(function() {
        var href = $(this).attr('href').split('/');
        var $form = $('.search_results form');
        var action = $form.attr('action');
        $form.attr('action', action+'/'+href[5]);

        $form.children('button').click();

        return false;
    });

});

// vertical alignment: execute after the page has been loaded completely
$(window).load(function() {

    // vertical alignment
    if ($('.product_cell').length > 0)
    {
        $(this).find('.pic img').each(function(i, e) {
            $(e).vAlign();
        });
    }
    if ($('#product .main_pic').length > 0)
    {
        $('.main_pic img').vAlign();
    }
    if ($('#product .scrollable').length > 0)
    {
        $('.items img').each(function(i, e) {
            $(e).vAlign();
        });
    }
});