$(function(){
    
  // This function alters any functionality needed for mobile layout
  function enableMobileLayout() {
	  
	  if ( $(window).width() < 640 ){
		  
		  // wrap menu in an accordion style expandable menu
		  $('body').addClass('mobileEnabled');
		  
		  if( $('.pt_storefront').length == 0 ) {
		  
			  $('#primaryNavigationList').wrap('<div class="mobileMenu" />');		  
			  $('.mobileMenu').prepend('<h2 class="mobileMenuToggle">View Navigation</h2>');		  
			  $('.mobileMenu h2').toggle(
					  function(){
						  $('#primaryNavigationList').fadeIn('fast');
						  $(this).text('Hide Navigation');
					   }, 
					   function(){
						   $('#primaryNavigationList').fadeOut('fast');
						   $(this).text('Show Navigation');
					   }
			  );
		  
		  }
		  else {
			  
			  var homepageAssetHeight = $('.mobile-home-content-slot').height();
			  console.log(homepageAssetHeight);
			  
			  $('.categorymenu')
			  	.css( 'top' ,  homepageAssetHeight + 135 )
			  	.children('#primaryNavigationList')
			  	.show();
		  };
		  
		  
		  
		  $('#primaryNavigationList .subMenuContent:has(a)').siblings('a').toggle(
				  function(e){
					  $(this).siblings('div').children('ul').fadeIn('fast');
				  },
				  function(){
					  $(this).siblings('div').children('ul').fadeOut('fast');
				  }

		  );
		  
		  $('#primaryNavigationList .subcategories:has(a)').siblings('a').toggle(
				  function(e){
					  $(this).siblings('div').fadeIn('fast');
				  },
				  function(){
					  $(this).siblings('div').fadeOut('fast');
				  }

		  );
		  
		  
		  // disable quickview links
		  $('.producttile .swatches a.swatch').click(function(e){return false;});
		  
		  // remove anything from the DOM that we don't need
		  $('.homepage').remove();
		  
		  // fix tool tips (no hover)
		  $('a.tooltip').removeClass('tooltip').click(function(e){
			  $(this).children('.tooltip-body').toggle();
			  return false;
		  });
		  
		  // fix caching of form fields
		  $('#dwfrm_profile_customer_emailconfirm, #dwfrm_profile_login_password, #dwfrm_profile_login_passwordconfirm').attr('autocomplete','off');
		  
		  // fix andriod form submit via virtual keyboard
		  $(".pt_checkout input").bind('keypress', function(e){
		       var key;
		        if(window.event)
		            key = window.event.keyCode;
		        else
		            key = e.which;
		        if(key == 13 || key == 10)
		            return false;
		        else
		            return true;
		  });
		  
		  // update toggle link so user can select full site
		  $('#full-site-link')
		  	.attr("href", app.URLs.staticPath)
		  	.text("View Full Site")
		  	.click(function(e) {
			  	e.preventDefault();
			  	$.ajax({
			  	  url: app.URLs.FullSite,
			  	  success: function(){
			  		window.location.reload();
			  	  }
			  	});
		  	}
		  );

		  return false;
	  }
  };
  
  // fire the resize function once on load (no resize called yet)
  enableMobileLayout();

});

