$(document).ready(function(){
    prep_freight_form();
    $(".color").bind("click", function(){
        var image = $(this).attr("href");
        $(".color-image").attr({
            src:image
        });
        return false;
    });
    
    // modal window for when users exceed their download limit for a song
    $(".email").click(function(){
        $.get("/products/email/", function(content){
            $.nyroModalManual({
                bgColor: '#1b1b1c',
                height: 500,
                css: {content: { overflow: 'hidden'}},
                content: content
            });
            $("#commentForm").validate({
                errorPlacement: function(error, element) {
                    error.appendTo( element.parent("td").parent("tr").prev(".error") )
                },
                submitHandler: function(form){
                    $(form).ajaxSubmit(function(){
                        $("#email_friend .inner").prepend("<p class='success'>Your message was sent successfully.  You can use this form to send a message to someone else, or you can <a class='nyroModalClose' href=''>close this window</a> to continue browsing products.</p>");
                    });
                }
            });
        });
    return false;
    });
    
    $(".larger").click(function() {
        link = $(this).attr("href");
        $.nyroModalManual({
            content: "<img src='" + link + "'>"
        });
        return false;
    });
});

function prep_freight_form(){
    $('.freight-form').submit(function() {
        $(".shipping_results").html("<p class='ajax_wait'><img src='/static/images/ajax-loader.gif' />Loading Data...</p>");
        $(".disclaimer").remove();
        var data = $(this).serialize();
        $.ajax({
            type: "POST",
            url: "/shipping/",
            data: data,
            success: function(html){
                $('.freight-calculator').html(html);
            },
            complete: function(XMLHttpRequest, textStatus){
                prep_freight_form();
            }
        });
        return false;
    });
}