function getFlashMovie(movieName){
	var isIE = navigator.appName.indexOf("Microsoft") != -1;
	return (isIE) ? window[movieName] : document[movieName];
}

function playYoutube(key){
	var swf = getFlashMovie('youtubeVideo');
	if (swf){
		swf.playYoutube(key);
	}
}

function startYoutube(){
	var swf = getFlashMovie('youtubeVideo');
	if (swf){
		swf.startYoutube();
	}
}


$(document).ready(function() {
	$(".moreDesc").css("display","none");

	//Hoofdnavigatie mouseovers
	$("#menu img").hover(
		function(){
			var imgsrc = $(this).attr("src")
			if(imgsrc.indexOf('-on.gif')==-1){
				imgsrc = imgsrc.replace('.gif','-on.gif');
				$(this).attr('src',imgsrc);
			}
		},
		function(){
			var thisclass = $(this).attr("class")
			if(thisclass.indexOf('active')==-1){
				var imgsrc = $(this).attr("src").replace('-on.gif','.gif');
				$(this).attr('src',imgsrc);
			}
		}
	);

	$('.thumblist li a').click(function(){
		if ($(this).parent().hasClass('youtubethumb')){
			$('#youtubeVideo').show();
			$('.mainimage').hide();
			$('.imgbuttons').hide();
			$('#bigimage').hide();
			
		}else{
			if ($('#youtubeVideo').size() > 0){
				$('#youtubeVideo').hide();
			}
			$('.mainimage').show();
			$('.imgbuttons').show();
			$('#bigimage').show();
			var new_image = ($(this).attr('href'));
			var new_image_big = ($(this).attr('rel'));
			$('.mainimage img').attr('src',new_image);
			$('#bigimage').attr('href',new_image_big);
		}
		return false;
	})

//Hoofdnavigatie mouseovers
	$(".hover").hover(
		function(){	$(this).addClass('on'); },
		function(){	$(this).removeClass('on'); }
	);

	$(".mOn").hover(
		function(){	mOn(this); },
		function(){	mOut(this); }
	);


	$(".showMore").click(function(){
			var thisId = (this).id.replace('sub','');
			$(this).hide();
			$("#arrow-more" + thisId).hide();
			$("#showSub" + thisId).slideDown("slow");
			return false;
		}
	);
	
	$(".check").click(function(){
			var imgsrc = $(this).find("img").attr("src");
			var input = $(this).find("input");
			if(imgsrc.indexOf('-error')>-1){
				imgsrc = imgsrc.replace('-error','');
			}
			if(imgsrc.indexOf('-on.gif')==-1){
				imgsrc = imgsrc.replace('.gif','-on.gif');
				$(this).find("img").attr('src',imgsrc);
				if ($(input).size() > 0){
					$(input).attr("value","1");

				}
				if ( $(this).attr("id") == 'showDelivery' ) {
					$('.delivery').show();
				}
			}else{
				imgsrc = imgsrc.replace('-on.gif','.gif');
				$(this).find("img").attr('src',imgsrc);
				if ($(input).size() > 0){
					$(input).attr("value","0");
				}
				if ( $(this).attr("id") == 'showDelivery' ) {
					$('.delivery').hide();
				}
			}
			if (document.getElementById('disclaimer')){
				chckField($('#disclaimer').get(0),$('#disclaimer').get(0),$('#disclaimer').attr('title'));
				if ($('.checkform').find('.error').size() == 0){
					$('#formerror').hide();
					$('#submit').show();
				}
			}
		}
	);

	$(".article .changeAmount .more").add(".article .changeAmount .less").click(function(){
			//Zoek de bovenliggende div, genaamd changeAmount, waarin alle wijzingen plaatsvinden
			var mainDiv = ($(this).closest(".changeAmount"));
			//de te wijzigen inputfield
			var amount = ($(mainDiv).find("input"));
			//de huidige waarde, geconverteerd naar integer
			var val = parseInt($(amount).attr("value"));
			//als de knop uit de container met class more is, wordt de nieuwe waarde +1
			if ($(this).attr("class")=='more'){
				val = val + 1;
			//anders wordt de nieuwe waarde -1, mits de waarde groter is dan 0
			}else if(val > 0){
				val = val - 1;
			}
			$(amount).attr('value',val);
		}
	);

	$(".showMoreDesc").click(function(){
			var textItem = $(this).closest("p");
			$(textItem).find(".moreDesc").show();
			$(this).remove();
			return false;
		}
	);

	$(".btn-sitemap").click(function(){
			if ($("#blockSitemap").css("display") == 'none'){
				$("#blockSitemap").slideDown("slow")
				$(".btn-sitemap.open").hide();
				$(".btn-sitemap.close").show();
			}else{
				$("#blockSitemap").slideUp("slow",function(){
					$(".btn-sitemap.close").hide();
					$(".btn-sitemap.open").show();
				});
			}
			return false;
		}
	);
	
	$("#submit").click(function(){
			if ($('.checkform').find('.error').size() > 0){
				$('#formerror').show();
				$('#submit').hide();
			}
		}
	);

	$(".paymethod").hover(
		function(){	$(this).addClass('hover'); },
		function(){	$(this).removeClass('hover'); }
	);

	$('.checkform input').add('.checkform textarea').blur(function(){
		if ($(this).attr('title')){
			if ($('.checkform').find('.error').size() == 0){
				$('#formerror').hide();
				$('#submit').show();
			}
		}
	});
	$('.forgot').click(function(){
		$('.blockSentpass').show();
		$('.blockLogin').hide();
		return false;
	});

	$('.backtologin').click(function(){
		$('.blockLogin').show();
		$('.blockSentpass').hide();
		return false;
	});

	$("#btn-mailfriend").click(function(){
		submit_onclick('frmContact',false)
		if ($('#frmContact').find('.error').size() > 0){
			return false
		};
		
		$.ajax({
				type: "POST",
				url: PROJECT_URL + "xml/mailfriend.asp",
				dataType: "html",
				data : {
						'email_sender' : $('#email-sender').val(),
						'name_sender' : $('#name-sender').val(),
						'email_receiver' : $('#email-receiver').val(),
						'name_receiver' : $('#name-receiver').val(),
						'link' : $('#link').val(),
						'remarks' : $('#remarks').val()
				},
				success: function(msg){
					 $('#highslide-body').html(msg);
			 }
		});
		return false
	});
	
	$('.search input').keypress(function(e) {
		if (e.which==13) {
			searchFor($(this).val());
			return false;
		}
	});

	$(".input_delivery").click(function(){
		$(".deliverytext").hide();
		$("#deliverytext-" + $(this).val()).show();
		if ($(this).val()== 2){
			$("#delivery-options-2").show();
		}else{
			$("#delivery-options-2").hide();
		}
	});
	
	$(".paymethod input").click(function(){
		if ($(this).attr("id") == 'rembours'){
			$('#pickup').css('visibility','hidden'); 
			$('#tnt').css('visibility','hidden'); 
			$('#evening_delivery').css('visibility','hidden'); 
		}else{
			$('#pickup').css("visibility","visible"); 
			$('#tnt').css("visibility","visible"); 
			$('#evening_delivery').css("visibility","visible"); 
		}
	});

	$(".delivery .options input").click(function(){
		if ($(this).attr("id") == 'pickup' || $(this).attr("id") == 'tnt' || $(this).attr("id") == 'evening_delivery'){
			$('#rembours').css('visibility','hidden'); 
		}else{
			$('#rembours').css("visibility","visible"); 
		}
	});

	$(".deliverycheck input").keypress(function(e) {
		if (e.which==13) {
			checkZipcode($(this).val());
			return false;
		}
	});

	$('.offer ul').show();
	$('.offer ul').innerfade({ 
		animationtype: 'slide',
		speed: 1000,
		timeout: 4000,
		type: 'random',
		containerheight: '1em'
	}); 

	$("#initialen, #voornaam").blur(function(e) {
		if ($('#email').val() == '' && $('#email-login').size) {
			$('#email').attr('value', $('#email-login').val());
		}
	});
})

