/**
 * Javascript Library
 */


/**
 * Document ready
 */


$(document).ready(function(){


	$('BODY').css({display: 'block'});

    $("#results_overlay").click(function () {
		
    	openResult();
    	
    });
    
    $("#results_link").click(function () {
    	
    	openResult();
    	
    });
    
    
    $('#abri_content').cycle({
		fx: 'scrollUp'
	});

	
	var uri = window.location.href;
	var uri_array = uri.split('/');

	if(uri_array[3] != "kef-key") {
	
		$("#key").hover(function() {
	       
	        $(this).addClass("hover").stop()
	            .animate({
	            	width: 701,
	                height: 308,
					left: 294
	            }, 200)
	            .css({cursor: 'pointer'});
	            		
	        }
	   
	    , function() {
	                   
	        $(this).removeClass("hover").stop()
	           .animate({
	                width: 456,
	                height: 200,
	                left: 540
	            }, 200);
	       
	    });
    
	   
		
	}
	
	$("#key").click(function () {
	            			
	   	location = '/kef-key';
	            			
	});
	
	$("#key_generator").click(function () {
		
		var html = $.ajax({
		  url: "/ajax/generateKey",
		  async: false
		 }).responseText;
		 
		 $("#txtKefkey").val(html);

		
	});
	
	
	 $('a.submit').click (function() {
        
	 	if ($(this).parents('form').length == 1)
            $(this).parents('form').submit();
        
        return false;
        
    });

    
    
    // validate signup form on keyup and submit
    if($("#formKefkey").length == 1) {
    	
		$("#formKefkey").validate({
			rules: {
				txtKefkey: {
					required: true,
					number: true,
					range: [10000, 99999]
				},
				txtInitials: "required",
				txtSurname: "required",
				txtStreet: "required",
				txtNumber: "required",
				txtZip: {
					required: true,
					zip: true
				},
				txtCity: "required",
				txtEmail: {
					required: true,
					email: true
				}
			},
			messages: {
				txtKefkey: "Vul een geldig KEF-key in",
				txtInitials: "Vul uw voorletters in",
				txtSurname: "Vul uw achternaam in",
				txtStreet: "Vul uw straat in",
				txtNumber: "Vul uw huisnummer in",
				txtZip: "Vul een juiste postcode in",
				txtCity: "Vul uw woonplaats in",
				txtEmail: "Vul een geldig e-mailadres in"
			}
		});
		
    }
    
    if($("#formContact").length == 1) {
    	
		$("#formContact").validate({
			rules: {
				firstname: "required",
				lastname: "required",
				address: "required",
				zipcode: {
					required: true,
					zip: true
				},
				location: "required",
				datebirth: "required",
				phone: "required",
				email: {
					required: true,
					email: true
				},
				remark: "required"
			},
			messages: {
				firstname: "Vul uw voornaam in",
				lastname: "Vul uw achternaam in",
				address: "Vul uw adres in",
				zipcode: "Vul een juiste postcode in",
				location: "Vul uw plaats in",
				datebirth: "Vul uw geboortedatum in",
				phone: "Vul uw telefoon in",
				email: "Vul een geldig e-mailadres in",
				remark: "Vul uw opmerkingen in"
			}
		});
		
    }
    
    
    $(".target_blank").attr({target: '_blank'});
    
    
    

    if($("#scroll-pane").height() > $("#content_overlay").height()) {
    
    	var value = $("#scroll-pane").height() - $("#content_overlay").height();
    	
		$("#slider-vertical").slider({
			orientation: "vertical",
			range: "max",
			min: 0,
			max: value,
			value: value,
			slide: function(event, ui) {
				
				scrollDiv(ui);
				
			}
		});
		
		function scrollDiv(ui) {
			
			ui.value = (ui.value - value) ;
				
			$("#scroll-pane").css({'margin-top':  ui.value});
			
		}
		
		function mouseScrollDiv(delta) {
		
			var min = 0;
			var max = ($("#scroll-pane").height() - $("#content_overlay").height()) * -1;
			
			if($("#scroll-pane").css('margin-top') == "auto") $("#scroll-pane").css({'margin-top' : 0})
			
			// current percentage from top
			var topMargin = (parseFloat($("#scroll-pane").css('margin-top')) / ($("#scroll-pane").height() - $("#content_overlay").height())) * 100;
				
			if(topMargin >= -100 && topMargin <= 0) {
			
				// current pixels from top
				var cm = parseFloat($("#scroll-pane").css('margin-top'));
				
				// new pixels from top
				var value = cm + (delta * 30);
				
				if(value > min) value = min;
				if(value < max) value = max;
				
				// new margin top in pixels
				$("#scroll-pane").css({'margin-top':  value});
				
				
				// new percentage from bottom
				var topMargin = (value / ($("#scroll-pane").height() - $("#content_overlay").height())) * 100;
				
				var bottomMargin =(((topMargin * -1) - 100) * -1);
				
				if (bottomMargin < 0) bottomMargin = 0;
				if (bottomMargin > 100) bottomMargin = 100;

				$("#slider-vertical a").css({bottom: bottomMargin + '%'})
				$("#slider-vertical div").css({height: (bottomMargin - 100) * -1 + '%'});
			
			}
		
		}
		
		$('#slider-vertical').bind('mousewheel', function(event, delta) {
			
			mouseScrollDiv(delta);
			return false;
	        
	    });
	
		$('#scroll-pane').bind('mousewheel', function(event, delta) {
	
			mouseScrollDiv(delta);
	        return false;

		});
		
    }
    
	

});


function openResult() {
	
	$("#results_wrapper").css({'z-index' : '9999'})
	
	$("#results").stop()
	    .animate({
            top: '-452px'
        }, 600)
        .find("#results_center")
        .animate({
            height: '241px'
        }, 600, function() { 
        
        	$(this).html($("#results_large").html());
        	
    	});
    
}

function closeResult(html) {
	
	$("#results").stop()
        .animate({
            top: '-275px'
        }, 600)
       
        .find("#results_center")
        .animate({
            height: '70px'
        }, 600, function() { 
        
        	$(this).html(html);
        	
        	 $("#results_wrapper").css({'z-index' : '2'})
        	
    	});
    	
    	
    	
}
