var timeout    = 500;
var closetimer = 0;
var ddmenuitem = 0;

function jsddm_open()
{  jsddm_canceltimer();
   jsddm_close();
   ddmenuitem = $(this).find('ul').css('visibility', 'visible');}

function jsddm_close()
{  if(ddmenuitem) ddmenuitem.css('visibility', 'hidden');}

function jsddm_timer()
{ /* closetimer = window.setTimeout(jsddm_close, timeout); */}

function jsddm_canceltimer()
{  if(closetimer)
   {  window.clearTimeout(closetimer);
      closetimer = null;}}

function openlogin(){
	$("#loginwindow").dialog({modal:true,width:400,title:"Inloggen"});
}


$(document).ready(function(){
	$('#nav > li').live('mouseover', jsddm_open);
  	$('#nav > li').live('mouseout',  jsddm_timer);
  	
  	$('#slide').cycle({
		fx: 'fade' 
	});
	
  	$("a.fancybox").fancybox();
	
	$.get('/php/siteConfig.php');
	
	$("form").submit(function(){
		if( checkForm($(this)) ){
			return true;
		} else {
			return false;
		}
	});
	
	function checkForm(form){
		var errors 	= 0;
		
		$(".error",form).removeClass("error");
		$(".error-message",form).remove();
		
		
		$(".verplicht",form).each(function(){
		
			if( 
				$(this).val()=="" ||
				$(this).val()==$(this).attr("title") ||
				( $(this).hasClass("minvalue") && $(this).val() < 1000 ) ||
				( $(this).attr("type")=="password" && $(this).val().length<6 )
			){
				var parent		= $(this).parent().find('label');
				var hasLabel	= (parent.size()>0);		
				var label		= (hasLabel) ? parent.html().replace(" *","") : "Dit";
				var msg;
				
				if( $(this).hasClass("minvalue") ){
					msg = '<span class="error-message" title="'+label+' bevat geen geldige invoer">&nbsp;</span>';
				} else if ( $(this).attr("type")=="password" ){
					msg = '<span class="error-message" title="Minimaal 6 tekens">&nbsp;</span>';
				} else {
					msg = '<span class="error-message" title="'+label+' is een verplicht veld">&nbsp;</span>';
				}
				
				$(this).addClass('error').after(msg);
				$(this).next()
				.css("left", $(this).position().left+$(this).width() )
				.tooltip({
					tip: '#tt-alert',
					effect: 'fade',
					position: 'center right'
				}); 
				errors++;
			}
			
			
			
		});
		$(".error:first",form).focus();
		
		return (errors==0);
	}
	
	$.fn.textWidth = function(){
		var sensor = $('<div />').css({margin: 0, padding: 0});
		$(this).append(sensor);
		var width = sensor.width();
		sensor.remove();
		return width;
	};

	
	function checkbtns(){
		$(".button").each(function(){
			if( !$(this).parent().hasClass("dialog-window") ){
				var txtwidth = $(".m", this).textWidth();
				$(this).css("width",txtwidth+20);
			}
		});
	}
	
	
	if( $.browser.msie && $.browser.version=='7.0' ){ checkbtns(); }
	
	function numeric(input){
		return input.replace(/[a-z]/gi,'').replace(/\./g,'');
	}
	
	function priceFormat(num){
		var output	= '';
		var len 	= num.length-1;
		var dot		= [];
		
		if( len >= 9 ){ dot[9] = '.'; }
		if( len >= 6 ){ dot[6] = '.'; }
		if( len >= 3 ){ dot[3] = '.'; }
		
		for(var i=len; i>=0; i--){
			output += num.charAt(len-i);
			if( dot[i]=='.' ){ output += '.'; }
		}
				
		return output;
	}
	
	$(".numeric").keyup(function(){
		$(this).val( numeric( $(this).val() ) );
	});
	$(".price").each(function(){
		$(this).val(priceFormat(numeric( $(this).val() )));
	}).keyup(function(){
		$(this).val(priceFormat(numeric( $(this).val() )));
	});
	
	
	$(".zipcode:not(.no-js)").mask("9999aa");
	$(".centimeter").mask("99,99");

	$(".clickable").hover(
		function(){ $(this).addClass("hovering"); },
		function(){ $(this).removeClass("hovering"); });
	$(".clickable:not(.agreement)").click(function(){
		window.location = $(".target", this).attr("href");
	});
		
	$(".open-dialog").click(function(){
		var link = $(this);
		
		if( !link.hasClass("prevent") ){
		
			var node = $(this).next('.dialog-window');
			var header = node.find('h3');
			
			node.find('h3').remove();		
			
			node.dialog({
				width: (node.hasClass("small")) ? 300 : 600,
				modal: true,
				title: header.html(),
				beforeclose: function(e,ui){
					killDialog();
					return false
				}
			});
			
			node.find('.close-dialog').click(function(){
				killDialog();
				return false;
			});
			
			node.find('.submit').click(function(){
				node.find('form').submit();
				return false;
			});
			
			function killDialog(){
				node.dialog('destroy');
				node.prepend(header);
				link.after(node);
			}
			
		}
		
		return false;
	});
	
	$(".icon[title]").tooltip({
		tip: '#tt-info',
		effect: 'fade',
		position: 'center right'
	});
	
	$(".infoicon[title]").tooltip({
		tip: '#tt-hint',
		effect: 'fade',
		position: 'center right'
	});

	

	

function externalLinks() {
 if (!document.getElementsByTagName) return;
 var anchors = document.getElementsByTagName("a");
 for (var i=0; i<anchors.length; i++) {
   var anchor = anchors[i];
   if (anchor.getAttribute("href") &&
       anchor.getAttribute("rel") == "group")
     anchor.target = "_blank";
 }
}
window.onload = externalLinks;
	
	
});

document.onclick = jsddm_close;