function mOn(obj) {
	var ext = obj.src.slice(-3)
	if(obj.src.indexOf('-on.'+ ext)==-1){
		obj.src=obj.src.replace('.'+ ext,'-on.'+ ext)
	}
}

function mOut(obj) {
	var ext = obj.src.slice(-3)
	obj.src=obj.src.replace('-on.'+ext,'.'+ext)
}

function searchFor(v){
	if (v){
		document.location.href = PROJECT_URL + tr_search + '/'+escape(v)
	}
}

function checkZipcode(v){
	if (v){
		document.location.href = PROJECT_URL + 'bezorgen-en-betalen?zipcode='+escape(v)
	}
}

function flashMovie(movieName) {
	if (navigator.appName.indexOf("Microsoft") != -1) {
		var target = document.getElementById(movieName);
		return target;
	} else {
		return document[movieName];
	}
}

function updateFlashData(file,articleId) {
	if (document.getElementById("productViewer_" + articleId)) {
		flashMovie("productViewer_" + articleId).updateData(file);
	}
}

function setDateOfBirth() {
	if (document.getElementById("dob_dd").options[document.getElementById("dob_dd").selectedIndex].value == "" || document.getElementById("dob_mm").options[document.getElementById("dob_mm").selectedIndex].value == "" || document.getElementById("dob_yy").options[document.getElementById("dob_yy").selectedIndex].value == "") {
		document.getElementById("dateOfBirth").value = '';
	}else{
		document.getElementById("dateOfBirth").value = document.getElementById("dob_dd").options[document.getElementById("dob_dd").selectedIndex].value + '-' + document.getElementById("dob_mm").options[document.getElementById("dob_mm").selectedIndex].value + '-' + document.getElementById("dob_yy").options[document.getElementById("dob_yy").selectedIndex].value
	}
}

