/* Predefined variables */

	var preLoader = '<div id="preloadContainer"><div id="preloadOverlay"></div><div id="preloader"></div></div>';
	var boolBusy = false;
	var stamId = '';
	var stamTitel = '';
	var bestelTitel = '';

$(document).ready(function() {

$('select#broodje_select').selectmenu();
	/* Lightbox for jQuery */
$("a[rel^='click']").bind({
	click: 	function(){
			stamId = $(this).attr("href").replace("#","");
			stamTitel = $(this).attr("title");		
			stamTitel = stamTitel.replace("Stel zelf uw broodje samen","");
		}
});
$("a[rel^='click2']").bind({
	click: 	function(){
			stamId = $(this).attr("href").replace("#","");
			stamTitel = $(this).attr("title");		
			stamTitel = stamTitel.replace("Kies uw drankje","");
		}
});
$("a[rel^='prettyPhotov2']").prettyPhoto({animationSpeed:'slow',theme:'facebook'});
$("a[rel^='prettyPhoto']").prettyPhoto({animationSpeed:'slow',theme:'facebook',show_title:false,social_tools:false,markup: '<div class="pp_pic_holder"> \
	<div class="ppt">&nbsp;</div> \
	<div class="pp_top"> \
		<div class="pp_left"></div> \
		<div class="pp_middle"></div> \
		<div class="pp_right"></div> \
	</div> \
	<div class="pp_content_container"> \
		<div class="pp_left"> \
		<div class="pp_right"> \
			<div class="pp_content"> \
				<div class="pp_loaderIcon"></div> \
				<div class="pp_fade"> \
					<a href="#" class="pp_expand" title="Expand the image">Expand</a> \
					<div class="pp_hoverContainer"> \
						<a class="pp_next" href="#">next</a> \
						<a class="pp_previous" href="#">previous</a> \
					</div> \
					<div id="pp_full_res"></div> \
					<div class="pp_details"> \
						<a class="pp_close" href="#" >Close</a> \
					</div> \
				</div> \
			</div> \
		</div> \
		</div> \
	</div> \
	<div class="pp_bottom"> \
		<div class="pp_left"></div> \
		<div class="pp_middle"></div> \
		<div class="pp_right"></div> \
	</div> \
</div> \
<div class="pp_overlay"></div>'
						});	


	$("iframe").each(function(){
    var ifr_source = $(this).attr('src');
    var wmode = "?wmode=transparent";
    $(this).attr('src',ifr_source+wmode);
	});

	$("ul.subnav").parent().append("<span></span>"); //Only shows drop down trigger when js is enabled - Adds empty span tag after ul.subnav
	
	$("div.select").click(function() { //When trigger is clicked...
		
		//Following events are applied to the subnav itself (moving subnav up and down)
		$(this).parent().find("ul.subnav").slideDown('fast').show(); //Drop down the subnav on click

		$(this).parent().hover(function() {
		}, function(){	
			$(this).parent().find("ul.subnav").slideUp('slow'); //When the mouse hovers out of the subnav, move it back up
		});

		//Following events are applied to the trigger (Hover events for the trigger)
		}).hover(function() { 
			$(this).addClass("subhover"); //On hover over, add class "subhover"
		}, function(){	//On Hover Out
			$(this).removeClass("subhover"); //On hover out, remove class "subhover"
	});

	//$('form.alertBox').bind({

	$('form.refreshVestiging').bind({
		submit: 	function(){
							var alertStr;
							alertStr = '';
							//alertStr = "Je wijzigt nu van vestiging.";
							if ($(this).find('input[name=Vestiging_Popup]').val() == 1) {alertStr = "Bij deze vestiging kun je alleen telefonisch bestellen.\nWanneer je verder gaat wordt je winkelmandje dan ook geleegd.\nTelefoonnummer: "+$(this).find('input[name=Vestiging_Telefoonnummer]').val();}
							if (alertStr != '') return (confirm(alertStr+"\n\nWil je verder gaan?"));
						}
	});
	blankWin();
	slider();
	bindRichInteraction();

});

function toggleE(e) { if ( $(e).css('display') == "none" ) { $(e).show(); }else{ $(e).hide(); } }

function blankWin()
{
  var hostname = window.location.hostname; hostname = hostname.replace("www.","").toLowerCase();
  $('a[href*="/"]').each(function(){ 
    if ($(this).attr('id') != 'zakelijk') {
    href = $(this).attr('href'); if (href.indexOf("http://")!=-1 && href.indexOf(hostname)==-1) $(this).attr('target','_blank');
    }
  });
};

function slider(){
	$('#slider').nivoSlider({
		effect:'random', //Specify sets like: 'fold,fade,sliceDown'
		slices:15,
		animSpeed:500,
		pauseTime:3000,
		startSlide:0, //Set starting Slide (0 index)
		directionNav:false, //Next & Prev
		directionNavHide:true, //Only show on hover
		controlNav:false, //1,2,3...
		controlNavThumbs:false, //Use thumbnails for Control Nav
		controlNavThumbsFromRel:false, //Use image rel for thumbs
		controlNavThumbsSearch: '.jpg', //Replace this with...
		controlNavThumbsReplace: '_thumb.jpg', //...this in thumb Image src
		keyboardNav:true, //Use left & right arrows
		pauseOnHover:true, //Stop animation while hovering
		manualAdvance:false, //Force manual transitions
		captionOpacity:0.8, //Universal caption opacity
		beforeChange: function(){},
		afterChange: function(){},
		slideshowEnd: function(){} //Triggers after all slides have been shown
	});
}

