$(function() {	
	
	jQuery.httpbase = function( ){
		return $('base')[0].href;
	}
	jQuery.loadcm = function( uri, htmlElementId ){
		$.ajax({ url: uri, success: function( data ){
	        $('#' + htmlElementId).html( data );
	    }});
	}
	
	$(".clickable").click( function() {					
		document.location.href=$('a:eq(0)',this).attr('href');
		return false;
	});
	
	$('.form form').submit( function() {
		if(this['zoeken'].value.length <= 2) {
			return false;
		}
	});
		
	//SUPPORT
	var values = new Array();
	var inputs = $(":input");
	
	inputs.each(function(i,e) {
		values[i] = e.value;
		if($(e).hasClass("clear")) {
			$(e).focus(function() {			
				if(e.value == values[i]) {				
					e.value = '';
				}
			});
			$(e).blur(function() {
				if(e.value == '') {
					e.value = values[i];
				}
			});
		} else if($(e).is(".password.text")) {
			$(e).focus(function() {		
				$(this).addClass('hide');
				$(this).parent("form").find(".password.input").removeClass('hide');
				$(".password.input",$(this).parent("form")).focus();					
			});			
		} else if($(e).is(".password.input")) {
			$(e).blur(function() {
				if(e.value == '') {
					$(this).addClass('hide');
					$(this).parent("form").find(".password.text").removeClass('hide');
				}
			});
		}
	});
	
	$("#categorieen ul.items li").each(function(i,cat) {
		$(this).hover(function(){	
			$("#subnav .subnav").hide();
			$("#subnav").show();
			$("#subnav .subnav").eq(i).show();
			$("#subnav .transparency").css("height",$("#subnav").height());
		}, function() {
			$("#subnav").hide();			
		});
	});
	
	$("#subnav").hover(
		function() {
			$("#subnav").show();
		}, function() {
			$("#subnav").hide();	
		}
	);
	
	$("#detail .informatie .tab").click( function() {
		$("#detail .informatie .tab").removeClass("is_active");
		$(this).addClass("is_active");
		$("#detail .informatie .content").addClass("hide");
		$("#detail .informatie ."+$(this).attr('title')).removeClass("hide");		
	});
		
	$(".select").each( function(i,filter){
		if(!$(this).hasClass('disabled')) {					
			$(this).click(function(e) {					
				$(".select .options").hide();				
				$(document).unbind('click');
				if(!$(this).hasClass('is_active')) {
					$(".select").removeClass('is_active');
					$(this).addClass('is_active');										
					$('.options',this).animate({			
					    height: ['show', 'swing']
					}, 300,'linear');									
					e.stopPropagation();
				} else {
					$(this).removeClass('is_active');	
				}
			});			
		}
	});
	
	if($('.filters').length > 0) {
		$('.filters .scroll-pane').jScrollPane(
			{
				autoReinitialise: true
			}
		);
	}
	
	$("#footer .merken .meer").click(function(){
		$.ajax({
			url: "home/ajaxmerken/",
			success: function(data) {
				$("#footer .merken").replaceWith(data);
			}
		});
	});
	
	// Nieuwsbrief
	$('#nieuwsbrief .label').click(function() {
		if (!$('#nieuwsbrief').hasClass('is_open')) {
			$('#nieuwsbrief').animate({width: '236px'},500,function(){
				$('#nieuwsbrief').addClass('is_open');
			});
		} else {
			$('#nieuwsbrief').animate({width: '35px'},500).removeClass('is_open');				
		}
	});
	
	$('#nieuwsbrief form').submit(function() {		
        $.ajax({
        	type: 'POST',
        	url: $(this).attr("action"),
        	data:  $(this).serialize(),
        	dataType: 'json',
        	success: function(data) {
        		if(data['melding']) {
        			$('#nieuwsbrief p').html(data['melding']);
        			$('#nieuwsbrief p').addClass('error');
        		} else if(data['succes']) {
        			$('#nieuwsbrief p').html(data['succes']);        			
        			$('#nieuwsbrief p').addClass('error');
        			setTimeout(function(){
        				$('#nieuwsbrief').animate({width: '35px'},500).removeClass('is_open');	
        			},3000);
        		}
        	}
        });
		return false;
	});
	
	/* zoeksuggesties */
	$('#zoeken input').focus(function() {
		suggestieUpdate();
		var suggestieInterval = setInterval('suggestieUpdate()', 1000);
		$('#zoeken_suggesties').slideDown('slow');
	}).blur(function() {
		$('#zoeken_suggesties').slideUp('slow');
		if (typeof suggestieInterval != 'undefined') {
			clearInterval(suggestieInterval);
		}
	});
});

/* suggestie-click */
function suggestieClick(artnr, titel) {
	document.location.href = baseUrl + 'shop/artikel/' + titel + '/' + artnr;
}

/* suggestie-interval */
function suggestieUpdate() {
	var zoekvak = $('#zoeken input');
	if (zoekvak.val().length>0 && zoekvak.val()!=zoekvak.attr('zoektekst')) {
		$.ajax({
			url: baseUrl + 'shop/zoekensuggestie/',
			dataType: 'json',
			data: { zoeken: zoekvak.val() },
			success: function(data) {
				$('#zoeken_suggesties table').html('');
				for (var i=0; i<data.length; i++) {
					var html =	'<tr onclick="suggestieClick(\'' + data[i].id + '\', \'' + data[i].urlpart + '\');"><td>' +
									'<div class="titel">' + data[i].titel + '</div>' +				
									'<div class="prijs">' + data[i].prijs + '</div>' +
									'<div class="afbeelding" style="background-image: url(' + data[i].afbeelding + ')"></div>' +
									'<div class="middle">' +
									'<b>Artikelnr</b>: ' + data[i].artnr + '<br />' +
									'<b>Dessin</b>: ' + data[i].dessin + '<br />' +
									'<b>Merk</b>: ' + data[i].merk +
									'</div>' +																		
								'</td></tr>';
					$('#zoeken_suggesties table').append(html);
				}
			}
		});
		zoekvak.attr('zoektekst', zoekvak.val());
	} else if(zoekvak.val().length==0) {
		$('#zoeken_suggesties table').html('');
	}
}
