$(document).ready(function() {
	ILloadBasket();
	ILnav();
	ILtaalswitch();
	ILcatMainToggle("div.cat_group");
	ILcaroussel();
	ILmapEurope();
	ILheightFixerLoader();
	ILintroSlider("#ILintro");
	ILmorelessButtons();
	ILzoomImage();
	ILcortLinks();
	ILexternal();
	ILvalidate();
});

//////////////////////////////////////////////////////////////////////////////////////

jQuery.ajaxSetup({
  'beforeSend': function(xhr) {
    xhr.setRequestHeader("Accept", "text/javascript");
    }
})

//////////////////////////////////////////////////////////////////////////////////////


function ILcortLinks() {
	
	var cortPHP ="../cort/cortlink.php";
	var cortLoadDiv ="#ILcortLoader";
	
	$(cortLoadDiv).load(cortPHP+" div.global-wrapper",function(){
		
		ILexternal();
				
		$('#ILcortLoader a').each(function(){
			
			zelfbediening = new Array("Belgium","Netherlands","France","Luxembourg","Switzerland","Germany","Andorra","Austria","Liechtenstein");
			
			for (j=0;j<zelfbediening.length;j++) {
				var test = $(this).text();
				console.log(test);
				if (test == zelfbediening[j]) {
						$(this).next("br").remove();
						$(this).remove();
				}
			}
		});
	});
}

function ILexternal() {
	$("a[href^='http://']").each(function(){
		if($(this).is('.news_back')){  //voor terug knop bij news
			return true;	
		} else {
 			$(this).attr('rel','external');
		}
	});
 
	$("a[rel=external]").click( function(){
		window.open($(this).attr("href")); 
		return false;
		});
}

function ILzoomImage(){
	$("div.overlay").hide();
	$("div.prod_pic img").hover(
		function(){
			$(this).parent("div").append("<img class=\"zoomIcon\">");
			$("img.zoomIcon").attr("src","/images/zoom_in.png").hide();
			$("img.zoomIcon").fadeIn();
		},
		function(){
			$("img.zoomIcon").remove();
	});
	
	$("div.prod_pic img").click(function(){
		$("select").css("visibility","hidden"); // voor IE6 die de selects altijd helemaal on top toont :s
		$("div.overlay a").remove();
		soerse = $(this).attr("src"); // de soerse naar de extra large staat ook in de longdesc van den image
		soerse = soerse.slice(0,-9);
		
		$("div.overlay").append("<img>").wrapInner("<a>");
		$("div.overlay img").addClass("zoom");
		$("div.overlay img").attr("src",soerse+"extra_large.jpg");
		$("div.overlay a").attr("href","#").addClass("zoomClose");
		var docHoogte = $(document).height();
		var scrollHoogte = $(window).scrollTop();
		$("div.overlay").height(docHoogte);
		$("div.overlay").css("padding-top",scrollHoogte);
		$("div.overlay").fadeIn();
	
		$("a.zoomClose").click(function(){
			$("div.overlay").fadeOut("1500",function(){$(this).children("a").remove();});
			$("select").css("visibility","visible");
			return false;
		});
	});
}


function ILintroSlider(ID) {
	$(ID).cycle({
			fx: 'fade'
		});
}


function ILnav() {
	$("ul#ILbig_nav li a").each(function(i){$(this).addClass("bignav0"+i)});
}

function ILtaalswitch() {

	$("li#ILtaalswitch ul").hide();
	$("li#ILtaalswitch > a").click(function(){
		$("#ILtaalswitch ul").slideDown();
		return false;
	});
	$("#ILtaalkeuze").hover(
		function () {
			$(this).css("cursor", "pointer");
		},
		function () {
			$(this).hide();
		});
}

