// Basis functie om dom elementen mee op te halen


function clearText(e){
	if(e) {
		if(e.value == 'Trefwoord') {
			e.value = '';
		}
	}	
}

function fillEmpty(e){
	if(e.value == ''){
		e.value = 'Trefwoord';
	}
}

function bindElements(){
	$('.deleteProduct').click(function(){
		var data = this.id.split('_');
		var variation = data.pop();
		var id = data.pop();
		updateBasket({id:id, variation:variation, amount:0, type:'amount'});
	});

	$('.updateAmount').focus(function(){
		$(this).select();
	});

	$('.updateAmount').keydown(function(e){
		$(this).attr('oldVal', ($(this).val()));
	});
	
	$('.updateAmount').keyup(function(e){
		var val = parseInt($(this).val());
		if(isNaN(val)){
			$(this).val($(this).attr('oldVal'));
		} else if(val+'' != $(this).val()){
			$(this).val(val);
		} else if(val === 0){
			var data = this.id.split('_');
			var variation = data.pop();
			var id = data.pop();
			updateBasket({id:id, variation:variation, amount:this.value, timeout:500, type:'amount'});
		}

	/*
		if(e.which != 9){
			var data = this.id.split('_');
			var variation = data.pop();
			var id = data.pop();
			updateBasket({id:id, variation:variation, amount:this.value, timeout:500, type:'amount'});
		}
	*/
	});
	/*
	$('.updateAmount').blur(function(e){
		var data = this.id.split('_');
		var variation = data.pop();
		var id = data.pop();
		resetBasket({id:'input_'+id+'_'+variation,value:'amount'});
	});

	$('.updateRemark').blur(function(e){
		var data = this.id.split('_');
		var variation = data.pop();
		var id = data.pop();
		updateBasket({id:id, variation:variation, remark:this.value, timeout:500, type:'remark'});
	});
	*/
	$('#finishOffer a.button').click(function(){
		var url = $(this).attr('href');
		var recs = [];
		$('.updateAmount').each(function(){
			var data = this.id.split('_');
			var variation = data.pop();
			var id = data.pop();
			recs[recs.length] = {id:id,variation:variation,val:$(this).val(),remark:$('#remark_'+id+'_'+variation).val()};
		});
		ajax.request({
			url:'customer/plugins/webshop/render/update.php',
			method:'GET',
			params:{
				task:'checkBasket',
				data:ajax.encode(recs)
			},
			success:function(response){
				var result = response.responseText;
				if(result != ''){
					result = eval('('+result+')');
					switch(result.success){
						case true:
							document.location = url;
						break;
						case false:
							alert(result.msg);
						break;
					}
				} else {
					alert('Geen reactie van update.php bij checkBasket');
				}
			},
			failure:function(response){
				alert(response.responseText);
			}
		});
		return false;
	});
}
// Onready functie
$(function(){
	$('.divbutton').mouseover(function() {
		$(this).toggleClass('divbuttonHover');
	});
	$('.divbutton').mouseout(function() {
		$(this).toggleClass('divbuttonHover');
	});
	$('input.button').mouseover(function() {
		$(this).toggleClass('buttonHover');
	});
	$('input.button').mouseout(function() {
		$(this).toggleClass('buttonHover');
	});

	var arrowUp = $('#arrowUp img');
	var arrowDown = $('#arrowDown img');
	var scroller = $('#projectImageScroller');
	var spi = $('.smallProjectImage');

	arrowUp.css({cursor:'pointer'});
	arrowDown.css({cursor:'pointer'});
	var moving = {};
	arrowDown.click(function(){
		if(moving.pscroller !== true){
			var t = (parseInt(scroller.css('top'))+$('.smallProjectImage').height());
			var time = 800;
			if(t > 0){
				var n = parseInt(scroller.css('top'))*-1;
				time = parseInt(time/(spi.height()/n));
				t = 0;
			}
			moving.pscroller = true;
			scroller.animate({
				top:t
			}, time, 'swing', function(){
				moving.pscroller = false;	
			});
		}
	});
	arrowUp.click(function(){
		if(moving.pscroller !== true){
			var t = (parseInt(scroller.css('top'))-spi.height());
			var time = 800;
			if(t < ($('#projectImageWrap').height()-scroller.height())){
				var n = $('#projectImageWrap').height()-scroller.height()-t;
				n = spi.height()-n;
				time = parseInt(time/(spi.height()/n));
				t = ($('#projectImageWrap').height()-scroller.height());
			}
			moving.pscroller = true;
			scroller.animate({
				top:t
			}, time, 'swing', function(){
				moving.pscroller = false;
			});
		}
	});

	$('.smallProjectImage img').css({
		cursor:'pointer'
	});

	$('.smallProjectImage img').click(function(){
		var src = $(this).attr('large');
		var image = new Image();
		image.src = src;
		$('#largeProjectImage img').attr('src', image.src);
	});
	
	$('#containerProjecten').css({display:'block'});
	$('#loadingProjects').css({display:'none'});
	bindElements();

	$('#fbStart').click(function() {
		var linkId = $(this).attr('linkid');
		fb.start(document.getElementById(linkId));
		return false;
	});

	$('input[name="postcheck"]').css({
		display:'none'
	});
});
