/* ------------------------------------------------- */
/* JQuery functions
/* ------------------------------------------------- */
var home = {
	
	articles: $('section article'),
	quickLinks: $('section li'),
	
	init: function() {
		var fn = this;
		fn.events();
	},
	events: function() {
		fn = this;
		fn.articles.each(function() {
			if($('a', $(this)).length) {
				$(this).css({'cursor':'pointer'});
				$(this).click(function() {
					var href = $('a', $(this)).attr('href');
					window.location = href;
				});
			}
		})
		fn.quickLinks.click(function() {
			var href = $('a', $(this)).attr('href');
			window.location = href;
		});
	}
}

var plan = {
	
	maps: $('#m-plattegrond area'),
	tooltips: $('div.tooltip-content'),
	sliders: $('div.photo-slider'),
	
	init: function() {
		var fn = this;
        fn.sliders.hide();
		fn.events();
		fn.maps.tooltip({track: true, delay: 0, showURL: false});
	},
	
	events: function() {
		var fn = this;
		fn.maps.click(function(e) {
			e.preventDefault();
			// Hide all tooltips
			fn.tooltips.hide();
			fn.sliders.hide();
			// Get id
			var id = $(this).attr('href');
			// Show selected tooltip/photo-slider
			$(id).show();
			$(id + '-slider').show();
			if($(id + '-slider div.item').length > 1) {
				$(id + '-slider div.item').scrollable();
			} else {
				$('a.next', $(id + '-slider div.item').parent()).hide();
				$('a.prev', $(id + '-slider div.item').parent()).hide();
			}
		});
	}
}

var news = {
	
	items: $('#news-items li a'),
	lastItem: $('#news-items li:last-child').not('.even'),
	
	init: function () {
		var fn = this;
		fn.items.parent().css('cursor','pointer');
		fn.lastItem.addClass('last');
		fn.events();
	},
	
	events: function() {
		var fn = this;
		fn.items.parent().click(function() {
			var href = $('a', $(this)).attr('href');
			window.location = href;
		});
	}
	
}

/* ------------------------------------------------- */
/* On document ready
/* ------------------------------------------------- */
jQuery(document).ready(function($) {
	if($('div.homepage').length) {
		// Backstretch full image
		// If we want dynamic pictures, put them in an hidden field with id backstretch-image
		if($('#backstretch-image').length) {
			var backstretchImage = '/uploads/cmspages/' + $('#backstretch-image').val();
		} else {
			var backstretchImage = '/themed/vierhoekhoeve/img/bg-homepage.jpg';
		}
		$.backstretch(backstretchImage);
		// Init home specific JS
		home.init();
	}
	// Init sf-menu
	$('.sf-menu').superfish({autoArrows:false, dropShadows:false});
	// Add last class to last element of navigation
	$('.sf-menu li:last-child').addClass('last');
	$('.sf-menu li:first-child').not('ul li ul li').addClass('first');
	// Init scrollable
	if($('#scrollable div.item').length > 1) {
		$('#scrollable').scrollable();
	} else {
		$('a.next', $('#scrollable').parent()).hide();
		$('a.prev', $('#scrollable').parent()).hide();
	}
	// Init Plattegrond
	if($('.plan-wrapper').length) plan.init();
	// Init validate
	if('#form-contact') $('#form-contact').validate();
	// Init validate
	if($('#news-items').length) news.init();
	// Mailinglist subscribe
	if($('#subscribe-form').length) {
		$("#subscribe-form").validate({
			submitHandler: function(form) {
				$(form).ajaxSubmit({
					target: "#feedback",
					success: function() {
						$('#feedback').fadeIn();
						// Reset form
						$('#subscribe-form').clearForm();
					} 
				});
			}
		});
	}
});

