$(function(){
	
	
	$('#menuTop li a').prepend("<div class='arrow'></div>");
	//$('#menuTop ul li ul').append("<div class='bottom'></div>");
	$('#menuTop ul li ul li a .arrow').remove();
	$('#menuTop ul li').click(function(){
		if($(this).attr('rel') != 1){
		$('#menuTop ul li').attr('rel','0');
		$('#menuTop ul li').removeClass('clicked');
		$(this).attr('rel','1');
		$(this).addClass('clicked');
		$('#menuTop ul li ul').css({display:"none"});
		topWidth = $(this).css('width');
		topWidth = topWidth.replace("px","");
		topWidth = Math.floor(topWidth);
		topWidth = topWidth + 21;
		$("ul",this).css({display:"block", width:topWidth + "px", opacity:0});
		$("ul",this).animate({opacity:1},200);
		return false;
		}
		
		if($(this).attr('rel') == 1){
		$('#menuTop ul li').attr('rel','0');
		$('#menuTop ul li').removeClass('clicked');
		$('ul',this).css({display:"none"});
		return false;
		}
	});
	$('#menuTop ul li ul li').click(function(){
		var theLink = $(this).children().attr('href');
		window.location= theLink;
	});
	
	customerIDprefill = $('#loginBox input[name="customerID"]').val();
	customerPasswordPrefill = $('#loginBox input[name="customerPassword"]').val();
		
	$('#loginBox input[name="customerID"]').focus(function(){
		if($(this).val() == customerIDprefill){
		$(this).removeClass('prefilled');
		$(this).val('');
		}
	});
	$('#loginBox input[name="customerID"]').blur(function(){
		if($(this).val() == ''){
			$(this).val(customerIDprefill);
			$(this).addClass('prefilled');
		}
	});
	$('#loginBox input[name="customerPassword"]').live("focus",function(){
		if($(this).val() == customerPasswordPrefill){
			$('.customerPasswordHolder').html('<input type="password" value="" name="customerPassword" /> <a href="#login" onclick="customerLogin(); return false;">Login</a>');
			$('.customerPasswordHolder input').focus();
		}
	});
	$('#loginBox input[name="customerPassword"]').live("blur",function(){
		if($(this).val() == ''){
			$('.customerPasswordHolder').html('<input type="text" class="prefilled" value="Vul uw wachtwoord in..." name="customerPassword" /> <a href="#login" onclick="customerLogin(); return false;">Login</a>');
			
		}
	});
	$('#loginBox input[name="customerPassword"]').live('keydown',function(event){
		if(event.which == '13'){
			customerLogin();
		}
	});
	
	
	
});

function customerLogin(){
	jCustomerID = $('#loginBox input[name="customerID"]').val();
	jCustomerPassword = $('#loginBox input[name="customerPassword"]').val();
	if(jCustomerID != '' && jCustomerPassword != '' && jCustomerID != customerIDprefill && jCustomerPassword != customerPasswordPrefill){
		$.post("http://arbori-arnhem.nl/klantensysteem/db-interaction/checkLogin.php", { klantID: jCustomerID, klantPassword: jCustomerPassword },
			 function(data) {
				 	if(data == 'true'){
					$.cookie('customerID', jCustomerID);
					$.cookie('customerPassword', MD5(jCustomerPassword));
					window.location = "http://arbori-arnhem.nl/klantenlogin/";
					}
					if(data != 'true'){
					alert('Het wachtwoord en/of de klantcode is onjuist.'); 	
					}
			 }
		);	
	}	
}


