$(document).ready(function() {
	
	$('a[rel=external]').attr('target','_blank');
	
	if($('html').attr('lang') != 'nl'){
		if($('#menu li:nth(1) a').text() != 'Vérandas'){
			$('#menu li:nth(0) a').css({
				'line-height'	:	'normal',
				'height'		:	'40px',
				'padding'		:	'7px 0px 0px 0px'
			});
		}
	}
	
	$('#menu li ul li').each(function() {
		if($(this).text() == $('h2.active').text()){
			$(this).addClass('active');
		}
		
		if($(this).attr('class') == 'active'){
			$(this).parent().parent().addClass('active');
		}
	});
	
	if($('#tc_button').length > 0){
		$('.slideshow_holder .images').hover(function(){
			$(this).parent().find('a.bt').addClass('hover');
		},function(){
			$(this).parent().find('a.bt').removeClass('hover');
		});
	}
	
	/* TOOLTIP */
	if($('.press').length > 0){
		$('.press').tooltip({delay: 0, track: true, showURL: false, fade:100});
	}
	if($('.presslogo').length > 0){
		$('.presslogo').tooltip({delay: 0, track: true, showURL: false, fade:100});
	}
	
	/* LAZY LOAD */
	if($('img.lazy').length > 0){
		$("img.lazy").lazyload({
			effect : "fadeIn"
		});
	}
	
	$('.cycle').cycle({
		fx: 		'fade',
		speed: 		1000,
		timeout: 	5000,
		next:		'#next',
		prev:		'#prev'
	});
	
	$('.images').cycle({
		fx: 'fade', // choose your transition type, ex: fade, scrollUp, shuffle, etc...
		speed: 1000,
		timeout: 4000
	});
	
	$('#next, #prev').click(function() {
		$('.cycle').cycle('pause');
	});
	
	if($('#menu > li:eq(7)').find('ul').length > 0){
		$('#menu > li:eq(7)').find('ul').css({'left':'-400px'});
	}
	
	$('#menu li').hover(function() {
		$(this).find('ul').show();
		$(this).addClass('hovering');
	}, function() {
		$(this).find('ul').hide();
		$(this).removeClass('hovering');
	});
	
	if($('.result').length > 0){
		$('.result').hover(function(){
			$(this).find('img').stop().animate({'opacity':'0.3'}, 500);
			$(this).append('<div class="z-overlay">&nbsp;</div>');
		}, function() {
			$(this).find('img').stop().animate({'opacity':'1'}, 500);
			$(this).find('.z-overlay').remove();
		});
	}
	
	if($(".result-thumb").length > 0){
		$(".result-thumb").fancybox({
			prevEffect	: 'none',
			nextEffect	: 'none',
			helpers	: {
				title	: {
					type: 'float'
				},
				overlay	: {
					opacity : 0.8,
					css : {
						'background-color' : '#fefefe'
					}
				}/*,
				thumbs	: {
					width	: 50,
					height	: 50
				}*/
			},
			  afterShow    : showCustomCursor,
			  beforeClose  : hideCustomCursor
		});
	}
	
	

	$(".video").click(function() {
		$.fancybox({
			'autoScale'		: false,
			'title'			: this.title,
			'href'			: this.href.replace(new RegExp("watch\\?v=", "i"), 'v/'),
			'type'			: 'swf',
			'swf'			: {
			'wmode'				: 'transparent',
			'allowfullscreen'	: 'true',
			helpers	: {
				overlay		: {
							opacity : 0.8,
							css : {
								'background-color' : '#fefefe'
							}
					}
				}
			}
		});

		return false;
	});

	
	$('#filterform').change(function() {
		
		var filters = [];
		$('#filterform input:checked').each(function() {
			filters.push($(this).attr('rel'));
		});
		
		$.ajax({
			url		: '/ajax.php',
			type	: 'GET',
			dataType: 'json',
			data	: {
				vals	:	filters
			},
			success	: function(data){
				if(data != null){
					var results = data;
					
					/*for(var j=0; j<results.length;j++){
						list += "<li><a href='javascript:void(0);' rel='"+results[j].ID+"'>"+results[j].name+"</a></li>";
					}*/
					
					var stands = "";     
					
					$.each(results, function(j,obj){
						
						stands += '<div class="product"> \
						<a class="result result-thumb" rel="result-'+results[j].project_ID+'" href="/media/img/pics/_large/'+ results[j].images[0].url +'" title="'+results[j].project_title+'"> \
                            <img src="http://veranclassic.be.boaserver.be/media/img/loader.gif" class="lazy" width="215" height="" data-original="/media/img/pics/_thumb/'+ results[j].images[0].url +'" alt="" title="'+results[j].project_title+'" /> \
                        </a> \
						<div class="hidden">';
                        	var d = 0;
							$.each(results[j].images, function(p,obj){
								d++;
								if(d > 1){
									stands += '<a href="/media/img/pics/_large/'+results[j].images[p].url+'" class="result-thumb" rel="result-'+results[j].project_ID+'" title="'+results[j].project_title+'"></a>';
								}
							})
                            	
						stands += '</div></div>';
					})
					
					$('#results').html(stands);
					
					/* LAZY LOAD */
					if($('img.lazy').length > 0){
						$("img.lazy").lazyload({
							effect : "fadeIn"
						});
					}
					
					/* PROJECT HOVERS */
					$('.result').hover(function(){
						$(this).find('img').stop().animate({'opacity':'0.3'}, 500);
						$(this).append('<div class="z-overlay">&nbsp;</div>');
					}, function() {
						$(this).find('img').stop().animate({'opacity':'1'}, 500);
						$(this).find('.z-overlay').remove();
					});
					
				}else{
					if($('html').attr('lang') == 'nl'){
						$('#results').html('<a class="result"><h1 style="color:#EE3524; font-size: 13px; font-weight: normal;">Er werden geen resultaten gevonden.</h1></a>');
					}else{
						$('#results').html('<a class="result"><h1 style="color:#EE3524; font-size: 13px; font-weight: normal;">0 résultats</h1></a>');
					}
				}
			}
		});
		
		//return false;
	});
	
     $('.searchbutton').click(function(){
        $(this).parents('form').submit();
     });
     
     /* optionally show fields in case .shownextfield is selected */
     var requiredStar = '<span style="color:#E01639;">*</span>';
     
     $('form .nextishidden').each(function(){
        var fieldcontainer = $(this);
        $(this).next().addClass('hidden').hide().find('label').append(requiredStar);
        $(this).next().next().addClass('hidden').hide().find('label').append(requiredStar);;
        
        $(this).find('select').change(function(){
           if($(this).find(':selected').hasClass('shownextfield')){
              fieldcontainer.next().removeClass('hidden').slideDown().find('input').attr('alt','Required');
              fieldcontainer.next().next().removeClass('hidden').slideDown().find('input').attr('alt','Required');
           } else {
              fieldcontainer.next().addClass('hidden').slideUp().find('input').attr('alt','');
              fieldcontainer.next().next().addClass('hidden').slideUp().find('input').attr('alt','');
           }
        });
     });
     
});

function scrollNaarId(id){
	var newpos = $("#"+id).offset().top - 20;
	$('html,body').animate({scrollTop: newpos},'slow');
}

function showCustomCursor(){
        
  //  console.log('show custom cursor element');
	if($('#cursor').length==0){
		$('body').append('<div id="cursor"></div>');
	}
	//console.log($('#cursor'));
	$('body').mousemove(function(e){
	   /*console.log(e.clientY+' '+e.clientX);*/
	   $('#cursor').css('top', e.clientY-50).css('left', e.clientX-50);
	   
	   $('.fancybox-prev span').mouseenter(function(){
		 // console.log('add .prev');
		  $('#cursor').addClass('prev');
	   }).mouseleave(function(){
		  // console.log('remove .prev');
		  $('#cursor').removeClass('prev');
	   });
	   
	   $('.fancybox-next span').mouseenter(function(){
		  // console.log('add .next');
		  $('#cursor').addClass('next');
	   }).mouseleave(function(){
		  // console.log('remove .next');
		  $('#cursor').removeClass('next');
	   });
	});
 }

 function hideCustomCursor(){
   // console.log('hide custom cursor element');
   $('#cursor').remove();
   /*console.log('removed #cursor');*/
 }
