jQuery(window).load(function() {
	/* categories */
	 jQuery('div.box ul.categories').children().each(function() {
	 	if (!jQuery(this).hasClass('active')) {
		 	jQuery(this).bind('mouseover', function() {
			   	this.className = 'hover'; 	
			 });
			 jQuery(this).bind('mouseout', function() {
			   	this.className = ''; 	
			 });
		};
		 jQuery(this).bind('click', function() {
		 	try {
		 		location.href = jQuery(this.firstChild).attr('href');
			} catch (error) {
			};	
		 });
	 });
	 /* site menu */
	 jQuery('div#site_menu_content ul').children().each(function() {
	 	jQuery(this).bind('mouseover', function() {
		   	this.className = 'hover'; 	
		 });
		 jQuery(this).bind('mouseout', function() {
		   	this.className = ''; 	
		 });
		 jQuery(this).bind('click', function() {
		   	try {
		   		location.href = jQuery(this.firstChild).attr('href');
			} catch (error) {};	
		 });
	 });
	 
	 jQuery('input.search_input').bind('click', function() {
	 	this.value = "";
	 });
	
});

var Tab = {

	init 	: function()
	{
		// tabjes at the product detail page
	 	if(location.hash != "") {
			var contentTabId = location.hash.replace('#','');
			// display the content
			jQuery('div.content_tab').hide();
		   	jQuery('div#js_' + contentTabId).show();
		   	// activate the correct tab
		   	jQuery('ol.js_labels').children().removeClass('current');
		   	jQuery('li#' + contentTabId).addClass('current');
		} else {
		 	jQuery('.content_tab').each(function(it) {
			   	if (it > 0) {
					jQuery(this).hide();   
				} ;
		 	});
		};
	 	jQuery('ol.js_labels').children().each(function() {
			jQuery(this).bind('click', function() {
				jQuery('ol.js_labels').children().removeClass('current');
			   	jQuery(this).addClass('current');
			   	var contentTabId = jQuery('#'+this.id + ' a').attr('href').replace('#','');
			   	jQuery('div.content_tab').hide();
			   	jQuery('div#js_' + contentTabId).show();
			});
		});
	}
}

var Scroller = {

	currentNumber 		: 3,
	totalAmount			: 0,
	interval			: 2000,
	
	init	: function(totalAmount) 
	{
		this.totalAmount = totalAmount;
		//by default, the scroll is only done vertically ('y'), change it to both.
		$.scrollTo.defaults.axis = 'x'; 			
		//this one is important, many browsers don't reset scroll on refreshes
		$('div.offers').scrollTo( 0 );//reset all scrollable panes to (0,0)
		$.scrollTo( 0 );//reset the screen to (0,0)
		if (this.currentNumber < this.totalAmount) {
			setTimeout(function() {
				Scroller.start();
			}, Scroller.interval);
		};
		
	},	
	
	start	: function()
	{		
		if (Scroller.currentNumber < Scroller.totalAmount) {
			$('div.offers').stop().scrollTo('+=238', Scroller.interval);
   			Scroller.currentNumber+=1;
 			setTimeout(function() {
				Scroller.start();
			}, Scroller.interval*3);
   		} else {
			// scroll totally back
			$('div.offers').scrollTo(0, Scroller.interval);
			Scroller.currentNumber = 3;
			setTimeout(function() {
				Scroller.start();
			}, Scroller.interval*4);
			return;
		};	
		return false;
	}
};

