FE = {};
FE.shopperId = "0104924577409621141031";
FE.newShopperId = '1';
FE.webBugUrl = 'http://shop.meetmark.com/shop/webbug';
FE.webBugSecureUrl = 'https://shop.meetmark.com/shop/webbug';
FE.logout_message = "Are you sure you want to logout?";
FE.logout_url = "http://shop.meetmark.com/shop/pwplogout.asp?url=kathimartin.mymarkstore.com";
FE.initDelay = 2000;

FE.handleAddCouponSubmit = function(e) {
  var coupon = $("#couponCodeTxt").val();
  if(!coupon) return false;
  $.ajax({
    type: "POST",
    url: "/proxy/add_coupon",
    dataType: 'json',
    data: {
      'coupon': coupon,
      'redirectUrl': escape(document.location.href)
    },
    success: function(ret) {
      $('body').append('<img src="/shop" height="1" width="1" />');
      FE.showBasket(ret.data.BagHtml, function() {
        AddCouponCode_Succeeded(ret.data);
      });
    }
  });
  return false;
}

var AddCouponToBasket = FE.handleAddCouponSubmit;

FE.handleRemoveProductClick = function(sku) {
  $.ajax({
    type: "POST",
    url: "/proxy/remove",
    dataType: 'json',
    data: {
      'sku': sku
    },
    success: function(ret) {
      FE.showBasket(ret.data.BagHtml, function() {
        RemoveItemFromBag_Succeeded(ret.data);
      });
    }
  });
  return false;
}

var Bag_RemoveItemFromBag = FE.handleRemoveProductClick;

FE.handleWhyRegisterClick = function() {
  $(this).bt({
    ajaxPath: '/why_register',
    width: '250px',
    positions: ['bottom'],
    ajaxCache: true
  }).btOn();
  H.setBt($(this));
  return false;
}

FE.handleContactMeClick = function() {
  $(this).bt({
    ajaxPath: $(this).attr('href'),
    width: '180px',
    positions: ['bottom']
  }).btOn();
  H.setBt($(this));
  return false;
}

FE.handleRepSendMessageClick = function() {
  $('#navigation_contact_me').trigger('click');
  if(!$('#navigation_contact_me').is(':in-viewport')) $.scrollTo('#navigation_contact_me', {duration:500});
  return false;
}

FE.handleContactMeSubmit = function() {
  var el = $("#contact_me_form");
  if(el.data('clicked')) return false;
  el.data('clicked',true);
  
  var submit_data = H.submitForm(el, "input[type='text'], textarea, select, input[type='hidden']", "[name='cust_prmry_phon_nr'], [name='subject'], [name='message']");

  if(submit_data == false) {
    el.data('clicked',false);
    return false;
  }
  
  $.ajax({
    type: "POST",
    url: el.attr('action'),
    data: submit_data,
    dataType: 'json',
    success: function(ret) {
      if(ret.status == 'error') {
        H.fieldError(ret, el.find('#' + ret.field));
        el.data('clicked',false);
        return false;
      }          
      H.notifySuccess(ret.data.message);
      $('#navigation_contact_me').btOff()
      el.data('clicked',false);
    }
  });      
  return false;
}

FE.parseProduct = function(rel) {
  var arr = rel.split('_');
  
  return {
    product_id: arr[0],
    sku: arr[1],
    price: arr[2]
  };
}

FE.addProduct = function(rel) {
  H.clearBt();
  var arr = FE.parseProduct(rel);
  $.ajax({
    type: "POST",
    url: "/proxy/add",
    dataType: 'json',
    data: {
      'sku': arr.sku,
      'product_id': arr.product_id
    },
    success: function(ret) {
      FE.showBasket(ret.data.BagHtml, function() {
        AddToBag_Succeeded(ret.data);
      });
    }
  });
  return false;
}

FE.handleAddProductClick = function() {
  FE.addProduct($(this).attr('rel'));
  return false;
}

FE.handleAddVariantClick = function() {
  FE.addProduct($(this).attr('rel'));
  return false;
}

FE.showBasket = function(html, cb) {
  $('#shopping_bag_container').fadeOut('fast', function() {
    $(this)
      .addClass('hidden')
      .html(html)
      .fadeIn('fast', function() { 
        $(this).removeClass('hidden');
        if($.isFunction(cb)) cb();
      });
  });
}

FE.getBasket = function() {
  $.ajax({
    type: "GET",
    url: "/proxy/basket",
    dataType: 'json',
    success: function(ret) {
      FE.showBasket(ret.data.BagHtml);
    }
  });
}

FE.handleShoppingBagIconClick = FE.getBasket;

FE.initShopperId = function() {
  setCookie('shopperID', FE.shopperId, 3000);
  $('body').append('<img src="' + FE.webBugUrl + '/' + FE.shopperId + '.jpg" height="1" width="1" />');
  setTimeout("FE.getBasket();",FE.initDelay); 
}

FE.logout = function(cb) {
  $.ajax({
    'method': 'POST',
    'url': FE.logout_url,
    'dataType': 'jsonp'
  });
  $.ajax({
    'method': 'POST',
    'url': '/logout',
    'dataType': 'json',
    success: cb
  });
}

FE.handleLogoutClick = function() {
  if(!confirm(FE.logout_message)) return false;
  FE.logout(function() {
    //$('#header_customer_logged_in, #header_customer_logged_in_edit').fadeOut('fast',function() {
    //$('#header_customer_sign_in, #header_customer_register').fadeIn('fast');
    H.redirect('/');
  });
  return false;
}

FE.handleNotYouClick = function() {
  if(!confirm(FE.logout_message)) return false;
  FE.logout(function() {
    H.redirect('/login');
  });
  return false;
}

FE.ready = function() {
  $('#why_register').hover(FE.handleWhyRegisterClick, function() { $('#why_register').btOff(); });
  $('#navigation_contact_me').click(FE.handleContactMeClick);
  $('#rep_send_message').click(FE.handleRepSendMessageClick);
  $('#contact_me_form input, #contact_me_form textarea')
    .live('focus', H.inputFocus)
    .live('blur', H.inputBlur);
  $('#contact_me_form_submit').live('click', FE.handleContactMeSubmit);
  $('#contact_me_form').live('submit', FE.handleContactMeSubmit);
  
  $('#pwp_logout_link').click(FE.handleLogoutClick);
  $('#pwp_not_you_link').click(FE.handleNotYouClick);
  
  $('.product_add_no_variants, .product_view_add').live('click', FE.handleAddProductClick);
  $('.product_view_variant_add').live('click',FE.handleAddVariantClick);  
  
  $('#couponUpdateBtn').live('click',FE.handleAddCouponSubmit);
  
  if(FE.newShopperId && $('#shopping_bag_container').html() == '') FE.initShopperId();
}
$(H.ready);$(FE.ready);$(Shared.ready)
