var timer;

$(function() {	
	if ($('#map').length) {
		$('#map').jmap('init', {'mapType':G_PHYSICAL_MAP,'mapCenter':[50.717490, -1.878596], 'mapZoom':15});
	
		$('#map').jmap('AddMarker', {
			'pointLatLng': [50.717490, -1.878596]
		});
	}

	if ($('#taken_date').length) {
		$('#taken_date').datepicker({
			inline: true,
			maxDate: '0',
			dateFormat: 'dd/mm/yy'
		});
	}
	
	if ($('a[rel="fancybox"]').length) {
		$('a[rel="fancybox"]').fancybox({ 
						'overlayShow' 	: false,
						'zoomSpeedIn' 	: 600,
						'zoomSpeedOut' 	: 500,
						'easingIn' 		: 'easeOutBack',
						'easingOut' 	: 'easeInBack'}); 
	}
	
	$(".grid_4 .photolist li").imagefit();
	
	$(".grid_4 .photolist li").each(function() {
		$(this).css({"position" : "relative"});
		
		$("img", this).css({
						   "position"	:	"absolute",
						   "top"		:	-((parseInt($("img", this).height()) / 2) - (parseInt($(this).height()) / 2)),
						   "left"		:	0
						   });
	});
	
	$("#banners ul").gcFader();
	
	$("#loyalty a").attr("href", "#loyaltyform");
	
	$("#loyalty a")
	.fancybox({ 
		'overlayShow' 		: false,
		'zoomSpeedIn' 		: 600,
		'zoomSpeedOut' 		: 500,
		'easingIn' 			: 'easeOutBack',
		'easingOut' 		: 'easeInBack',
		'hideOnContentClick': false,
		'frameHeight'		: 540
		});
	
});

/*

	Code derived from here - http://net.tutsplus.com/videos/screencasts/how-to-build-a-simple-content-slider-jquery-plugin/
	
	Why reinvent the wheel

*/
(function($) {
	
	$.fn.gcFader = function(options) {
		
		// set default options
		var defaults = {
			speed : 1200,
			pause : 3000,
			transition : 'fade'
		},
		
		options = $.extend(defaults, options);
			
		return this.each(function() {
			$this = $(this);
								  
			$("li", this).each(function () {
				$(this).css({
					backgroundPosition : ((parseInt($("#wrapper").width()) / 2) - (1400 / 2))+'px 0'
				});
			});	  
			
			for(var i = $this.children().length, y = 0; i > 0; i--, y++) { 		
					$this.children().eq(y).css('zIndex', i + 99999);
				}
		
		 	fade();
		
			function fade() {
				setInterval(function() {
					$this.children(':first').animate({'opacity' : 0}, options.speed, function() {	
						$this
						   .children(':first')
						   .css('opacity', 1) // Return opacity back to 1 for next time.
						   .css('zIndex', $this.children(':last').css('zIndex') - 1) // Reduces zIndex by 1 so that it's no longer on top.					
						   .appendTo($this); // move it to the end of the line.
					})
				}, options.pause);
			} // end fade	
							  
		}); // end each		
	
	} // End plugin. Go eat cake.
	
})(jQuery);


/* jquery.imagefit 
 *
 * Version 0.2 by Oliver Boermans <http://www.ollicle.com/eg/jquery/imagefit/>
 *
 * Extends jQuery <http://jquery.com>
 *
 */
(function($) {
	$.fn.imagefit = function(options) {
		var fit = {
			all : function(imgs){
				imgs.each(function(){
					fit.one(this);
					})
				},
			one : function(img){
				$(img)
					.width('100%').each(function()
					{
						$(this).height(Math.round(
							$(this).attr('startheight')*($(this).width()/$(this).attr('startwidth')))
						);
					})
				}
		};
		
		this.each(function(){
				var container = this;
				
				// store list of contained images (excluding those in tables)
				var imgs = $('img', container).not($("table img"));
				
				// store initial dimensions on each image 
				imgs.each(function(){
					$(this).attr('startwidth', $(this).width())
						.attr('startheight', $(this).height())
						.css('max-width', $(this).attr('startwidth')+"px");
				
					fit.one(this);
				});
				// Re-adjust when window width is changed
				$(window).bind('resize', function(){
					fit.all(imgs);
				});
			});
		return this;
	};
})(jQuery);
