var timeout    = 200;
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"});
}

function countTotal(type){
	if(type == 'verhuur'){
		var total = 0;
		$('span.price').each(function(index, value){
			total = eval(total) + eval($(this).text().replace(',','.'));
		});
		total = total.toFixed(2).replace('.',',');
		$('span.price_total').text(total);
		$('input.price_total').attr('value',total);
	}
}


$(document).ready(function(){
//	$('#nav > li').live('mouseover', jsddm_open);
//  	$('#nav > li').live('mouseout',  jsddm_timer);
  	
  	$('#slider').cycle({
		fx: 'fade' 
	});
	
		$('#promo').flash(
	{
		swf: 'http://www.youtube.com/v/o4pjGc1Vhdg?enablejsapi=1&amp;version=3&amp&rel=0&autohide=1&controls=false&iv_load_policy=3&border=0',
		height: '350',
		width: '440'
	});
	
  	$("a.fancybox").fancybox();
	
	$("a.film").click(function() {
	        $.fancybox({
    	        'padding'             : 0,
        	    'autoScale'   		  : false,
	            'transitionIn'        : 'none',
	            'transitionOut'       : 'none',
	            'title'               : this.title,
	            'width'               : 680,
	            'height'              : 495,
	            'href'                : this.href.replace(new RegExp("watch\\?v=", "i"), 'v/'),
	            'type'                : 'swf',    // <--add a comma here
	        	'swf'                 : {'allowfullscreen':'true'} // <-- flashvars here
    		});
	    return false;

    });
    
    // BOEKINGEN
    
    $('a.additem').live('click', function() {
    
    	var CurrentDiv = $(this).parent();
 		var NewDiv = $(CurrentDiv).clone().appendTo(CurrentDiv.parent());
 		$(this).fadeOut(500, function() { $(this).css('display:none'); });
 		$(NewDiv).removeClass('first');    
    });
	
	$('a.removeitem').live('click', function() {
		if($('div.cartitem').size() <4){
			$('a.additem'[0]).show(function(){$('a.additem'[0]).css('display:block !important')});
		}
 		$(this).parent().fadeOut(500, function() { $(this).remove(); });
    });
	
	
	$('select.cart_product').live('change', function() {
		var pieces = $(this).siblings('input.aantal').attr('value');
		var item_price = $(this).children('option:selected').attr('price');
		var total = pieces * item_price;
		total = total.toFixed(2);
		$(this).siblings('span.price').text(total.replace('.',','));
		countTotal('verhuur');
	});
	
	$('input.aantal').live('change', function(){
		var item_price = $(this).siblings('select.cart_product').children('option:selected').attr('price');
		var pieces = $(this).attr('value');
		var total = pieces * item_price;
		total = total.toFixed(2);
		$(this).siblings('span.price').text(total.replace('.',','));
		countTotal('verhuur');
	});
	
	// -EIND BOEKINGEN
	
	$.get('/php/siteConfig.php');
	
	$("form").submit(function(){
		if( checkForm($(this)) ){
			return true;
		} else {
			return false;
		}
	});
	
	$('table#fileManager td a.delete').click(function()
	    {
	        if (confirm("Weet u zeker dat u dit item wilt verwijderen?"))
	        {
	            var id = $(this).attr('id');
	            var data = 'id=' + id ;
	            var parent = $(this).parent().parent().parent();
	 
	            $.ajax(
	            {
	                   type: "POST",
	                   url: "/admin/classes/PhpFancyDirectoryBrowsing.inc/delete_row.php",
	                   data: data,
	                   cache: false,
	                   success: function(data)
	                   {
	                   	if(data == 'success'){
	                    	parent.fadeOut('slow', function() {$(this).remove();});
	                    }else{
	                    	alert('Het bestand kon niet worden verwijderd: ' +data);
	                    }
	                   },
	                   error: function()
	                   {
   						alert('Verwijderen van bestand is mislukt!');
  					   }

	             });
	        }
	    });

	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).hasClass("telefoon") && $(this).val().length < 10 )
				( $(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 if ( $(this).hasClass("telefoon") ){
					msg = '<span class="error-message" title="Dit is geen geldig telefoonnummer">&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");
	$(".format_datum:not(.no-js)").mask("99-99-2099");
	$(".format_time:not(.no-js)").mask("99:99");
	
	$(".centimeter").mask("99,99");
	$(".rent_time").live("change", function(){
		
		var rent_time = $(this).attr("value").split(':');
		
		if(eval(rent_time[0]) <= 11 && eval(rent_time[0]) >= 7){
			$("span.discount").text("Omdat u voor 12:00u huurt, ontvangt u 20% korting op de huurprijs!\n");
			$("input.discount").attr("value", "20");
		}else{
			$("span.discount").text(" ");
			$("input.discount").attr("value", "0");			
		}
	});

	$(".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 center'
	});

	

	

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;
