$(document).ready(function(){

    // update wishlist
    function updateWishlist(){
        $('#wishlist').load($('#wishlist').attr('title'), function(){

            // update wishlist buttom
            var label = $('#wishlist_welcome_label').attr('rel').replace('%amount%', $('.count_wishlist').length);
           $('#wishlist_welcome_label').html(label);

           // rebind click event for deleting products
           $('#wishlist_content .delete.remove').each(function(){
                $(this).unbind('click');
                $(this).click(function(ev){
                    ev.preventDefault();

                    // save data
                    btn = $(this);
                    url = $(this).attr('href');
                    rel = $(this).attr('rel');

                    // hide the product and remove it
                    $(this).parent().hide(200, function(){
                        $(this).remove();

                        // also update on the server
                        $.get(url,  function(data){
                            updateWishlist();

                            // update the class in the products list if it changed
                            if($('a[rel="' + rel + '"]').length == 0){
                                $('#' + rel.substring(7)).switchClass('in_wishlist', '');
                            }
                        });
                    });
                });
            });

            // rebind click event for adding products
            $('.form_add_wishlist').each(function(){
                $(this).rebindUpdate();
                $(this).unbind('submit');
                $(this).submit(function(e){
                   e.preventDefault();

                   // update on the server
                   val = $(this).serialize();
                    $.post($(this).attr('action'), $(this).serialize(), function(message){
                        updateWishlist();
                    });
                });
            });
        });
    }

    // jquery rebind events
    jQuery.fn.rebindUpdate = function(){
        if( ! $(this).hasClass('eventBound')){
            $(this).addClass('eventBound');
            $(this).change(function(){
                $(this).submit();
            });
        }
    }

    // add link to added to wishlist message
    $('div.add_to_wishlist').each(function(){
        sibling = $(this);
        html = sibling.html();
        html = html.replace('$', '<a href="' + sibling.attr('rel') + '">');
        html = html.replace('%', '</a>');
        sibling.html(html);
    });

    // init wishlist
    updateWishlist();

    // add to wishlist
    $('a.add_to_wishlist').click(function(e){
        e.preventDefault();

        // update product class
        var link = $(this);
        link.parents('.hoverinfo').addClass('in_wishlist');

        // update on the server
        $.get($(this).attr('href'), function(){
            updateWishlist();
            link.hide();
            sibling = link.siblings('.add_to_wishlist');
            sibling.show();

            // if first time show them the way to the wishlist
            if($('#hasSubmitWishlist').html() != 1){
                // close the box and show the wishlist
                $('#hasSubmitWishlist').html(1);
                $.fancybox.close();
                $('#wishlist').animate({height: 'show', opacity:'show'}, 400);
            }
        })
    });


    // activate fancybox
    $('.show_tooltip').fancybox({
        'height': '90%'
    });

    // products opacity rollover effect
    $('#product_list li .hideoverflow').animate({opacity: '0.8'}, 200);
    $('#product_list li .hideoverflow').hover(function()
    {
        $(this).stop();
        $(this).animate({opacity: '1.0'}, 200);
    }, function(){
            $(this).stop();
            $(this).animate({opacity: '0.8'}, 200);
    });

    // crossbrowser placeholder
    if(!Modernizr.input.placeholder){

        $("input").each(function(){

            if($(this).val()=="" && $(this).attr("placeholder")!=""){

                $(this).val($(this).attr("placeholder"));
                $(this).focus(function(){

                    if($(this).val()==$(this).attr("placeholder")) $(this).val("");
                });
                $(this).blur(function(){
                    if($(this).val()=="") $(this).val($(this).attr("placeholder"));
                });
            }
        });
    }

    // form validation
    $("#contact_form").validationEngine('attach');
    $('.ajax input[type="submit"]').click(function(event){

        event.preventDefault();

        var form = $(this).parents('form');

        if(form.validationEngine('validate')){
                $.post($(form).attr('action'), $(form).serialize(), function(message){
                $(form).html('<p>' + message + '</p>');
                $('#contact_info').hide();
            });
        }
    });

    // show hide wishlist
    $('.wishlist_btn').click(function(ev){
        ev.preventDefault();
        $('#wishlist').animate({height: 'toggle', opacity:'toggle'}, 400);
    });

    // delete entry from wishlist when they click the remove button
    $('.delete, .closewindow').click(function(ev)
    {
        ev.preventDefault();
        $(this).stop();
        btn = $(this);
        if(btn.hasClass('remove'))
        {
            $(this).remove();
        }
    });

    // menu opacity rollover effect
    $('#product_categories li, #products_menu ul.categories li, .description_product').css({opacity: '0.5'});
    $('#product_categories li, #products_menu ul.categories li, .description_product').hover(function()
    {
        $(this).stop();
        $(this).animate({opacity: '1.0'}, 200);
    }, function(){
        $(this).stop();
        $(this).animate({opacity: '0.5'}, 200);
    });
});