var Webshop = {

	totalSpanId	: 'span#amount_total',
	subTotalSpanId : 'span#amount_subtotal',
	sendCostsSpanId : 'span#sendcosts',
	
	update	: function(obj)
	{
		if (parseInt(obj.value) > 0) {
			var itemId = obj.name.replace(/^aantal\[(\d+)\]$/,'$1');
			var pObject = this;
			$.post('/ajax_server.php?opt=webshop&value=' + obj.value + '&id=' + itemId,
			  	function(data){
			  		if (data) {
			  			var objectData = eval('(' + data + ')');
			  			jQuery(pObject.sendCostsSpanId).html(objectData.sendcosts);
			  			// update the total amount
			  			var amountIncSendcosts = parseFloat(objectData.totalAmount)+parseFloat(objectData.sendcosts); 
			  			jQuery(pObject.totalSpanId).html(amountIncSendcosts.toFixed(2));
			  			try{
						   jQuery('span#lft_t_total_overview').html(amountIncSendcosts.toFixed(2));
						} catch (error) {
							
						}
			  			// update the subtotal amount
			  			jQuery(pObject.subTotalSpanId).html(objectData.subTotal.toFixed(2));
			  			jQuery('span#product_total_amount_' + itemId).html(objectData.productTotalAmount);
			  			
			  			try {
			  				var tempAmount = Math.floor(objectData.subTotal/7);
							jQuery('span.bonus_points_span').text(tempAmount);  
						} catch (error) {
						
						} /*
						try {
							jQuery('span#bonus_points_amount').text(bonusCodeDiscountAmount.toFixed(2));
						} catch (error) {
						
						}  */
			  			return false;
			  		};
			  	}
			);
		};
	},
	
	displayAddress	: function(selectObj, targetId, type)
	{                              
		if (jQuery(selectObj).val() != 0) {
			jQuery.post('/ajax_server.php?action=getAddressData', 
			{ 
		  		id			: selectObj.options[selectObj.options.selectedIndex].value,
		  		type		: type
			},
		  	function(data){
		  		if (data) {
		  		 	jQuery(targetId).html(data);
		  		}
		  	});
		};
	},
	
	setAction		: function(inputField, t)
	{
		if (inputField.checked) {
			var selectedValue = 0;
			for (i=0; i < inputField.form.pay_option.length; i++) {
				if (inputField.form.pay_option[i].checked)
					selectedValue = inputField.form.pay_option[i].value;
			};
			var formAction = "";
			if (selectedValue == 2) {
			  	formAction = "https://ideal.rabobank.nl/ideal/mpiPayInitRabo.do";
			};
			inputField.form.action = formAction;  
		};
		return false;	
	},
	
	doAction	: function(obj)
	{
		if (obj.algemene_voorwaarden.checked) {
			var selectedValue = 0;
			for (i=0; i < obj.pay_option.length; i++) {
				if (obj.pay_option[i].checked)
					selectedValue = obj.pay_option[i].value;
			};
			var formAction = "";
			if (selectedValue == 2) {
				var t = document.getElementById('hide_form');
					t.submit();
				setTimeout(function() {
					obj.submit();
				}, 500);
			}	
		}
	}
};

function animateScrollTo(obj)
{
	jQuery(obj.href).scrollTo(obj,{ top:jQuery(obj).offset().top}, 800 );
};


var Form = {

	initActionCode	: function(formId)
	{
		jQuery(formId).bind('submit', function() {
			jQuery('div#action_code_form_error').show();
			jQuery('div#action_code_form_error').html('<img src="/img/ajax-loader.gif" />');
			if (this.action_code.value == "") {
				jQuery('div#action_code_form_error').text('Verplicht veld');
				return false;	
			} else {
			    jQuery('div#action_code_form_error').hide();
			    jQuery.post('/ajax_server.php?opt=action_code&value=' + this.action_code.value,
			  	function(data){
			  		if (data) {
			  		   	var objectData = eval('(' + data + ')');
			  		   	jQuery('div#action_code_form_error').text(objectData.warning);
			  		   	jQuery('div#action_code_form_error').show();
			  		   	if (objectData.status_code == 400) {
							var currentPoints = parseInt(jQuery('span#bonus_points_text').text());
							var newPoints = currentPoints+parseInt(objectData.points);
							jQuery('span#bonus_points_text').text(newPoints);	 
						}
			  			return false;
			  		}
			  	});
			  	return false;
			}
		});
	}
}