function jsAlert(title,content)
{
	var boxHeight = 300;
	var box = '';
	    box += '<strong>'+title+'</strong><br/><br/>'
	    box += '<p>'+content+'</p>'
			  $.fn.prettyPhoto({social_tools:false,theme:'facebook'});
			  $.prettyPhoto.open('http://www.bakerstreet.nl/particulier/images/logo.png', '','<p class="center">'+title+'</p><br/><br/><p class="black font11 center">'+content+'</p><br/>');
}

function bindRichInteraction(){

		/* Clear & Restore input */
	$('input[type=text]').bind({
		focus: function(){ if ($(this).val() == $(this)[0].defaultValue) $(this).val(''); },
		blur:  function(){ if ($(this).val() == "") $(this).val($(this)[0].defaultValue); }
	});


	/* Producten aanklikbaar */
	$("#producten li").bind({
		click: 			function(){ window.location = $(this).find("a").attr("href"); },
		mouseenter: function(){ $(this).addClass("active") },
		mouseleave: function(){ $(this).removeClass("active") }
	});
	$("#producten li").addClass("clickable");	
	$("span.close").addClass("clickable");
	
	/* In uitschakelen formulier elementen */
	$('.toggleFormParts').each(function(){ toggleFormPart($(this)) } );
	
	$('.toggleFormParts').bind({ click: function(){ toggleFormPart($(this)) } });
	
	function toggleFormPart(e){
		var targetE = '#part'+ e.attr('rel') +'Adres';
		var targetSpry = e.attr('rel');
		var eVisible = e.is(':checked');
		if (eVisible){
			$(targetE).removeClass("jsHide");
			// Spryregels toevoegen op basis van variabele targetSpry
		}else{
			$(targetE).addClass("jsHide");
			// Spryregels verwijderen op basis van variabele targetSpry
		}
	}
}

// Kassa
function getBestelling(guid,klantId)
{
	debugLog('getBestelling('+guid+','+klantId+')')
	$.ajax({ 
		cache: false,
		type: "GET", 
		url: '/particulier/ajax/getBestelling.asp', 
		data: 'guid='+ guid +'&klantId='+ klantId,
		success: function(response){ 
			$('#ajaxBestelling').html(response);
			bindBestellingAddEdit();
		}
	});
}
function addProductToCart(guid,stamId,stamTitel,stamExtraBedrag)
{
	debugLog('addToCart('+guid+','+stamId+','+stamTitel+')');
	$.ajax({ 
		cache: false,
		type: "POST", 
		url: '/particulier/exec/addtocart.asp', 
		data: 'guid='+ guid +'&stam_id='+ stamId+'&stam_titel='+ stamTitel+'&aantal=1&extra_bedrag='+stamExtraBedrag,
		beforeSend: function() { preLoaderShow( $('#ajaxBestelling') ); },
		success: function(response){
			getBestelling(guid,'');
			preloaderHide();
		}
	});
}
function bindBestellingAddEdit(regelId,guid,amount)
{
	$('.minder').bind({
		click: function() { changeAmount($(this).parent().parent().parent(),guid,-1); return false; }
	});
	$('.meer').bind({
		click: function() { changeAmount($(this).parent().parent().parent(),guid,1); return false; }
	});

	$('a.bestelling').bind({
		click: function() {
			if ( $('#vestigingCheck').val() == '' )  {
			jsAlert("Bestellen","Je moet een vestiging kiezen om de bestelling te kunnen plaatsen");
			return false;
			}
		}
	});
}
function changeAmount(e,guid,amount)
{
	var currentAmount = e.find('.amount').html();
	var regelId = e.attr('rel');
	var newAmount = parseInt(currentAmount) + parseInt(amount);
	if (newAmount < 0) newAmount = 0;

	$.ajax({ 
		cache: false,
		type: "POST", 
		url: '/particulier/exec/refreshcart.asp', 
		data: 'guid='+ guid +'&regel_id='+ regelId+'&regel_aantal='+newAmount+'&ajax=true',
		beforeSend: function() { preLoaderShow( $('#ajaxBestelling') ); },
		success: function(response){
			getBestelling(guid,'');
			preloaderHide();
		}
	});
}

function preLoaderShow(e)
{
	if (e==undefined) e = $('#container');
	e.append(preLoader);
	$('#preloadOverlay').css({ 'opacity' : 0.7 });
	$('#preloadContainer').show();
}
function preloaderHide()
{
	$('#preloadContainer').remove();
}

function doCheckIntOnly(Etje, Obj) {
  var keyCode = Etje.keyCode ? Etje.keyCode : Etje.which ? Etje.which : Etje.charCode;
  if (keyCode >= 48 && keyCode <= 57 && Etje.ctrlKey == false && Obj.value.length < 3) {
    return true;
  } else {
    return false;
  }
}

function debugLog(str)
{
	if (!ie){ console.log(str)	}

	window.log = function(){
	  log.history = log.history || [];  
	  log.history.push(arguments);
	  arguments.callee = arguments.callee.caller;  
	  if(this.console) console.log( Array.prototype.slice.call(arguments) );
	};

}

