$(document).ready(function(){
	$('.news-latest').cycle({
		fx: 'scrollDown',
		timeout: 6000,
		delay: -2000
	});
	
	$('input[type="text"], input[type="password"], textarea').focus(function() {
		if(this.value == this.defaultValue){ 
			this.value = '';
		}
		if(this.value != this.defaultValue){
			this.select();
		}
	});
	$('input[type="text"], input[type="password"], textarea').blur(function() {
		if($.trim(this.value) == ''){
			this.value = (this.defaultValue ? this.defaultValue : '');
		}
	});
	
	$('.menu_sub, .menu_subsub').hide();
	
	$('.first').hover(
		function(){
			$(this).find('.menu_sub').show();
		},
		function(){
			$(this).find('.menu_sub').hide();
		}
	);
	$('.second').hover(
		function(){
			$(this).find('.menu_subsub').show();
		},
		function(){
			$(this).find('.menu_subsub').hide();
		}
	);
	
	var flashvars = {
		xmlURL: "fileadmin/flash/settings.xml"
	};
	var params = {
		wmode: "transparent"
	};
	var attributes = {};
	swfobject.embedSWF("fileadmin/flash/BannerRotator.swf", "keyvisual", "700", "202", "8.0.0",false, flashvars, params, attributes);

	
	$('.datum').datepicker({
		changeMonth: true,
		changeYear: true, 
		yearRange: '-00:+01',
		minDate: +1,
		showWeek: true
	});
	
	/* Inloggen CASHWeb*/	
	$('#wachtwoord1').show();
	$('#wachtwoord2').hide();
	 
	$('#wachtwoord1').focus(function() {
	    $('#wachtwoord1').hide();
	    $('#wachtwoord2').show();
	    $('#wachtwoord2').focus();
	});
	$('#wachtwoord2').blur(function() {
	    if($('#wachtwoord2').val() == '') {
	        $('#wachtwoord1').show();
	        $('#wachtwoord2').hide();
			$('#wachtwoord1').val('Wachtwoord');
	    }
	});
	
	$('#check_uid26_0').attr('checked', true);
	
	/* Aanmelden proefaccount */	
	$('#powermaildiv_uid75').change(function(){
		$('#powermaildiv_uid75 option:selected').each(function (){
			if($(this).val() == 'CASHWeb (online)') {
				$('#powermaildiv_uid71').fadeIn(1000);
			}
			else {
				$('#powermaildiv_uid71').hide();
				$('#powermaildiv_uid72').hide();
				$('#powermaildiv_uid73').hide();
				$('#powermaildiv_uid74').hide();
				
				for(i = 0; i <= 8; i++){
				$('#check_uid72_'+i).attr('checked', false);
				}
				for(i = 0; i <= 3; i++){
				$('#check_uid73_'+i).attr('checked', false);
				}
				for(i = 0; i <= 2; i++){
				$('#check_uid74_'+i).attr('checked', false);
				}
			}
		});
	}).change();
	$('#powermaildiv_uid71').change(function(){
		$('#powermaildiv_uid71 option:selected').each(function (){
			if($(this).val() == 'CASH Financieel') {
				$('#powermaildiv_uid72').fadeIn(1000);
				$('#powermaildiv_uid73').hide();
				$('#powermaildiv_uid74').hide();
			}
			else if($(this).val() == 'CASH Compact') {
				$('#powermaildiv_uid72').hide();
				$('#powermaildiv_uid73').fadeIn(1000);
				$('#powermaildiv_uid74').hide();
			}
			else if($(this).val() == 'CASH Compact Pro') {
				$('#powermaildiv_uid72').hide();
				$('#powermaildiv_uid73').hide();
				$('#powermaildiv_uid74').fadeIn(1000);
			}
			else {
				$('#powermaildiv_uid72').hide();
				$('#powermaildiv_uid73').hide();
				$('#powermaildiv_uid74').hide();
			}
		});
	}).change();
	
	$('#check_uid72_0').change(function(){
		if($(this).attr('checked')){
			$('.powermail_check_inner_uid72_1').fadeOut(500);
		}
		else{
			$('.powermail_check_inner_uid72_1').fadeIn(500);
		}
	});
	$('#check_uid72_1').change(function(){
		if($(this).attr('checked')){
			$('.powermail_check_inner_uid72_0').fadeOut(500);
		}
		else{
			$('.powermail_check_inner_uid72_0').fadeIn(500);
		}
	});
	
	$('.functieoverzicht table').hide();
  	$('.functieoverzicht h2').click(function() { 
    	$(this).next('table').slideToggle('slow');
		if($(this).attr('class') != 'actief'){
			$(this).removeClass('inactief');
			$(this).addClass('actief');
		} else {
			$(this).removeClass('actief');
			$(this).addClass('inactief');
		}
	}); 
	
	/* Restore and conserve login data when the page is loaded or the login form is submit */
	$("#inloggen").submit(function() {
		ConserveLoginData();
		return true;
	});
	RestoreLoginData();
});

// Cookie helper functions
// Creates a new cookie or overwrite an existing one
function CreateCookie(name, value, days)
{
  if(days)
  {
    var date = new Date();
    date.setTime(date.getTime()+(days*24*60*60*1000));
    var expires = "; expires="+date.toGMTString();
  }
  else
    var expires = "";

  document.cookie = name+"="+value+expires+"; path=/;";
}
// Read the value of a cookie. Returns either the value or null if cookie doesnt exists
function ReadCookie(name)
{
  var ourName = name + "=";
  // Iterate over all cookies
  var ca = document.cookie.split(';');
  for(var i=0;i < ca.length;i++)
  {
    var c = ca[i];

    // Iterate over each character till we find a space,
    // trimming the first character each time
    while (c.charAt(0)==' ')
      c = c.substring(1,c.length);

    // c now contains the name of the cookie we're iterating, compare it to our name
    if (c.indexOf(ourName) == 0)
      // Return the part after the name (which is the value)
      return c.substring(ourName.length, c.length);
  }
  return null;
}

function ConserveLoginData()
{
  CreateCookie("remember", "yes", 7);
  CreateCookie("login", $("input[name=login]").val(), 7);
  CreateCookie("client", $("input[name=client]").val(), 7);
}
function RestoreLoginData()
{
  var remember = ReadCookie("remember");
  if(remember && remember == "yes")
  {
    var login = ReadCookie("login"), client = ReadCookie("client");
    if(login)
      $("input[name=client]").val(client);
    if(client)
      $("input[name=login]").val(login);
  }
}