/**
 * Validate forms
 */

function validateForm() {

	var errors = 0;

	var reqFields = $('input.required');
	
	for ( i=0; i<reqFields.length; i++ ) {
	
		switch ( $(reqFields[i]).attr('type') ) {
		
			case "text":
			
				if ( $(reqFields[i]).val() == "" && $(reqFields[i]).attr('disabled') == false ) {
				
					$(reqFields[i]).addClass('error');
					
					errors++;
				
				} else {
				
					$(reqFields[i]).removeClass('error');
				
				}
			
				break;
				
			case "password":
			
				if ( $(reqFields[i]).val() == "" && $(reqFields[i]).attr('disabled') == false ) {
				
					$(reqFields[i]).addClass('error');
					
					errors++;
				
				} else {
				
					$(reqFields[i]).removeClass('error');
				
				}
			
				break;
				
			case "checkbox":
			
				if ( $(reqFields[i]).attr('checked') == false ) { 
				
					$(reqFields[i]).addClass('error');
					
					errors++;
				
				} else {
				
					$(reqFields[i]).removeClass('error');
				
				}
			
				break;
		
		}
		
	}
	
	if ( $('[name=forum]').length == 1 && $('[name=forum]').attr('checked') == true ) {
	
		if ( $('[name=fpassword]').val() != $('[name=confirm]').val() ) {
		
			$('[name=fpassword]').addClass('error');
			$('[name=confirm]').addClass('error');
			
			errors++;
		
		} else {
		
			if ( $('[name=fpassword]').val() != "" &&  $('[name=confirm]').val() != "" ) {
		
				$('[name=fpassword]').removeClass('error');
				$('[name=confirm]').removeClass('error');
				
				}
		
		}
	
	}
	
	if ( errors == 0 ) {
	
		return true;
	
	} else {
	
		alert("U heeft niet alle velden correct ingevuld.");
	
		return false;
		
	}

}

function validateNewsletterForm() {

	var elem = $('form#newslettersubscribe ul li input');
	
	for (i=0; i<elem.length; i++) {
		
		switch ($(elem[i]).attr('type')) {
		
			case "text":
				
				if ($(elem[i]).val() == "") {
			
					var fieldName = $(elem[i]).parents('li').find('span').html();
					
					alert("Verplicht veld '" + fieldName + "' is nog niet ingevuld.");

					return false;
					
				}
				
				break;
		
		}
		
	}
	
	return true;
	
}

function sendFormForgot() {

	$.post('/', { action: 'sendFormForgot', email: $('[name=email]').val() }, function (response) {
	
		response = eval('('+response+')');
				
		$('p#msg').addClass(response.type)
			.html(response.msg)
			.fadeIn(200, function () { 
			
				setTimeout(function () {
				
					$('p#msg').fadeOut(200, function () {
					
						$(this).removeClass('error');
						$(this).removeClass('success');
					
					});
				
				}, 2000);
		
			 });
	
	});
	
	return false;

}

function initForms() {

	if ( $('input#chkForum').length == 1 ) {
	
		$('input#chkForum').click(function () {
		
			if ( $(this).attr('checked') == true ) {
			
				$('[name=fpassword]').attr('disabled', false);
				$('[name=confirm]').attr('disabled', false);
			
				$('[name=fpassword]').focus();
			
			} else {
			
				$('[name=fpassword]').attr('disabled', true);
				$('[name=confirm]').attr('disabled', true);
			
			}
		
		});
	
	}

}

function initCarousel() {
	
	$('ul.image-shadow').innerfade({
            speed: 1000,
            timeout: 5000,
            type: 'sequence',
            containerheight: '487px'
    });
				
}

/**
 * Document ready
 * @desc Initialize (web) interface
 */

$(document).ready(function () {

	/**
	 * Interface
	 */
	
	$(document).iFace();
	
	$(document).initInterface();
		
		
		
	$('[name=title]').focus(function () { 
				
		if ( $(this).val() == 'Naam van uw onderwerp' ) {
		
			$(this).val('');
			$(this).css('color', '#000');
		
		}
		
	}).blur(function () { 
	
		if ( $(this).val() == '' ) {
		
			$(this).css('color', '#AFAEAE');
			$(this).val('Naam van uw onderwerp');
		
		}
		
	});
	
	$('[name=content]').focus(function () { 
				
		if ( $(this).val() == 'Omschrijving van uw onderwerp' ) {
		
			$(this).val('');
			$(this).css('color', '#000');
		
		}
		
	}).blur(function () { 
	
		if ( $(this).val() == '' ) {
		
			$(this).css('color', '#AFAEAE');
			$(this).val('Omschrijving van uw onderwerp');
		
		}
		
	});
	
	$('[name=reaction]').focus(function () { 
				
		if ( $(this).val() == 'Typ hier uw reactie' ) {
		
			$(this).val('');
			$(this).css('color', '#000');
		
		}
		
	}).blur(function () { 
	
		if ( $(this).val() == '' ) {
		
			$(this).css('color', '#AFAEAE');
			$(this).val('Typ hier uw reactie');
		
		}
		
	});
		
		
	/**
	 * Login
	 */
	
	$('input.btn-login').click( function () {
	
		if ( $('[name=username]').length == 1 ) {
		
			$.post('/', { action: 'login', username: $('[name=username]').val(), password: $('[name=password]').val() }, function (response) {
		
				response = eval( '(' + response + ')' );
				
				if ( response.type == 'error' ) {
				
					$(document).showLoginMsg('error', response.msg);
				
				} else if ( response.type == 'success' ) {
				
					//$(document).showLoginMsg('success', response.msg);
					
					window.location = document.URL;
				
				}
			
			});
		
		}
	
	});
	
	/**
	 * Fancybox
	 */
	
	if ( $('.list-thumbs').length == 1 && $('.list-thumbs').hasClass('images') ) {
	
		$('.list-thumbs li a').fancybox( { hideOnContentClick: false } );
	
	}
	
	initCarousel();
	
	initForms();
	
	var tweet = getTwitters('tweet', { 
	  id: 'marketinglwd', 
	  count: 1, 
	  enableLinks: true, 
	  ignoreReplies: true, 
	  clearContents: true,
	  template: '"%text%" <a href="http://twitter.com/marketinglwd/statuses/%id%/">%time%</a>'
	});
	
	//$('#twitter').html(tweet);
	
	//alert(tweet);
	
	
});