// JavaScript Document// JavaScript Document

// Mate Functions 

jQuery(document).ready(function($) {
	
var siteURL = 'http://www.matewine.com/';


/*
if ((screen.width<=800) && (screen.height<=600)) {
 window.location.replace('http://example.com/800-600-or-less');
}
else {
   window.location.replace('http://example.com/greater-than-800-600');
}
*/



$('#supersize').cycle({
    fx:     'fade',
    timeout: 7700,
    speed:  3000,
	pager:  '#cycle-nav'
	/*
	after: function(){
           
		  if($(this).attr('id') == 'Tuscany') {
 
		   
sIFR.replace(berolina, {
  selector: '.quote-Tuscany h1',
	wmode: 'transparent',
  
  css: [
		'.sIFR-root {   color: #FFFFFF; text-align:center; font-size:19px; letter-spacing: 1;}',
		'.bold {  color: #CA1317; font-size:24px; font-weight:900; }'	
		]
	});


		  } else if($(this).attr('id') == 'Kitchen') {

sIFR.replace(berolina, {
  selector: '.quote-Kitchen h1',
	wmode: 'transparent',
  
  css: [
		'.sIFR-root {   color: #FFFFFF; text-align:center; font-size:19px; letter-spacing: 1;}',
		'.bold {  color: #CA1317; font-size:24px; font-weight:900; }'	
		]
	});

		  } else if($(this).attr('id') == 'Vineyard') {


sIFR.replace(berolina, {
  selector: ' .quote-Vineyard h1',
	wmode: 'transparent',
  
  css: [
		'.sIFR-root {   color: #FFFFFF; text-align:center; font-size:19px; letter-spacing: 1;}',
		'.bold {  color: #CA1317; font-size:22px; font-weight:900; }'	
		]
	});

		} else if($(this).attr('id') == 'Cyprus') {
	
	sIFR.replace(berolina, {
  selector: '.quote-Cyprus h1',
	wmode: 'transparent',
  
  css: [
		'.sIFR-root {   color: #FFFFFF; text-align:center; font-size:19px; letter-spacing: 1;}',
		'.bold {  color: #CA1317; font-size:22px; font-weight:900; }'	
		]
	});
	
	}
	
	}*/
});


/**** CART AJAX ****/

  // bind to the change event of the state select in the checkout form
    $('.buy-now').click(function(){
   	
	   //Update the form with the productID
	    $('#entry_id').val(  $(this).attr("id").slice(7) );
		
		//Update the form with the quantity	
		$('#quantity').val( $('#add_quantity'+$(this).attr("id").slice(7)+'').val() );
		
		 //Get the product ID for notifications
	     product = $(this).attr("id").slice(7);
	   		

        // use the jquery form plugin method ajaxSubmit to send the form
        $('#my_add_to_cart_form').ajaxSubmit({
            success: function(data) {
                // load the updated tax value back into the page
               $('#cart_subtotal').load(siteURL + 'cart/cart_subtotal');
               $('#items_in_cart').load(siteURL + 'cart/items_in_cart');
			    $('#notification'+product+'').load(siteURL + 'cart/you_just_added/' + product, function(){ 
								$('.close-notification').click(function(){
									$('div.notification').html('');
											return false;
												});
										});
            				} });
				return false;
  	  });
	

		
	$('.remove_item').click(function(){
   	
		$(this).parent().parent().html(''); 
	   //Get the product ID
	   // $('#entry_id').val(  $(this).attr("id").slice(7) );
		
//		alert($(this).attr("id").slice(11)); 
		
		 $('#remove_from_cart').load(siteURL + 'cart/remove_from_cart/' + ($(this).attr("id").slice(11)) + '', function(){
			 $('#cart_tax').load(siteURL + 'cart/cart_tax', function(){ 
			 	$('#items_in_cart').load(siteURL + 'cart/items_in_cart', function(){ 
						 $('#cart_total').load(siteURL + 'cart/cart_total', function(){ 
							 $('#cart_subtotal, #checkout_subtotal').load(siteURL + 'cart/cart_subtotal', function(){ 
					  			   $('#cart_shipping').load(siteURL + 'cart/cart_shipping');
									 });						 
								});
						 }); 
					});
			 });
			 
		return false;

    });
			
	
     function armClear() {
		 
	   
	   $('#empty_cart').click(function(){
		   		   
                // load the updated tax value back into the page
				$('#clear_cart').load(siteURL + 'cart/clear_cart', function() { 
					$('#cart_subtotal').load(siteURL + 'cart/cart_subtotal', function() { 				
					     $('#items_in_cart').load(siteURL + 'cart/items_in_cart');
				
					});
				
				});
				
				return false;	
   								
			 });
	   }
	   
	   
	   
	   //Runtime
	   armClear(); 
	   
	   //Make sure it works after we loads in dynamically
	   $('#items_in_cart').ajaxComplete(function() {
  		
		armClear(); 
		
		});
		
  $('#country_code').change(function(){

          $('#hidden_country_field').val( $('#country_code').val() );
		  
        // use the jquery form plugin method ajaxSubmit to send the form
      
	    $('#save_customer_info_form').ajaxSubmit({
            success: function(data) {
                // load the updated tax value back into the page
			   $('#cart_tax').load(siteURL + 'cart/cart_tax');			  
              //$('#cart_shipping').load(siteURL + 'cart/cart_shipping'); 
				 $('#cart_total').load(siteURL + 'cart/cart_total');			  
	               // $('#cart_total').load('{path=includes/cart_total}');
            }
        });
    });	
	
	
	function updateShipping(){ 
	
	 $('#hidden_shipping_country_field').val( $('#shipping_country_code').val() );
        // use the jquery form plugin method ajaxSubmit to send the form
	    $('#save_customer_info_form').ajaxSubmit({
            success: function(data) {
                // load the updated tax value back into the page
//			   $('#cart_tax').load(siteURL + 'cart/cart_tax');			  
                $('#cart_shipping').load(siteURL + 'cart/cart_shipping');
				 $('#cart_total').load(siteURL + 'cart/cart_total');			  
	               // $('#cart_total').load('{path=includes/cart_total}');
            }
        });
	
	
	}

$('#shipping_country_code').change(function(){
	
  updateShipping();
    
	 });


  // Copy ship to
      $("input#same").click(function(){ 
          if ($("input#same").is(':checked')) 
          { 
              // Checked, copy values 
              $("input#shipping_first_name").val($("input#first_name").val());
              $("input#shipping_last_name").val($("input#last_name").val());
              $("input#shipping_address").val($("input#address").val());
              $("input#shipping_address2").val($("input#address2").val());
              $("input#shipping_city").val($("input#city").val());
              $("select#shipping_state").val($("select#state").val());
              $("input#shipping_zip").val($("input#zip").val());
              $("select#shipping_country_code").val($("select#country_code").val());
		

          } 
          else 
          { 
              // Clear on uncheck 
              $("input#shipping_first_name").val("");
              $("input#shipping_last_name").val("");
              $("input#shipping_address").val("");
              $("input#shipping_address2").val("")
              $("input#shipping_city").val("");
              $("select#shipping_state").val("");
              $("input#shipping_zip").val("");
              $("select#shipping_country_code").val("");
			  
          } 
		  
		   updateShipping();
		  
      });




//Background image
$("div#supersize img").supersize();		
				
		
$('#modal-container').ajaxComplete(function(e, xhr, settings) {
			
	//Make sure the Ajax call is for a review
	if (settings.url.indexOf("AJAX/reviews/") != -1 || settings.url.indexOf("AJAX/book_details/") != -1  || settings.url.indexOf("AJAX/contact/") != -1 ) {
		
		$('#modal-container').jScrollPane();	
	
		$('#modal-container').fadeIn(2000);	

	 /*  	sIFR.replace(berolina, {
 		 selector: '.modal-content h2',
		wmode: 'transparent',
  
		  css: [
			'.sIFR-root {   color: #FEFFEA; text-align:left; font-size:24px; letter-spacing: 1;}'
			]
		});
		
		*/
	
	}  if (settings.url.indexOf("AJAX/contact") != -1) {
	
	
	$('#mail-btn').click(function () {

	$.post(siteURL + "AJAX/mailer/", $("#contactForm").serialize(), successFunction);
	
	
	});

	successFunction = function() {
	
	$('#mailing-container').fadeOut(1200);
	$('#thank-you').html('<p>Thank You</p>');
		
			}
		}
});




var id = '';


//MODAL Windows

setWindow = function() {

//Get the screen height and width
		var maskHeight = $(document).height();
		var maskWidth = $(window).width();
	
		//Set heigth and width to mask to fill up the whole screen
		$('#mask').css({'width':maskWidth,'height':maskHeight});
		
		//transition effect		
		$('#mask').fadeIn(1000);	
		$('#mask').fadeTo("slow",0.85);	
	
		//Get the window height and width
		var winH = $(window).height();
		var winW = $(window).width();
              	
	
}

//select all the a tag with name equal to modal
	$('a[name=modal], a[name=modal-books]').click(function(e) {
		//Cancel the link behavior
		e.preventDefault();
				
		//Get the A tag
		 id = $(this).attr('href');
	
			setWindow();
		
		if($(this).attr('name') == 'modal') {
		
		$('#modal-container').load(siteURL + "AJAX/reviews/" + $(this).attr("href").slice(7)+'');
		
		} else {
			
		$('#modal-container').load(siteURL + "AJAX/book_details/" + $(this).attr("href").slice(7)+'');
	
		}
		
		return false;
		
	});
	
	
	$('a#mailing').click(function() {
		
		
		setWindow();

		$('#modal-container').load(siteURL + "AJAX/contact/");
		
        
		 return false;

	
	});
	

	
	//if close button is clicked
	$('.window .close').click(function (e) {
		//Cancel the link behavior
		e.preventDefault();

		//$('#reviews').jScrollPaneRemove();
		$('#modal-container').hide();	
		$('#mask').hide();
		
		return false;

	});		
	
	
	
	//if mask is clicked
	$('#mask').click(function () {
	$('#modal-container').jScrollPaneRemove();
	$('#modal-container').hide();
	//	$('#reviews').fadeTo("fast", 0);
		
		$(this).hide();
	
	});	
	
	var samplersY = $('div.product_sampler_container').first().position().top;
	
	$('a.available-in-sampler').click(function (e) {
		
		e.preventDefault();
		
		 $('html, body').animate({ scrollTop: samplersY }, 800);
		
		return false;

	});			



Shadowbox.init();

$(".gallery img").hover(
	  function(){
		$(this).fadeTo("fast", 0.35);
		//$(this).find("div.landing-caption").fadeIn(400);
	  },
	  function(){
		   $(this).fadeTo("fast", 1);

		//$(this).find("div.landing-caption").fadeOut(400);
	  }
	);
	
	
		
	
	

});



