// Check if the console object exists (which is does in Firefox and IE8) end create a dummy to make sure nothing fails on console calls.
if (typeof console == 'undefined') {
	var console = {
		info: function() {},
		dir: function() {},
		group: function() {},
		groupEnd: function() {}
	};
}

Q = {
	
	container : '.content',
	serviceFile : 'static/pages.php',
	serviceConfig : {
		pages	: { serviceUrl: 'static/pages.php' },
		contact	: { serviceUrl: 'static/contact.php' }
	},
	preinit : function() {
	//	$(Q.container).hide();
		this.init();
	},
	init : function() {
		this.vCard();
		this.addBindings();
	},
	addBindings : function() {
		$('#navigation a').bind('click', function(){
														$('.current').removeClass('current');
														$(this).parent().addClass('current');
														Q.loadPage($(this));
													//	alert(Q.pageID);
														return false;
													});
		
	},
	addEmailBinding : function() {
		$('.email a').bind('click', function(){
											   			Q.loadPage($(this));
													//	alert('here');
														return false;
													});
	},
	loadPage : function(el) {
		$(Q.container).hide();
		var pages = el.attr('title');
		
		$.ajax({
			method: 'get',
			url: Q.serviceFile,
			data: 'request=' + pages,
			beforeSend: function(){$('#loader').show('fast');},
			complete: function(){ $('#loader').hide('fast');},
			success: function(data){
				$(Q.container).fadeIn('slow');
				$(Q.container).html(data);
				Q.pageFunctions(data);
				Q.addEmailBinding();
				Q.newWindow();
				Q.formValidate();
				
			}
		});
	},
	pageFunctions : function(data) {
		
		// portfolio
		$('a[rel=portfolio]').fancybox({
									'titlePosition' 		: 'inside'
								});
		// Social
		$('.social a').hover(function () {
				$('img', this).stop().animate({'marginLeft' : 5}, 250);
			}, function() {
					$('img', this).stop().animate({'marginLeft' : 10}, 250);
		});
		
		// vcard
		
		
	},
	vCard : function() {
		$('.v_card').hover(function(){
			var xpos = $(this).offset().left;
			var ypos = $(this).offset().top;
			$('#pointer').fadeIn('slow').css({'left': xpos + 60, 'top': ypos + 10});
		}, function() {
			$('#pointer').css({left: '-999em', top: '0'});
		});
	},
	newWindow : function() {
		var oEl = $('a.open');
		
		oEl.each(function(){
			$(this).bind('click', function(){ window.open(this.href); return false; });
		});
	},
	formValidate : function(data) {
		var oForm = $('#contactForm');
		var msgBox = '#message';
		if(oForm.is(":visible")) {
			
			var ajaxConfig = {
				target		:	'msgBox',
				dataType	:	'json',
				success		:	function(data) {
					if(data.status == true) {
						
						$(msgBox).hide().html(data.message).fadeIn('fast', function(){
																						Q.redirect(data.url);
																					});
					} else {
						$(msgBox).hide().html(data.message).fadeIn('fast');
					}
				}
			}
			
			var validContact = oForm.validate({
				rules		: {
					naam : {
						required : true,
						minlength : 2
					},
					email: {
							required: true,
							email	: true
					},
					protectwebformcode : 'required',
					vraag : 'required'
					
				},
				messages	: {
					naam : {
						required	: 'Vul uw naam in.',
						minlength	: 'Een naam bestaan uit tenminste twee letters.'
					},
					email : { 
						required	: 'Vul uw e-mailadres in.',
						email		: 'Vul een geldig e-mailadres in.'
						
					},
					protectwebformcode : {
						required	: 'Vul de captcha code in.'
					},
					vraag : {
						required	: 'Vul uw vraag/opmerking in.'
					}
				},
				submitHandler : function(form) {
					$(form).ajaxSubmit(ajaxConfig);
				}
			});
		 }
	},
	redirect : function(url) {
		 	
		setTimeout(function(){ window.location = url; }, '3000');
	}
	
};


$(document).ready(function() {	
	Q.preinit();
});