var Gallery = {
	total: 0,
	current: 0,
	fadeSpeed: 1000,
	clickable: true,
	autoplay: true,
	interval: null,
	timeOut: 5000,
	countHolder: null,
	hasCaption: false,
	
	init: function(){
	
		this._gallery = $('.gallery');
		
		if( this._gallery.length === 1 )
		{
			this._items = this._gallery.find('.item');
			this._nav   = this._gallery.find('.nav');
			this.total  = this._items.length;
			this.countHolder = this._gallery.find('#gallery_count');
			if( this.total > 1 )
			{
				this.bindEvents();
				this.updateCount();
				this.collectCaptions();
				var _this = this;
				if(this.autoplay == true){
					this.interval = setInterval(function() { _this.next(); }, _this.timeOut);
				}	
			}
			else
			{
				this._nav.hide();
			}
		}
	},
	
	bindEvents: function(){
		this._gallery.on('click', '.arrow', function(){
			clearInterval(Gallery.interval);
			var _this = $(this);
			
			if( Gallery.clickable )
			{
				Gallery.clickable = false;
				( _this.hasClass('left') ) ? Gallery.prev() : Gallery.next() ;
			}
		});
	},
	
	updateCount: function(){
		this.countHolder.text(this.current + 1+' of '+this.total);
	},
	
	prev: function(){
		if( Gallery.current === 0 )
		{
			Gallery._items.last().fadeIn(Gallery.fadeSpeed, function(){
				Gallery.clickable = true;
			});
			
			if( Gallery.hasCaption )
			{
				Gallery._imageCaptions.filter('.current').removeClass('current').fadeOut((Gallery.fadeSpeed/2),function(){
					Gallery._imageCaptions.last().addClass('current').fadeIn((Gallery.fadeSpeed/2));
				});
			}
			
			Gallery.current = Gallery.total-1;
		}
		else
		{
			Gallery._items
				.eq((Gallery.current-1))
				.show()
				.next()
				.fadeOut(Gallery.fadeSpeed, function(){
					Gallery.clickable = true;
				});
				
			if( Gallery.hasCaption )
			{
				var _nextCaption = Gallery._imageCaptions.eq(Gallery.current-1);
				Gallery._imageCaptions.filter('.current').removeClass('current').fadeOut((Gallery.fadeSpeed/2),function(){
					_nextCaption.addClass('current').fadeIn((Gallery.fadeSpeed/2));
				});
			}
				
			Gallery.current--;
		}
		this.updateCount();
	},
	
	next: function(){
		if( Gallery.current + 1 < Gallery.total )
		{
			Gallery._items.eq(Gallery.current+1).fadeIn(Gallery.fadeSpeed, function(){
				Gallery.clickable = true;
				$(this).prev().hide();
			});
			
			if( Gallery.hasCaption )
			{
				var _nextCaption = Gallery._imageCaptions.eq(Gallery.current+1);
				Gallery._imageCaptions.filter('.current').removeClass('current').fadeOut((Gallery.fadeSpeed/2),function(){
					_nextCaption.addClass('current').fadeIn((Gallery.fadeSpeed/2));
				});
			}
			
			Gallery.current++;
		}
		else
		{
			Gallery._items.first().show();
			Gallery._items.eq(Gallery.current).fadeOut(Gallery.fadeSpeed, function(){
				Gallery.clickable = true;
				$(this).prev().hide();
			});
			
			if( Gallery.hasCaption )
			{
				Gallery._imageCaptions.filter('.current').removeClass('current').fadeOut((Gallery.fadeSpeed/2),function(){
					Gallery._imageCaptions.first().addClass('current').fadeIn((Gallery.fadeSpeed/2));
				});
			}
			
			Gallery.current = 0;
		}
		this.updateCount();
	},
	
	collectCaptions: function(){
		Gallery._captionWrap = $('.img_caption');
		if( Gallery._captionWrap.length === 1 )
		{
			Gallery.hasCaption = true;
			
			Gallery._items.each(function(i){
				var _item = $(this),
					_caption = _item.find('.caption_item');

				Gallery._captionWrap.append(_caption);
			});
			
			Gallery._imageCaptions = Gallery._captionWrap.children();
			Gallery._imageCaptions.hide().first().addClass('current').show();
		}
	}
};

function initialize() {
	var latlng = new google.maps.LatLng(-27.524412, 153.008183);
	var settings = {
		zoom: 13,
		center: latlng,
		mapTypeControl: false,
		mapTypeControlOptions: {style: google.maps.MapTypeControlStyle.DROPDOWN_MENU},
		navigationControl: true,
		navigationControlOptions: {style: google.maps.NavigationControlStyle.SMALL},
		mapTypeId: google.maps.MapTypeId.TERRAIN
	};

	var map = new google.maps.Map(document.getElementById("gmap"), settings);
	var companyPos = new google.maps.LatLng(-27.524412, 153.008183);
	
	var companyLogo = new google.maps.MarkerImage('/templates/mirvac_dev_tennysonreach2/img/map_marker.png',
		new google.maps.Size(136,49),
		new google.maps.Point(0,0),
		new google.maps.Point(120,49)
	);

	var companyMarker = new google.maps.Marker({ position: companyPos, map: map, icon: companyLogo, title:"Farringford by Tennyson Reach" });
}

$(function(){

	Gallery.init();
	
	//init gmap if map element exists. 
	if( $("#gmap").length ) {
		initialize();
	}
	
	// Clear default search text	
	$('#search_input').focus(function(){
		var _this = $(this);
		if( _this.val() === "Search Site" ){ _this.val(''); }
	}).blur(function(){
		var _this = $(this);
		if( _this.val() === "" ) { _this.val('Search Site'); }
	});
	
	//Fancy Form Bind
	$('input[type="checkbox"]').ezMark();
	$('input[type="radio"]').ezMark();
	
	//Form Validation
	$('#registration_form').attr('action', $('#form_action').text());
	$("#registration_form").validate({
		
		errorElement: "em",
		
		highlight: function(element, errorClass, validClass) {
			$(element).addClass(errorClass).removeClass(validClass);
			$(element.form).find("label[for=" + element.id + "]").addClass(errorClass);
		},

		unhighlight: function(element, errorClass, validClass) {
			$(element).removeClass(errorClass).addClass(validClass);
			$(element.form).find("label[for=" + element.id + "]").removeClass(errorClass);
		}
	});
	
});
