$ = jQuery;

$(document).ready(function(){

	$("div.shopping-cart-wrapper").click(function(){
	
		$(this).fadeOut('fast',function(){
			
			$(this).empty().show();
		});
	});
	
	$('div#home_content ul.carousel').each(function(){
		
        
    	$(this).parent().find("a.next").click(function(){
    		
    		$(this).parent().find('ul.carousel').data('jcarousel').next();
    	});
    	
    	$(this).parent().find("a.prev").click(function(){
    		
    		$(this).parent().find('ul.carousel').data('jcarousel').prev();
    	});
    	
		$(this).jcarousel({
	        scroll: 3,
	        wrap: false,
	        buttonNextCallback: function(c,el,enabled){
	        	if( enabled) $(el).show();
	        	if(!enabled) $(el).hide();
	        	
	        	if( enabled) $(el).parent().parent().find('a.next').show();
	        	if(!enabled) $(el).parent().parent().find('a.next').hide();
	        },
	        buttonPrevCallback: function(c,el,enabled){
	        	if( enabled) $(el).show();
	        	if(!enabled) $(el).hide();
	        	
	        	if( enabled) $(el).parent().parent().find('a.prev').show();
	        	if(!enabled) $(el).parent().parent().find('a.prev').hide();
	        }
	    });
		
    });
		
	$("body form#properties input").change(function(){
		
		$("form#properties").submit();
	
	});
	
	$("body.single ul#wijnen.single div.wine_thumb a,td.firstcol div.wine_thumb a").fancybox({ 
		'titleShow'		:	false, 
		'overlayShow'	:	true, 
		'overlayColor'	:	'#000'
	});
	
	
	if($("tr#no_quote").length>0){
		$("form#change_country").submit();
	}
	
	// Fix product thumbs on single wine pages to match sizes with their borders
	
	$("ul#wijnen.single li div.wine_thumb a img").each(function(){

		if($(this).height()<128){
			$(this).parent().parent().css("height",$(this).height()+"px");
		}
		
	});
});


// WP E-Commerce function overrides

var WPSC_URL = "/wp-content/plugins/wp-e-commerce";

var amountSubmitted = 0;


function singleCheckSoort(el){
	$("input.notkleur").removeAttr("checked");
	$(el).attr("checked","checked");
	
	return true;
}

function singleCheckKleur(el){
	$("input.kleur").removeAttr("checked");
	$(el).attr("checked","checked");
	
	return true;
}

function paginate(start){
	$("form#properties input#start").val(parseInt(start));
	$("form#properties").submit();
}

function showAll(){
	$("form#properties input#start").val(0);
	$("form#properties input#toonalles").val(1);
	$("form#properties").submit();
}

function submitform(frm, show_notification)
{
  
  amountSubmitted = frm.quantity.value;
  
  if(show_notification != false)
  {
    show_notification = true;
  }
  
  ajax.post("./?ajax=true&user=true",getresults,ajax.serialize(frm));
  
  if(document.getElementById('loadingimage') != null)
  {
    document.getElementById('loadingimage').src = WPSC_URL+'/images/indicator.gif';
    document.getElementById('loadingindicator').style.visibility = 'visible';
  } 
  else if(document.getElementById('alt_loadingimage') != null)
  {
    document.getElementById('alt_loadingimage').src = WPSC_URL+'/images/indicator.gif';
    document.getElementById('alt_loadingindicator').style.visibility = 'visible';
  }     
  
  if((show_notification == true) && (document.getElementById('fancy_notification') != null))
  {
	var options = {
	  margin: 1 ,
	  border: 1 ,
	  padding: 1 ,
	  scroll: 1 
	};
	
	form_button_id = frm.id + "_submit_button";

	var container_offset = {};
	new_container_offset = jQuery('#products_page_container').offset(options, container_offset);
	
	if(container_offset['left'] == null) {
	  container_offset['left'] = new_container_offset.left;
	  container_offset['top'] = new_container_offset.top;
	}
	
	var button_offset = {};
	new_button_offset = jQuery('#'+form_button_id).offset(options, button_offset)
	
	
	if(button_offset['left'] == null) {
	  button_offset['left'] = new_button_offset.left;
	  button_offset['top'] = new_button_offset.top;
	}
	
	jQuery('#fancy_notification').css("left", (button_offset['left'] - container_offset['left'] + 10) + 'px');
	jQuery('#fancy_notification').css("top", ((button_offset['top']  - container_offset['top']) -60) + 'px');
	
	jQuery('#fancy_notification').css("display", 'block');
	jQuery('#loading_animation').css("display", 'block');
	jQuery('#fancy_notification_content').css("display", 'none');  
  }
  
  return false;
}
  
var getresults=function(results) {
	eval(results);
	if(window.drag_and_drop_cart_updater) {
		drag_and_drop_cart_updater();
	}
	if(document.getElementById('loadingimage') != null) {
		document.getElementById('loadingindicator').style.visibility = 'hidden';
	} else if(document.getElementById('alt_loadingimage') != null) {
		document.getElementById('alt_loadingindicator').style.visibility = 'hidden';
	}
	if((document.getElementById('sliding_cart') != null) && (document.getElementById('sliding_cart').style.display == 'none')) {
		jQuery("#fancy_collapser").attr("src", (WPSC_URL+"/images/minus.png"));
		jQuery("#sliding_cart").show("fast",function(){
		ajax.post("./",noresults,"ajax=true&set_slider=true&state=1"); });
	}
	if(document.getElementById('fancy_notification') != null) {
		jQuery('#loading_animation').css("display", 'none');
	}
	
	jQuery('div.shopping-cart-wrapper p span:first').append("<br style='display:inline;'><br style='display:inline;'><a href='javascript:void(0)' onclick='hideNotification()' style='display:inline;float:right;color:#0089C4;'>Verder winkelen</a><a style='display:inline;color:#0089C4;' href='/winkel/afrekenen/'>Afrekenen</a>");
	
	setTimeout("hideNotification()",8000);
	
	updateCartBox(amountSubmitted);
	
	amountSubmitted = 0;
}

function updateCartBox(amount){
	
	/*
	if($('#cart_status #currentAmount').length!=0){
		
		var currentAmount = parseInt($('#cart_status #currentAmount').text());
		
		var newAmount = parseInt(parseInt(currentAmount) + parseInt(amount));
		
		$('#cart_status #currentAmount').html(""+newAmount);
		
		if(newAmount == 1){
			$('#amountSuffix').empty();
		}else{
			$('#amountSuffix').html('sen');
		}
		
	}else{
	*/
		$("#cart_status").html('<a href="/winkel/afrekenen/" id="checkout">Mijn bestelling</a>');
				
 //	}
	
}

function hideNotification(){

	$("div.shopping-cart-wrapper").fadeOut('fast',function(){
			
		$(this).empty().show();
	});
}
