$(function() {

    $("select.products-sorting, select.products-bygender, select.products-perpage").change(function(){
        $(this).parent('form').submit();
    });

    $('.newsletter-block input:text').focus(function(){
        $(this).val('');
    });
    
    $('.newsletter-block input:submit').click(function(){
        $("#newsletter-signup-success").hide();
        $("#newsletter-signup-error").hide();
        $.ajax({
            type: "POST",
            url: "index.php",
            data: { 
                page : "/modules/newsletter/ajax_newsletter_signup", 
                email : $('.newsletter-block input:text').val()
            },
            success: function(subscriber) {
	            if(subscriber.id != null) {
                    $("#newsletter-signup-success").show();
                } else {
                    $("#newsletter-signup-error").text(subscriber.errors.email);
                    $("#newsletter-signup-error").show();
                }
	        },
            dataType:"json"
        });
        return false;
    });

    $('.help').live('click',function() {
        var dialogId = "#" + $(this).attr('rel');
        $(dialogId).dialog({
           width: 650,
           resizable: false,
           close: function() { $(this).dialog( 'destroy' );}
	    });
		return false;
	});

    $('#category-list > ul > li').hover(function() {
        var submenu = $(this);
        if($(submenu).size() > 0)
        {
			var position = Math.floor(($(this).children("div.categories-level-2").height() - 20) / 2);
			$(this).children("div.categories-level-2").css({ top: -position });
            $(this).children("div.categories-level-2").show();
        }
    }, function() {
        $(this).children("div.categories-level-2").hide();
    });

    $('.thumbnails a').click(function(){
        $("#mainImage").attr('src', $(this).attr('href'));
        $('#productImage a').attr('href', $(this).attr('rel'));
        $('#enlarge a').attr('href', $(this).attr('rel'));
        return false;
    });

});    
