// JavaScript Document

$(document).ready(function() {
   $.facebox.settings.opacity 	=	0.2;
   $('a[rel*=facebox]').facebox();
    var lng_opacity = 0.3;
	loadCorner('#intro');
	loadCorner('div.content');
	loadCorner('div.rechts');
	loadCorner('.product');
	$('.item').mouseover(function() {
		if($(this).children().next().attr('class') == 'submenu') {
			$(this).children().next().slideDown('fast');
		}
	}).mouseleave(function() {
		if($(this).children().next().attr('class') == 'submenu') {
			$(this).children().next().slideUp('fast');
		}
	});
	$('#impressie').cycle({ 
		fx		:	'fade', 
		speed	: 	500,
		duration:	500
	 });
	
	$('.winkelwagen').css({left:(($(window).width()-350)/2)+'px'});
	$(window).resize(function() {$('.winkelwagen').css({left:(($(window).width()-350)/2)+'px'});});
	$('.shopProduct:last').css({margin:'0px'});
	$('#languages .language').css({
		opacity	:	lng_opacity
	});
	$('#languages .language.selected').css({
		opacity	:	1
	});
	$('#languages .language').mouseover(function() {
		if($(this).attr('class') == 'language') {
			$(this).animate({
				opacity	:	1
			}, 500);
			$('#languages .language.selected').animate({
				opacity	:	lng_opacity
			}, 500);
		}
	}).mouseout(function() {
		if($(this).attr('class') == 'language') {
			$(this).animate({
				opacity	:	lng_opacity
			}, 500);
			$('#languages .language.selected').animate({
				opacity	:	1
			}, 500);
		}
	});
	$('.rechts .item:first').css({						 
		borderTop	: '1px #eeeeee solid'
	});
	$('#projecten .item input:checkbox').css({
		marginRight	: '10px'
	}).change(function() {
		var soort = $(this).attr('id').split('_');
		setProjectSoort(soort[1], $(this).attr('checked'));
	});
	$('#projecten .item').click(function(el) {
		if(!$('input').attr("disabled")) {
			$('input').attr("disabled", true); 
			if($(el.target).get(0).tagName == 'DIV') {
				if($(this).children().attr('checked')) {
					$(this).children().attr('checked', '');
				}
				else {
					$(this).children().attr('checked', 'checked');			
				}
			}
			var soort = $(this).children().attr('id').split('_');
			setProjectSoort(soort[1], $(this).children().attr('checked'));
			if($(this).next().attr('id') != 'undefined') {
				var subitems = $(this).next().attr('id').split('_');
				if(subitems.length == 2) {
					if($('#' + $(this).next().attr('id')).is(':visible')) {
						$(this).next().slideUp('slow');
						$(this).next().children().children().attr('checked', '');
						var soorten = '';
						$(this).next().children().each(function() {
							if($(this).children().get(0).tagName.toLowerCase() == 'input') {
								var soort = $(this).children().attr('id').split('_');
								soorten  += soort + ',';
							}
						});
						setProjectSoort(soorten, false);
					}
					else {
						$(this).next().slideDown('slow');
						$(this).next().children().children().attr('checked', 'checked');	
						var soorten = '';
						$(this).next().children().each(function() {
							if($(this).children().get(0).tagName.toLowerCase() == 'input') {
								var soort = $(this).children().attr('id').split('_');
								soorten  += soort + ',';
							}
						});		
						setProjectSoort(soorten, true);			
					}
				}
			}
		}
	});
	//	Projecten / producten slider
	$('#slider img').css({opacity:0.5});
	$('#slider img:first').animate({opacity:1},100);
	var fotos			=	Math.ceil(Math.round($('#slider').children().children().children().children().length)/2);
	var slider_position = 	0;
	$('.rechts #next').click(function() {
		slider_position++;
		$('#slider').animate({
			left: '-' + ((slider_position * 96) - 3) + 'px'
		}, 500);
		$('.rechts #prev').show();
		if(slider_position >= 2 || slider_position >= (fotos - 3)) {
			$('.rechts #next').hide();
		}
	});
	$('.rechts #prev').click(function() {
		slider_position--;
		if(slider_position == 0) {
			$('#slider').animate({
				left: '3px'
			}, 500);
			$('.rechts #prev').hide();
		}
		else {
			$('#slider').animate({
				left: '-' + ((slider_position * 96) - 3) + 'px'
			}, 500);
		}
		$('.rechts #next').show();
	});
	$('#slider img').mouseover(function() {
		$('#slider img').animate({opacity:0.5},100);
		$(this).animate({opacity:1},500);
		$('#foto').attr('src', $(this).attr('src').replace('mini', 'middle'));
		$('.popupf').hide();
		$('#popup_' + $(this).attr('id').replace('thumb_', '')).show();
	});
});

function setProjectSoort(nSoortID, sStatus) {
	$('#geen_projecten').show();
	var status	=	(sStatus ? 1 : 0);
	$.ajax({
		url		:	'request.php',
		type	:	'POST', 
		data	:	'&request=project_soort&project_soort=' + nSoortID + '&status=' + status,
		success	:	function(response) {
			$('#result').html(response);
			$('input').attr("disabled", false); 
			/*
			$('#projecten').children().hide();
			if(response == '') {
				if($('#projecten').length == 0) {
					$('#geen_projecten').hide();
					$('#projecten').children().show();
				}
				else {
					$('#geen_projecten').show();
				}
			}
			else {
				$('#geen_projecten').hide();
				var projecten = response.split('|');
				for(i = 0; i < projecten.length; i++) {
					$('#project_' + projecten[i]).show();
				}
			}
			window.setTimeout(function() {
				$('input').attr("disabled", false); 
		    }, 500);
			return false;
			$('div.content').remove();
			$('h1.content').after(response);
			*/
			loadCorner('div.content');
		}
	});
}

function loadCorner(el) {
	$(el).prepend('<div class="ll"></div>');
	$(el).prepend('<div class="clt"></div>');
	$(el).prepend('<div class="lt"></div>');
	$(el).prepend('<div class="crt"></div>');
	$(el).prepend('<div class="lr"></div>');
	$(el).prepend('<div class="crb"></div>');
	$(el).prepend('<div class="lb"></div>');
	$(el).prepend('<div class="clb"></div>');
    if($('#tabs').length > 0 && $('#tabs .tab:first').hasClass('active')) {
		$('#tabs').next().children('.clt').remove();
	}
	if($('#tabs').length > 0) {
		$('.rechts').css({top:'35px'});
    }
}