function openBig(url){
	if(url!='') $('#bigimage').attr('href',url);
	document.getElementById('bigimage').onclick();
}


function printme() {
	var objOut = document.getElementById("output");
	var out = objOut.innerHTML;
		
	var wh = window.open("","wh","width=640px,height=480px,scrollbars=yes");
	wh.document.open();
	var output = '<link href="' + PROJECT_URL + 'css/print.css" rel="stylesheet" type="text/css">';
	output = '<html><head>'+output+'</head><body>'+out.replace('<object','<!--object').replace('/object>','/object-->')+'</body></html>';
	output += '<'+'script type="text/javascript">';
	output += 'setTimeout("window.print()",2000);';	
	output += '</'+'script>';
	wh.document.writeln(output);
	wh.document.close();
}

function controlRekeningnummer() {
  if (form.betaalWijze.options[form.betaalWijze.selectedIndex].value=='<%=con_BetalenVooruit%>') {
    form.bankgiro.value='';
    form.bankgiro.disabled=true;
    }
  else {
    form.bankgiro.disabled=false;
  }
}

function submit_onclick_wrapped() {
	var okBank = false;
	var okDate = false;
	
  if (document.getElementById("bankgiroNr").value==""){
		$("#bankgiroNr").addClass('error');
		document.getElementById("return_bankgiroNr").innerHTML = " Dit veld is verplicht";
		okBank=false;
	}else{
		var elfCheck = doElfCheck(document.getElementById("bankgiroNr"))
		if (elfCheck == "") {
			$("#bankgiroNr").removeClass('error');
			document.getElementById("return_bankgiroNr").innerHTML = "";
			okBank = true;
		}else{
			$("#bankgiroNr").addClass('error');
			document.getElementById("return_bankgiroNr").innerHTML = elfCheck;
			okBank=false;
		}
	}
	
	if (document.getElementById("dob_dd").value=="" || document.getElementById("dob_mm").value=="" || document.getElementById("dob_yy").value==""){
		if (document.getElementById("dob_dd").value==""){
			document.getElementById("dob_dd").style.backgroundColor = "#d4d4d4";
		}else{
			document.getElementById("dob_dd").style.backgroundColor = "#FFFFFF";
			document.getElementById("dob_dd").style.color = "#000000";		
		}
		if (document.getElementById("dob_mm").value==""){
			document.getElementById("dob_mm").style.backgroundColor = "#d4d4d4";
		}else{
			document.getElementById("dob_mm").style.backgroundColor = "#FFFFFF";
			document.getElementById("dob_mm").style.color = "#000000";		
		}
		if (document.getElementById("dob_yy").value==""){
			document.getElementById("dob_yy").style.backgroundColor = "#d4d4d4";
		}else{
			document.getElementById("dob_yy").style.backgroundColor = "#FFFFFF";
			document.getElementById("dob_yy").style.color = "#000000";
		}
		document.getElementById("return_dateOfBirth").innerHTML = " Dit veld is verplicht";
		okDate=false;
	}else{
		if (FV_IsDateIsBirthDate(document.getElementById("dateOfBirth").value)) {
			document.getElementById("dob_dd").style.backgroundColor = "#FFFFFF";
			document.getElementById("dob_dd").style.color = "#000000";
			document.getElementById("dob_mm").style.backgroundColor = "#FFFFFF";
			document.getElementById("dob_mm").style.color = "#000000";
			document.getElementById("dob_yy").style.backgroundColor = "#FFFFFF";
			document.getElementById("dob_yy").style.color = "#000000";
			document.getElementById("return_dateOfBirth").innerHTML = "";			
			okDate = true;
		}else{
			document.getElementById("dob_dd").style.backgroundColor = "#d4d4d4";
			document.getElementById("dob_mm").style.backgroundColor = "#d4d4d4";
			document.getElementById("dob_yy").style.backgroundColor = "#d4d4d4";
			document.getElementById("return_dateOfBirth").innerHTML = "U dient tenminste 18 jaar oud te zijn!";
			okDate = false;		
		}
	}
	
	if (okDate == true && okBank == true) {
		document.getElementById("frmPaymethod").submit();
	}
}

