/**
 * Created by LoungeRoom, www.loungeroom.nl
 *
 * Global Javascript functions, like document.ready functions and binding links.
 *
 * @author Terry Duivesteijn <terry@duivesteijn.com>
 * @copyright Copyright (c) 2011, LoungeRoom, Voorschoten
 * @license http://opensource.org/licenses/gpl-3.0.html GNU General Public License, version 3 (GPLv3)
 * @version $Id: v 1.0 2011-02-03 18:38
 */

$(document).ready(function(){

	$(document).pngFix();
	binds();

	/**
	 * Styling of placeholders is not common yet.
	 * @author Terry Duivesteijn <terry@duivesteijn.com>
	 */
	$("input[type=text]").each(function() {
		$(this).attr("value",$(this).attr("placeholder"));
	});


});

/**
 * Binds elements to a function.
 * @author Terry Duivesteijn <terry@duivesteijn.com>
 * @category onload
 * @return void
 **/
function binds(){
	/**
	 * Styling of placeholders is not common yet.
	 * @author Terry Duivesteijn <terry@duivesteijn.com>
	 * Turn back on by binding only when not available:
	 * if(!available('placeholder','input'))
	 */
	$("input").bind('focus blur',placeholder);

	$("input.set_password_user").bind('focus click',PassLabelHideOnInput);
	$("input.set_password").bind('focus click',PassLabelHide);

	$("a.bind").removeClass('bind').bind('click',action);
	$("a[rel^='prettyPhoto']").prettyPhoto();
	$("#contactform.bind").removeClass('bind').bind('submit',contactform);
	$("#boekingform.bind").removeClass('bind').bind('submit',boekingform);
}

/*
 * Container for click actions
 * @author Terry Duivesteijn <terry@duivesteijn.com>
 */
function action(e){
	e.preventDefault();
	if($(this).hasClass('menu')) {
		// clicked on a menu item
		$("nav ul li a.menu").removeClass('open');
		$(this).addClass('open');
		$("section article").remove();
		$.fancybox({
			autoDimensions: false,
			scrolling: false,
			width:600,
			height:463,
			enableEscapeButton:false,
			overlayShow: false,
			href: BASE_URL+'ajax/gateway.php?page='+$(this).attr('rel')
		});

	} else if($(this).hasClass('news')) {
		// clicked on a news item
		$("section article").remove();
		$.fancybox({
			autoDimensions: false,
			scrolling: false,
			width:600,
			height:463,
			enableEscapeButton:false,
			overlayShow: false,
			href: BASE_URL+'ajax/gateway.php?news='+$(this).attr('rel')
		});
	} else if($(this).hasClass('agenda')) {
		// clicked on a agenda item
		$("section article").remove();
		$.fancybox({
			autoDimensions: false,
			scrolling: false,
			width:600,
			height:463,
			enableEscapeButton:false,
			overlayShow: false,
			href: BASE_URL+'ajax/gateway.php?agenda='+$(this).attr('rel')
		});
	}
}

/*
 * Switches input from text to password
 * @author Terry Duivesteijn <terry@duivesteijn.com>
 */
function PassLabelHideOnInput(){
	$(this).removeClass('set_password_user');
	$("input.set_password").attr('autocomplete','on').prev().hide();
}
function PassLabelHide(){
	$(this).removeClass('set_password').attr('autocomplete','on').prev().hide();
}

/**
 * Delete default value when focussed on input. Restore value on blur.
 * @html5 Fallback for HTML5-placeholder
 * @author Terry Duivesteijn <terry@duivesteijn.com>
 * @category functionality
 * @return void
 **/
function placeholder(event){
	if(event.type == 'focus') {
		if($(this).attr('value') == $(this).attr('placeholder')) {
			$(this).attr('value','');
		}
	} else if(event.type == 'blur') {
		if($(this).attr('value') == '') {
			$(this).attr('value',$(this).attr('placeholder'));
		}
	}
}

/**
 * Check for available attribute (HTML5)
 * @author Terry Duivesteijn <terry@duivesteijn.com>
 * @category core
 * @return bool
 **/
function available(attribute, element){
	if ((attribute in document.createElement(element))) {
		return true;
	}
	return false;
}

/**
 * Enable debuggin in Firebug for example.
 * @author Terry Duivesteijn <terry@duivesteijn.com>
 * @category core
 */
function log() {
	if (window.console && window.console.log)
		window.console.log('debug: ' + Array.prototype.join.call(arguments,' '));
}

/*
 * Send contactform
 * @author Terry Duivesteijn <terry@duivesteijn.com>
 */
function contactform(e){
	e.preventDefault();
	var contact = {
				Name: $("#form_Name").val(),
				CompanyName: $("#form_CompanyName").val(),
				Telephone: $("#form_Telephone").val(),
				Email: $("#form_Email").val(),
				Message: $("#form_Message").val()
		};
	$.post(BASE_URL+'ajax/gateway.php?page=contact&type=json', {
				contact: contact
	}, function(result){
		$("section article").remove();
		$.fancybox({
			autoDimensions: false,
			scrolling: false,
			width:600,
			height:463,
			overlayShow: false,
			content: result.html

		});

	});
	return false;
}

/*
 * Send boekingform
 * @author Terry Duivesteijn <terry@duivesteijn.com>
 */
function boekingform(e){
	e.preventDefault();
	var boeking = {
				Name: $("#form_Name").val(),
				CompanyName: $("#form_CompanyName").val(),
				Address: $("#form_Address").val(),
				Zip: $("#form_Zip").val(),
				City: $("#form_City").val(),
				Telephone: $("#form_Telephone").val(),
				Email: $("#form_Email").val(),
				TypeOfPerformance: $("#form_TypeOfPerformance").val(),
				Budget: $("#form_Budget").val(),
				DateOfPerformance: $("#form_DateOfPerformance").val(),
				Remarks: $("#form_Remarks").val()
		};
	$.post(BASE_URL+'ajax/gateway.php?page=boekingen&type=json', {
				boeking: boeking
	}, function(result){
		$("section article").remove();
		$.fancybox({
			autoDimensions: false,
			scrolling: false,
			width:600,
			height:463,
			overlayShow: false,
			content: result.html

		});

	});
	return false;
}

