$(function(){
	$.fn.center=function(){
		H=$(this).height();
		W=$(this).width();
		wT=$(document).scrollTop();		
		wL=$(document).scrollLeft();		
		wH=$(window).height();		
		wW=$(window).width();	
		if(wH <= H){
			$(this).css('top',wT+'px');
		}else{
			$(this).css('top',Math.max((wH-H)/2 + wT)+'px');
		}
		if(wW <= W ){
			$(this).css('left',wL+'px');
		}else{
			$(this).css('left',Math.max((wW-W)/2 + wL)+'px');
		}
		return this;
	}
	
	$.fn.hoverImage=function(i,j){$(this).hover(function(){this.src=i;},function(){this.src=j;});return this;}
	$('a.navigation-item').each(function(){
		$(this).hover(
			function(){
				src=$(this).children('img').attr('src');
				if(src)
					$(this).children('img').attr('src',src.substr(0,src.length-4)+'_active.gif');
			}, 
			function(){
				src=$(this).children('img').attr('src');
				if(src)
					$(this).children('img').attr('src',src.substr(0,src.length-11)+'.gif');
			}
		);
	});
	$('a.navigation-item-active').each(function(){
		$(this).hover(
			function(){
				src=$(this).children('img').attr('src');
				if(src)
					$(this).children('img').attr('src',src.substr(0,src.length-11)+'.gif');
			}, 
			function(){
				src=$(this).children('img').attr('src');
				if(src)
					$(this).children('img').attr('src',src.substr(0,src.length-4)+'_active.gif');
			}
		);
	});
	$('li.navigation-left-item').each(function(){
		$(this).hover(
			function(){
				$(this).css('background-image','url(style/images/bg_navigation_left_active.gif)');
			}, 
			function(){
				$(this).css('background-image','url(style/images/bg_navigation_left.gif)');
			}
		);		
	})

	$('#quick-search input-image').click(function(){
		document.forms.quick_find.submit();
	});
	$('#quick-login-button').hoverImage('style/images/button_login_active.jpg','style/images/button_login.jpg').click(function(){
		this.form.submit();
	});
	$('.ui-dialog').center();
	$('div.categorie').each(function(){
		$(this).hover(
			function(){$(this).children('div.category-description').slideDown(200); },
			function(){$(this).children('div.category-description').fadeOut(100);}
		);
	})

/*	
	$('#shoppingcart_box_img').hover(
		function(){
			this.src='style/images/bestelknop-active.gif';
		},
		function(){
			this.src='style/images/bestelknop-passive.gif';
		}
	);
*/

	//AF20101105 Added tabbed slider for productpage
	//BC20110113 onClick behaviour
	/* http://www.gayadesign.com/diy/animated-tabbed-content-with-jquery/ */
	var TabbedContent = {
		active: 1,
		init: function() {
			var me=this;
			$("span.sld_tab_item").click(function() {
				if (me.active !=  $(this).index()){
					me.active = $(this).index();
					$("div.sld_moving_bg").stop().animate({
						left: $(this).position()['left']
					}, {
						duration: 250
					});
					
					TabbedContent.slideContent((me.active-1));
			
				}
			});
		},
		slideContent: function(tab) {
			var numMargin = $("div.sld_slide_content").width() * (tab+1);
			$("div.sld_tabslider").children().hide().eq(tab).css({'margin-left':numMargin}).show();
			numMargin = numMargin * -1;
			numHeight =$("div.sld_tabslider").children().eq(tab).height();
			$("div.sld_tabslider").stop().animate({
				marginLeft: numMargin + "px",
				height: numHeight + "px"
			}, {
				duration: 250
			});
			
		}
	}
	TabbedContent.init();
	$('#hvrbtn_lft_afrekenen').hoverImage('images/buttons/dutch/btn_afrekenen_act_trans.png','images/buttons/dutch/btn_afrekenen_trans.png');
	$('#hvrbtn_afrekenen').hoverImage('images/buttons/dutch/btn_afrekenen_act_trans.png','images/buttons/dutch/btn_afrekenen_trans.png');
	$('#hvrbtn_lft_offerte').hoverImage('images/buttons/dutch/btn_offerte_act_trans.png','images/buttons/dutch/btn_offerte_trans.png');
	$('#hvrbtn_fact_opslaan').hoverImage('images/buttons/dutch/btn_opslaan_act_trans.png','images/buttons/dutch/btn_opslaan_trans.png');
	$('#hvrbtn_verz_opslaan').hoverImage('images/buttons/dutch/btn_opslaan_act_trans.png','images/buttons/dutch/btn_opslaan_trans.png');
	$('#hvrbtn_fact_annuleren').hoverImage('images/buttons/dutch/btn_annuleren_act_trans.png','images/buttons/dutch/btn_annuleren_trans.png');
	$('#hvrbtn_verz_annuleren').hoverImage('images/buttons/dutch/btn_annuleren_act_trans.png','images/buttons/dutch/btn_annuleren_trans.png');
	$('#couponValidate').hoverImage('style/images/shop/buttons/dutch/btn_voucherinleveren_act_trans.png','style/images/shop/buttons/dutch/btn_voucherinleveren_trans.png');
	$('#hvrbtn_offerteaanvragen').hoverImage('images/buttons/dutch/btn_offerteaanvragen_act_trans.png','images/buttons/dutch/btn_offerteaanvragen_trans.png');
	$('#hvrbtn_fact_adresboek').hoverImage('images/buttons/dutch/btn_adresboek_act_trans.png','images/buttons/dutch/btn_adresboek_trans.png');
	$('#hvrbtn_verz_adresboek').hoverImage('images/buttons/dutch/btn_adresboek_act_trans.png','images/buttons/dutch/btn_adresboek_trans.png');
	$('#hvrbtn_fact_wijzigen').hoverImage('images/buttons/dutch/btn_wijzigen_act_trans.png','images/buttons/dutch/btn_wijzigen_trans.png');
	$('#hvrbtn_verz_wijzigen').hoverImage('images/buttons/dutch/btn_wijzigen_act_trans.png','images/buttons/dutch/btn_wijzigen_trans.png');
	$('#hvrbtn_toevoegen').hoverImage('images/buttons/dutch/btn_toevoegen_act_trans.png','images/buttons/dutch/btn_toevoegen_trans.png');
	$('#hvrbtn_inschrijven').hoverImage('images/buttons/dutch/btn_inschrijven_act_trans.png','images/buttons/dutch/btn_inschrijven_trans.png');
	$('#hvrbtn_terug').hoverImage('style/images/shop/buttons/dutch/btn_terug_act_trans.png','style/images/shop/buttons/dutch/btn_terug_trans.png');
	$('#hvrbtn_verder').hoverImage('style/images/shop/buttons/dutch/btn_verder_act_trans.png','style/images/shop/buttons/dutch/btn_verder_trans.png');
	$('#hvrbtn_zoeken').hoverImage('style/images/shop/buttons/dutch/btn_zoeken_act_trans.png','style/images/shop/buttons/dutch/btn_zoeken_trans.png');
	$('#hvrbtn_bevestigen').hoverImage('style/images/shop/buttons/dutch/btn_bevestigen_act_trans.png','style/images/shop/buttons/dutch/btn_bevestigen_trans.png');
	$('#hvrbtn_registreren').hoverImage('style/images/shop/buttons/dutch/btn_registreren_act_trans.png','style/images/shop/buttons/dutch/btn_registreren_trans.png');
	$('#hvrbtn_verderwinkelen').hoverImage('style/images/shop/buttons/dutch/btn_verderwinkelen_act_trans.png','style/images/shop/buttons/dutch/btn_verderwinkelen_trans.png');
	$('#hvrbtn_klantgegevenswijzigen').hoverImage('style/images/shop/buttons/dutch/btn_klantgegevenswijzigen_act_trans.png','style/images/shop/buttons/dutch/btn_klantgegevenswijzigen_trans.png');
	$('#hvrbtn_wachtwoordwijzigen').hoverImage('style/images/shop/buttons/dutch/btn_wachtwoordwijzigen_act_trans.png','style/images/shop/buttons/dutch/btn_wachtwoordwijzigen_trans.png');
	$('#hvrbtn_adresboek').hoverImage('images/buttons/dutch/btn_adresboek_act_trans.png','images/buttons/dutch/btn_adresboek_trans.png');
	$('#hvrbtn_wijzigen').hoverImage('images/buttons/dutch/btn_wijzigen_act_trans.png','images/buttons/dutch/btn_wijzigen_trans.png');
	$('#hvrbtn_offerteverwijderen').hoverImage('images/buttons/dutch/btn_offerteverwijderen_act_trans.png','images/buttons/dutch/btn_offerteverwijderen_trans.png');
	$('#hvrbtn_offerteverwijderen').hoverImage('images/buttons/dutch/btn_offerteverwijderen_act_trans.png','images/buttons/dutch/btn_offerteverwijderen_trans.png');
	
	
	
	if( $('td.messageStackError').length >0){
		var table=$('td.messageStackError').parent().parent();
		$('td.messageStackError').each(function(){
			$("div#errorDialog ul.content").append('<li>'+$(this).html()+'</li>');
		});
		errorDialog.show();
		$(table).empty().css({'display':'none'});
	}
	/*if( $('div.osc_error_msg').length>0 ){
		$('div.osc_error_msg td').each(function(){
			$("div#errorDialog ul.content").append('<li>'+$(this).html()+'</li>');
		});
		errorDialog.show();
		$('div.osc_error_msg').empty();
	}
	*/
	$('a.product_image').lightBox({containerResizeSpeed:100});
	
	$('.attribute').each(function(){
		switch(this.type){
			case 'select-one':
				var value=parseFloat(attributes[this.id.substr(9)][this.value]);
				if(value!==0){
					$(this).attr('data',value);
				}
				$(this).change(function(){
					value=parseFloat(attributes[this.id.substr(9)][this.value]);
					if($(this).attr('data')!==undefined){
						basePrice=parseFloat(basePrice)-( parseFloat($(this).attr('data')) *  document.cart_quantity.cart_quantity.value);
					}
					$(this).attr('data',value);
					basePrice=parseFloat(basePrice)+( parseFloat(value) * document.cart_quantity.cart_quantity.value);
					$("#price").html("&euro; "+CurrencyFormatted(basePrice));
				});
				break;
			case 'checkbox':
				$(this).click(function(a){
					if($(this).attr('checked')==false){
						basePrice=basePrice-( parseFloat(attributes[this.id.substr(9)][this.value]) * document.cart_quantity.cart_quantity.value);
					}else{
						basePrice=basePrice+( parseFloat(attributes[this.id.substr(9)][this.value]) * document.cart_quantity.cart_quantity.value);
					}
					$("#price").html("&euro; "+CurrencyFormatted(basePrice));
				});
				break;
			case 'radio':
				var value=parseFloat(attributes[this.id.substr(9)][this.value]);
				inputGroups.add(this);
				if(this.checked==true){
					inputGroups.set(this.name,'price',value);	
				}
				$(this).click(function(){
					$('.license_reset').trigger('click');
					this.checked=true;
					var value=parseFloat(attributes[this.id.substr(9)][this.value]);
					previousPrice=inputGroups.get(this.name,'price');
					if(previousPrice!==false){
						basePrice=parseFloat(basePrice) - parseFloat(previousPrice * document.cart_quantity.cart_quantity.value) + parseFloat(value * document.cart_quantity.cart_quantity.value);
					}else{
						basePrice=parseFloat(basePrice) + parseFloat(value * document.cart_quantity.cart_quantity.value );
					}
					inputGroups.set(this.name,'price',value);
					$("#price").html("&euro; "+CurrencyFormatted(basePrice));
				});
				break;
		}
	});
	$('input.product_quantity').each(function(){
		basePrice=basePrice*$(this).val();	
		$("#price").html("&euro; "+CurrencyFormatted(basePrice));
	}).change(function(){
		basePrice=basePrice/product_quantity;
		basePrice=basePrice*$(this).val();
		product_quantity=$(this).val();
		$("#price").html("&euro; "+CurrencyFormatted(basePrice));		
	});
	$("form#productInfoAdd :input").each(function(){
		if(this.type=='select-one'){
			//TODO 
			//if($(this).text()=="Maak hier uw keuze.."){
			//	console.log($(this).text());
			//}
		}
	});

	// AF20110921: add field info to forms based on url
	var params=getUrlVars();
	$('form.dynamicForm :input').each(function(){
		name='field'+$(this).attr('name');
		if(array_key_exists(name,params)){
			this.value=urldecode(params[name]);
		}
	})
	
})
