
$(document).ready( function() {
    $('#nav > li').hover( 
        function () { $(this).addClass('sfhover'); },  
        function () { $(this).removeClass('sfhover'); }
                      );

    /* ACTIVATE LIGHTBOX */
    if ($.fn.lightbox) {
        $('#thumbContnr a.picPop, a.lightbox').lightbox();
    }

    /* activate cart deleting x's */
    $('td.cart_del a').bind('click', function() {
        if (confirm("Remove this item from your purchase?")) {
            var url = document.location.href.replace(/[\?&]op_del=[^&\?$]+/g, ''); // remove any ?op_del=sxx112 from the url
            var sep = (url.match(/\?/))? '&' : '?'; 
            document.location.replace(url + sep + 'op_del=' + this.rel);
        }
        $(this).blur();
        return false;
    });


    /* init any videos..? */
    if ($.fn.media) {
        $('a.embedmedia').media({ width: 400, height: 300, autoplay: true, type: 'flv' });
    }

    $('#maillist a, #op_subscribe').bind('click', footerform.action);
    $("#mailinglist-form form").bind('submit', function() { footerform.submit(); return false; });
    footerform.init();

});

(function( $ ){
    function get_nat_w(elem) {
        if (!$(elem).data('nat_w'))
            $(elem).data('nat_w', $(elem).width());
        return $(elem).data('nat_w');
    }

  $.fn.naturalSlide = function(duration, direction) {
      if (direction === 0)  {
          return $(this).animate({width:0}, duration, function() { $(this).css('display', 'none'); });
      } else {
          var to_w = get_nat_w(this);
          return $(this).css({width:0,overflow:'hidden',display:'block'}).animate({width:to_w}, duration, function() { $(this).css('width', 'auto'); });
      }
  };
})( jQuery );


var footerform = {

    state : 0,

    init: function() {

        this.def_h = $('#maillist').height();
        this.adder_h = $('#maillist .info').outerHeight();

        $('#mailinglist-form').ajaxError( function() { 
                                                    $('#mailinglist-form input').remove();
                                                    $('#maillist button').remove();
                                                    $('#maillist .info').hide();
                                                    $(this).find('.notice').addClass('error').text('Sorry, an error occurred. Please try again later.'); });

    },
    action: function(e) {
        if (this.id == 'op_subscribe' && footerform.state == 1) footerform.submit();
        else if (footerform.state == 1) footerform.close();
        else footerform.open();
        $(this).blur();
        return false;
    },
    open : function(e) {
        $('#mailinglist-form').naturalSlide(500);
        $('#maillist').animate({height:this.def_h + this.adder_h});
        $('#maillist .info').show();
        footerform.state = 1;
    },
    close : function(e) {
        $('#mailinglist-form').naturalSlide(500, 0);
        $('#maillist .info').hide();
        $('#maillist').animate({height:this.def_h});
        footerform.state = 0;
    },
    submit : function(e) {
        if (footerform.state == 0) {
            return footerform.open();
        }
        else {
            footerform.lookbusy();
            $.get($("#mailinglist-form form").attr('action'), $("#mailinglist-form form").serialize(), footerform.handleSubmit, 'json');
            return false;
        }
    },
    lookbusy:function() {
            $('#op_subscribe').attr('disabled', true).css('opacity', 0.5);
    },
    lookidle:function() {
            $('#op_subscribe').attr('disabled', false).css('opacity', 1);
    },
    handleSubmit: function(data) {
        $('#op_subscribe').attr('disabled', false).css('opacity', 1);
        if (data.validationError) {
            footerform.lookidle();
            $('#maillist .info').addClass('error').text(data.validationError);
        }
        else if (data.error) {
            $('#maillist .info').html(data.error);
        }
        else {
            $('#mailinglist-form input').remove();
            $('#op_subscribe').hide();
            $('#maillist .info').hide();
            $('#mailinglist-form .notice').text('Thank you for subscribing! Please check your email for confirmation.');
        }
    }

};


