//---------------------------------------------------------
//
//  General javascript file for Fratello Demo website
//
//  Authors:	Davy De Pauw (davy@marlon.be)
//				Gerrit Bertier (gerrit@marlon.be)
//
//---------------------------------------------------------

/**
 * On DOM ready
 */
$(document).ready(function()
{
	// General javascript interaction
	general.init();
});

/**
 * General JS functions
 */
var general =
{
	/**
	 * Initialize
	 */
	init: function()
	{
		// Set hoverables
		this.hoverable();

		// Set indicator for required fields
		this.setRequiredIndicators();
		
		// Lay-out changes
		layout.init();
		
		// Form on page?
		//if($('.form').length > 0) forms.init();
		
		if($('.widget table').length > 0) tables.init();

		// Header visual
		if($('#visual-head a').length > 0) this.hoverableVisual();

		// Onze tv form
		onzetv.init();

	},
	
	/**
	 * Set hoverables.
	 */
	hoverable: function()
	{
		var hoverIn = function() { $(this).addClass('hover'); };
		var hoverOut = function() { $(this).removeClass('hover'); }
				
		// Generic hoverable function
		if($('ul.hoverable').length > 0) $('.hoverable li').hover(hoverIn, hoverOut);
	},
	
	
	hoverableVisual: function()
	{
		$('#visual-head a').hover(
			function() {
				general.changeHover($(this))
			},
			function() {
				general.changeHover($(this))				
			}
		);
	},
	
	changeHover: function(target)
	{
		var src = $(target).find('img').attr('src');
		// Set hover image
		$(target).find('img').attr('src', $(target).attr('rel'));
		// Save old src in rel
		$(target).attr('rel', src);		
	},
	
	/**
	 * setRequiredIndicators adds * to elements which have a class="required"
	 */
	
	setRequiredIndicators: function()
	{
		$('.required label').append('<span class="indicator">*</span>');
	}
}

/**
 * Layout specific functions
 */
var layout =
{
	/**
	 * Initialize
	 */
	init: function()
	{				
		// Set min height
		this.setMinHeight();
	},
		
	/**
	 * set minimum content height
	 */
	setMinHeight: function()
	{
		if($('#nav').length > 0) {
			$('#main').css('min-height', ($('#nav').outerHeight() + 5) + 'px');
		}
	}
}

var tables = {
	init: function()
	{
		$('.widget table:not(.layout) tr:even').addClass('even');
		$('.widget table:not(.layout) td:first').addClass('first');
		$('.widget table:not(.layout) th:first').addClass('first');
	}
}

var onzetv = {
	init: function()
	{
		$('#onzetv a.button').click(function(e) {
			$(this).hide();
			$('#onzetv form').show();
			$('#onzetv .btn-send').attr('disabled', '');

			if ($('#onzetv input.field').length > 0) {
				$('#onzetv input.field').focus(function() {
					if ($(this).val() == $(this).attr('title')) {
						$(this).val('');
					}
				});

				$('#onzetv input.field').blur(function() {
					if ($(this).val() == '') {
						$(this).val($(this).attr('title'));
					}
				});
			}

			$('#onzetv .btn-send').click(function(e) {
				$(this).attr('disabled', 'disabled');
				$('#onzetv form').submit();

				e.preventDefault();
			});

			$('#onzetv form').submit(function(e) {
				// Get numbers
				$.post('/ajax/postalcode', { postcode: $('#postalcode').val() }, function(data) {
					$('#onzetv form fieldset').hide();
					$('#onzetv .feedback').html(data).show();

					if($('#onzetv a.retry').length > 0)
					{
						$('#onzetv a.retry').click(function(e) {
							onzetv.resetFeedback();

							e.preventDefault();
						});
					}

					if($('#onzetv a.thanks').length > 0)
					{
						$('#onzetv a.thanks').click(function(e) {
							onzetv.resetFeedback();

							e.preventDefault();
						});
					}
				});

				e.preventDefault();
			});

			e.preventDefault();
		});



	},

	resetFeedback: function() {
		$('#onzetv .feedback').hide();
		$('#onzetv fieldset').show();
		$('#postalcode').val('gemeente');

		$('#onzetv .btn-send').attr('disabled', '');
	}
}