function ILcatMainToggle(DIV){
	
	// standaard gedrag: open geklapt
	// hieornder staan degene die toe moeten zijn
	
	$("#ILbody.ILservices_IL_rent "+DIV+" div.ILcat_products").hide();
	$("#ILbody.ILservices_IL_rent "+DIV+" h4").addClass("down");
	$("#extras div.ILcat_products").hide();
	$("#extras h4").addClass("down");
	
	//$("#ILbody.ILservices_IL_temp "+DIV+" div.ILcat_products").hide();
	//$("#ILbody.ILservices_IL_temp "+DIV+" h4").addClass("down");
	//$("h4.temp_extras").addClass("down");
	//$("h4.temp_extras").next().hide();

	$(DIV+" h4").wrapInner("<a></a>").children("a").attr("href","#");
	$(DIV+" h4 > a").click(function(){
			if($(this).parent("h4").next("div.ILcat_products").is(":visible")){
				$(this).parent("h4").next("div.ILcat_products").slideUp();
				$(this).parent("h4").addClass("down");
			} else {
				$(this).parent("h4").next("div.ILcat_products").slideDown();
				$(this).parent("h4").removeClass("down");
				ILheightFixer();
			}	

			return false;
		});	
}

function ILheightFixer(){
	if($("div.prod_pic img").is(":visible")){
	$("div.prod_pic img").each(function(){
			var oogte1 = $(this).height();
			var oogte2 = $(this).parent("div").next("div.prod_specs").height();
			oogte2 = oogte2 + 20; // padding 
			var oogteVerschil = oogte2 - oogte1;
			$(this).css("padding-bottom",oogteVerschil);
		});	
	}
}

function ILheightFixerLoader(){
	$(window).load(function(){ // Stafke Ari moet claire zijn, anders zijn de waarden verkeerd
			ILheightFixer();
		});	
}

function ILcaroussel() {
	$("div.caroussel_cntr ul").jcarousel({
			visible:6,
			scroll: 6,
			vertical: false
	});
}


function ILmapEurope() {
	
	$(".ILcountry").hoverIntent(
		
		function() {
			var titel = $(this).attr('title');
			
			$('#ILadres_Uitleg').hide();
			$('.ILadres.place').hide();
			$('#ILadres_'+titel).fadeIn('slow');
		},
		function() {
			$('.ILadres.place').fadeOut('fast');
		});
		
	$('img#europe').hover(function(){
		
		$('#ILadres_Uitleg').fadeIn();
		
	});
}




function ILloadBasket(){
	if($('#ILmandje').size() == 0){
		$.ajax({
			url: '/inlease/baskets/:current.js',
			dataType: 'html',
			success: function(data){
				$('#ILheader').append(data);
			} 
		});
	}
}

function ILmorelessButtons(){
	$('p.aantal a.less').click(function(){
		var $input = $(this).parent().find('input.amount');
		var value = parseInt($input.val());

		if (isNaN(value)){
			$input.val(1);
		}
		else{
			if(value > 1){
				$input.val(value - 1);
			}			
		}

		return false;
	});
	
	$('p.aantal a.more').click(function(){
		var $input = $(this).parent().find('input.amount');
		var value = parseInt($input.val());

		if (isNaN(value)){
			$input.val(1);
		}
		else{
			$input.val(value + 1);			
		}

		return false;
	});
	
	$('p.aantal a.inlease_basket_item_submit').click(function(e){
		var $form = $(this.rel);

		$form.ajaxSubmit({
			success: function(data){
				$('p.add_flash').remove();
				var $message = $(document.createElement('p'));
				$('#ILmandje').html(data);
				$('#ILmandje p.productMessage').slideDown();

				$message.addClass('add_flash').text($('#ILmandje p.productMessage').text()).fadeIn();
				$form.append($message.get(0));
				setTimeout(function(){
					$('#ILmandje p.productMessage').slideUp();
					$message.fadeOut();
				}, 2000);
			}
		});

		return false;
	});
	
	$('form.new_inlease_basket_item').ajaxForm({
	});
}

function ILvalidate(){
	
	$('form#contactForm').validate({
		rules:{
			Email: {
				required: true,
				email: true
		     	}
	  		}
	});
	
};

function noSpam(user){
	locationstring = "mailto:" + user + "@" + "in-lease.com";
	window.location = locationstring;
}


