// JavaScript Document
$(document).ready(function(){
	
	// Banner
	if($('#slider ul').length && $('#slider ul li').length > 1){
		$("#slider").easySlider({
			auto: true,
			continuous: true,
			numeric: true,
			pause: 7000
		});
	}
	
	// Form blur en focus regelen
	if($('#zoekform').length){
		$('#zoekform').bind('keyup',function(e){
			if(e.keyCode == 13){
				zoeken();
			}
		});
		$('#zoekform input:not(input[type="button"]), #zoekform textarea').each(function(){
			$(this).attr('defaultvalue',$(this).val());
			$(this).blur(function(){ 
				if(!$(this).val())
					$(this).val($(this).attr('defaultvalue'));
			})
			$(this).focus(function(){ 
				if($(this).val() == $(this).attr('defaultvalue'))
					$(this).val('');
			})
		});
	}
	
	// Email links vervangen
	$('a[href*="mailto:"]').each(function(){
		var email = $(this).attr('href').replace("[at]","@").replace(/\[dot\]/g,".");
		$(this).attr('href',email).html(email.replace("mailto:",""));
	});
	
	// Message van form weg laten faden
	if($('form').find('p.message').length){
		setTimeout(function(){
			$('form').find('p.message').slideUp('slow');
		},2000);
	}
	
	// Zoeken
	if(page == 'zoeken'){
		zoeken(1);
	}
	
	$('select').customStyle();
	
	// Assortiment detail pagina laatste li-tje (de 'last' class meegeven)
	$('div.assortimentdetailbox ul li:last').addClass('last');
	
	// Fotoalbum
	if($('.thumbs img').length){
		$('div.big a').bind('click', handlefotoalbum);
		handlefotoalbum(false,0);
	}
});

var currentindex = 0;
function handlefotoalbum(event, index){
	
	if(!event){
		currentindex = index;
	} else {	
		if(index){
			currentindex = index;
		} else{
			if($(event.target).hasClass('left'))
				currentindex--;
			else 
				currentindex++;
			
			if(currentindex >= $('div.thumbs img').length)
				currentindex = 0;
			
			if(currentindex < 0)
				currentindex = $('div.thumbs img').length-1;
		}
	}
	
	loadimage($('div.thumbs img:eq('+currentindex+')').attr('original'));
}
function loadimage(src){
	$('div.big div.img').load(base + 'ajax/genimage.php?src=' + src, function(){
		// Left en right hoogte meegeven
		$('div.big a').css({'height':$('div.big img').height(),'backgroundPosition':'50%'})
	});
}

function zoeken(ondocumentready){
	var query = "";
	if(!ondocumentready){
		query += "branche=" + $('#zoekform').find('#branche').val();
		query += "&productgroep=" + $('#zoekform').find('#productgroep').val();
		query += "&merk=" + $('#zoekform').find('#merk').val();
		query += "&product=" + $('#zoekform').find('#product').val();
		query += "&zoekterm=" + $('#zoekform').find('#zoekterm').val();
		query = encodeURI(query);
	}
	
	if(page !='zoeken'){
		$('#zoekform').submit();
		return;
	} 
	
	var prehtml = '	<p>&nbsp;</p>\
            		<div class="toiletBox">\
            			<h3>zoekresultaat</h3>\
						<ul>\
                    		<li class="active"><span>Bezig met zoeken.</span></li>\
                    	</ul>\
            		</div>\
					<div class="readBox sub">\
					<img src="'+base+'backoffice/images/loader.gif" alt="" class="loader"/>\
					</div>';
	
	$('#rangeCntr').html(prehtml);
	
	$.ajax({
		url : base + 'ajax/zoeken.php?r=' + Math.random(),
		type: 'get',
		data: query,
		success: function(data){
			$('#rangeCntr').html(data);
			$('div.assortimentdetailbox ul li:last').addClass('last');
		}
	});
}
