/**
 * Global JavaScript Function
 *
 * @website http://thebarnsomerset.co.uk
 * @author Neal Hulme Creative Ltd <nealhulme@gmail.com>
 * @version 1.0
 */

$(document).ready(function() {
	addGrid();
	initOverlay();
	initColorbox();
});

function addGrid() {
	if ( getQueryString('grid') ) {
		$('<div />').css({
			'background': 'url(/images/backgrounds/grid.png) repeat-y 50% 0',
			'height': '100%',
			'position': 'fixed',
			'width': '100%',
			'z-index': 999
		}).appendTo($('body'));
	}
}

function getQueryString(element) {
	strQuery = window.location.search.substring(1);
	arrElements = strQuery.split('&');
	for (var i = arrElements.length - 1; i >= 0; i--){
		arrPair = arrElements[i].split('=');
		if ( arrPair[0] == element ) return arrPair[1];
	};
	return false;
}

function initOverlay() {
	if (!$('body').hasClass('contact')) {
		var returnURL = window.location.pathname;
		var $myOverlay = $('<div id="contactOverlay" />');
		$myOverlay.load("http://thebarnsomerset.co.uk/contact-overlay", function() {
			$myOverlay
				.appendTo($('body'))
				.css('top', 5 - $myOverlay.outerHeight())
				.find('#toggle')
				.css('margin-top', '-100px')
				.toggle(function() {
					$myOverlay.animate({
						top: 0
					}, 500, function() {
						$myOverlay
							.find('#toggle')
							.html('Close')
							.css('background-image', 'url(/images/backgrounds/cross.png)');
					})
				},		
				function() {
					$myOverlay.animate({
						top: 5 - $myOverlay.outerHeight()
					}, 500, function() {
						$myOverlay
							.find('#toggle')
							.html('Get in touch')
							.css('background-image', 'url(/images/backgrounds/circle.png)');
					})
				})
				.animate({
					'margin-top': 0
				}, 2000)
				.end()
				.find('input[name="return"]')
				.attr('value', returnURL);
		})
	}
}

function initColorbox() {
	$('.colorbox').colorbox();
}
