$(function() {
  $('#card_number').keyup(function() {
    $(this).val($(this).val().replace(/[^0-9]/g, ''));
  });
  $('#subscribe-payment-form').submit(function() {
    $('#subscribe-payment-button').attr("disabled", "disabled");
    return true;
  });
  $('#category_nav li a.parent').click(function() {
    var liParent = $(this).parent();
    if(liParent.hasClass('active')) {
      $(this).siblings('ul').slideUp('slow', function() {
        $(liParent).removeClass('active');
      });
    } else {
      $(this).siblings('ul').slideDown('slow', function() {
        $(liParent).addClass('active');
      });
    }
    return false;
  });
  $('#btn_product_purchase_with_cart').click(function() {
    $('#purchase_with_credits_fieldset').fadeOut('slow', function() {
      $('#product_purchase_box').fadeIn('slow');
    });
    
  });
  $('#link_login').live('click', function() {
    if(!$('#dialog_box').length) {
      $('body').append('<div id="dialog_box"></div>');
    }
    //load up the login module
    $('#dialog_box').dialog({
        modal: true,
        open: function ()
        {
            $(this).load('/customer/login');
        },
        close: function() {
          $(this).html('');
        },
        height: 280,
        width: 500,
        title: 'Customer Login'
    });
    return false;
  });
  $('#customer_login_form').live('submit', function() {
    if($(this).parent().attr('id') == 'dialog_box') {
      $.post('/customer/login', $(this).serialize(), function(response) {
        if(response == 'success') {
          window.location = window.location;

        } else {
          $('#dialog_box').html(response);
        }
      });
      return false;
    }
    return true;
  });
  $('.link_tos').click(function() {
    if(!$('#dialog_box').length) {
      $('body').append('<div id="dialog_box"></div>');
    }
    var url = $(this).attr('href');
    $('#dialog_box').dialog({
        modal: true,
        open: function ()
        {
            $(this).load(url);
        },
        close: function() {
          $(this).html('');
        },
        height: 280,
        width: 500,
        title: 'AIVectorArt Terms of Service'
    });
    return false;
  });
  $('#cart_tab_control li a').click(function() {
    if(!$(this).parent().hasClass('active')) {
      $(this).parent().siblings('.active').removeClass('active');
      $(this).parent().addClass('active');
      if($(this).parent().attr('id') == 'tab-categories') {
        $('#cart-action-checkout').fadeOut('slow', function() {
          $('#cart-action-categories').fadeIn('slow');
        })
      } else {
        $('#cart-action-categories').fadeOut('slow', function() {
          $('#cart-action-checkout').fadeIn('slow');
        })
      }
    }
    return false;
  });
  $('#btn_forgot_password').live('click',function() {
    $('#customer_login_form').fadeOut('slow', function() {
      $('#customer_reset_form').fadeIn('slow');
    })
  });
  $('#btn_account_login').live('click',function() {
    $('#customer_reset_form').fadeOut('slow', function() {
      $('#customer_login_form').fadeIn('slow');
    })
  });
  $('#credit_purchase_form').submit(function() {
    if(!$('#i_agree').is(':checked')) {
      alert('Please check the "I agree" box above');
      return false;
    }
    
    //ajax call to the page
    /*
    $.post($(this).attr('action'), $(this).serialize(), function(response) {
      alert(response);
      if(response != 'failed') {
        $('#purchase_with_credits_fieldset').fadeOut('slow', function() {
          $('#product_purchase_box').html(response).fadeIn();
        })
      }
      
    });
    */
    return true;
  });
  if($('#clipart-download-form').length) {
    $('#clipart-download-form').attr('onsubmit', '');
    $('#clipart-download-form').submit(function() {
      if(!$('#dlg-download').length) {
        $('body').append('<div id="dlg-download" style="display:none;"><p>The system is converting your vector art image into your select clipart format and size, this takes around 20-30 seconds on up to a minute. Once completed your download will start.</p><div align="center"><img src="/images/loading.gif" /></div></div>');
      }
      $('#dlg-download').dialog({
        modal:true,
        title:'Converting to Clipart',
        height: 250,
        width: 420
      });
      $('#dlg-download').find('img').attr('src', '/images/loading.gif');
      var downloadLink = $(this).attr('action')+'?id='+$(this).find('input[name=id]').val()+'&format='+$(this).find('select[name=format]').val()+'&size='+$(this).find('select[name=size]').val();
      //use jquery to load the convert first
      $.post('/customer/convert', $(this).serialize(), function(response) {
        setTimeout('dlgDownlClose()', 1000);
        window.location = downloadLink;
      });
      return false;
    });
  }
  $('input[name=term]').focus(function() {
    if($('input[name=term]').val() == 'Search Over 24K Graphics') {
      $('input[name=term]').css('color', '#000').val('')
    }
  });
  $('input[name=term]').blur(function() {
    if($('input[name=term]').val() == '') {
      $('input[name=term]').css('color', '#888').val('Search Over 24K Graphics')
    }
  });
  if(!$('input[name=term]').val()) {
    $('input[name=term]').css('color', '#888').val('Search Over 24K Graphics');
  }
  
})

function dlgDownlClose() {

  $('#dlg-download').dialog('close');
}
