$(function() {	
	
	jQuery.httpbase = function( ){
		return $('base')[0].href;
	}
	
	$('#filtering .options').each( function() {
		$(this).hide();
	});
	
	$('#filtering .filter').each( function() {		
		
		defaultheight = 65;
		
		$(this).click(function() {		
			filter = this;
			filterheight = $('.options',filter).height();
			
			if($(filter).is('.active')) {
				$(filter).removeClass('active');
				$('.option span', filter).removeClass("close").addClass("open");							
				$('.options',filter).hide();	
				$('#filtering').animate({ height: defaultheight+'px'},
										{ duration: 'fast' }
				);	
			} else { 																
				$('#filtering .filter').each( function() {
					if($(this).is('.active')) {
						$('.options',this).hide();
						$(this).removeClass('active');
						$('.option span', this).removeClass("close").addClass("open");
					}
				});				
				
				$('.options',filter).show();
				$('#filtering').animate({ height: (filterheight + defaultheight + 10) +'px'},
										{ duration: 'normal' }
				);			
				$('.option span', filter).removeClass("open").addClass("close");	
				$(filter).addClass('active');
			}
		});
	});	
	
	$('.maten .maat').click( function() {
		$('.maten .maat').removeClass('active');
		$(this).addClass('active');
		$('input',this).attr('checked','checked');
		
		$.ajax({
			url: 'shop/ajaxprijs',
			data: 'artikel_id='+$("input[name='artikel_id']").val()+'&maat_id='+$("input[name='maat']:checked").val(),
			dataType: 'html',
			success: function(r) {
				$('.prijs').html(r);
			}
		});
		return false;
	});
	
	$('.bestellen .info').each( function(i,info) {
		$("input",info).focus(function() {
			if($(info).is(":has(span)")) {				
				$.showTooltip(info,$("span",info).html());	
			}
		});	
	});
	
	$("form[name='gegevens']").each(function() {		
		$.copyAfleveradres( this );
		
		$(this).bind("focus blur click keypress change", function(){
			$.copyAfleveradres( this );
		});		
	});

	$("form[name='bestelForm']").submit( function() {			
		if($(this).is(":has(input[type='radio'][name='maat'])")) {
			if(!$("input[type='radio'][name='maat']:checked").val()) {
				$('.warning',this).show();
				return false;
			}
		}
	});
	
	$(".betalen .betaalwijze").each(function() {
		$(this).click(function() {
			if(!$(this).is('.active')) {				
				$(".betalen .betaalwijze .betaalmethodes").hide('fast');
				$(".betalen .betaalwijze").removeClass('active');
				$(".betalen .betaalwijze .betaalmethodes input[name='betaalmethode']").removeAttr("checked");

				
				$(".betaalmethodes",this).show("normal");
				$(this).addClass("active");
				$(".betaalmethodes .betaalmethode",this).click(function() {
					$("input[name='betaalmethode']",this).attr("checked","checked");
				});
			}				
		});
	});
	
	$('form.creditcard input[type="button"]').click(function(){			
		$.getJSON($.httpbase()+"bestel/creditcardvalidate",$('form.creditcard').serialize(),function( data ){
			if (data['succes']==true){
				$('form.creditcard').submit();
    		}else{    			    			
    			$('.warnings').remove();
    			$('.fieldset').prepend(data['html']);    			
    		}
    	});	
		return false;
	});
	
	$(".detail #thumbs .thumb").each( function(i) {			
		$(this).click(function() {						
			$(".gallery a:eq("+i+")").trigger('click');		
		});		
	});	
	
	$pirobox = $().piroBox({
	      my_speed: 300, //animation speed
	      bg_alpha: 0.5, //background opacity
	      radius: 0, //caption rounded corner
	      scrollImage : false, // true == image follows the page _|_ false == image remains in the same open position
	                           // in some cases of very large images or long description could be useful.
	      slideShow : 'false', // true == slideshow on, false == slideshow off
	      slideSpeed : 3, //slideshow duration in seconds(3 to 6 Recommended)
	      pirobox_next : 'piro_next', // Nav buttons -> piro_next == inside piroBox , piro_next_out == outside piroBox
	      pirobox_prev : 'piro_prev', // Nav buttons -> piro_prev == inside piroBox , piro_prev_out == outside piroBox
	      close_all : '.piro_close,.piro_overlay' // add class .piro_overlay(with comma)if you want overlay click close piroBox
	});	
	
});

jQuery.copyAfleveradres = function( form ){
	if( !form["afleveradres"].checked ){
		$('#afleveradres').hide();
	} else if( form["afleveradres"].checked && $('#afleveradres').css('display') == 'none' ) {
		$('#afleveradres').show();	
	} 
}

jQuery.showTooltip = function(element,data) {	
	
	tooltip = '<div class="tooltip">';
	tooltip += '<div class="top"></div>';
	tooltip += '<div class="content">'+data+'</div>';   
    tooltip += '<div class="bottom"></div>';
    tooltip += '</div>';   
		
	$(element).prepend(tooltip);			
	$('.tooltip', element).css('left',$('input',element).width());
	$(element).css('z-index','5');			
	
	$('input',element).blur(function() {
		$('.tooltip', element).remove();
		$(element).css('z-index','1');
	});	
}