// deze functie doet eerst de telefoonnummer check, en vervolgens de bestaand check
function submit_onclick_lindorff() {
	var i, val, msgs = [],
		$phone = $('#phone'),
		$mobile = $('#mobile'),
		phone = $phone.val(), 
		mobile = $mobile.val();
	
	// check alleen uitvoeren als de velden bestaan
	if ($phone.size() && $mobile.size()) {
		// eerst telnummers controleren via ajax, on succes reguliere formcheck uitvoeren
		$.ajax({
			type : "GET",
			url : PROJECT_URL + "xml/lindorffCheckPhoneMobile.json.asp",
			data : { "phone" : phone, "mobile" : mobile },
			dataType : "json",
			success : function(data) {
				if (data.isValid) {
					submit_onclick_wrapped();
				} 
				else {
					$.each(data.feedback, function(i, val) {
						msgs.push('- ' + htmlDecode(val));
					});
					$('#feedbackphonemobile').html(msgs.join('<br>'));
					//alert('Uw telefoongegevens zijn nog niet juist.\n' + msgs.join('\n'));
				}
			},
			error : function(XMLHttpRequest, textStatus, errorThrown) { 
				//alert("Er is een fout opgetreden bij de validatie" + textStatus + errorThrown);
			}
		});
	}
	else {
		submit_onclick_wrapped();
	}
}

function htmlDecode(s) {
	return $('<div>' + s + '</div>').text();
}

// from John Resig's book on JS
var getStyle = function (elem, name) {
	if (elem.style[name]) {
		return elem.style[name];
	}	else if (elem.currentStyle) {
		return elem.currentStyle[name];
	}	else if (document.defaultView && document.defaultView.getComputedStyle) {
		name = name.replace(/([A-Z])/g, "-$1");
		name = name.toLowerCase();
		var s = document.defaultView.getComputedStyle(elem, "");
		return s && s.getPropertyValue(name);
	} else {
	return null;
	}
}

var isHidden = function (dOb) {
	while (dOb && dOb !== document && dOb.tagName !== undefined) {
	if (getStyle(dOb, 'display') === 'none') {
		return true;
	}
	dOb = dOb.parentNode;
	}
	return false;
}
