var toggleObject = function(toggle){
	switch(toggle){
		case "open":
			$('p.privacy_policy').show();
			break;
		
		case "close":
			$('p.privacy_policy').hide();
			break
	}
};

// GALLERY JS

var galleryfeed = {

	_images : [],
	_titles : [],
	_descriptions : [],
	
	getGalleryData : function(dataUrl){
		$.ajax({
			url: dataUrl,
			cache: false,
			type: "GET",
			processData: false,
			success: function(data){
				var galleryArr = $(data).find('#gallery_images .gallery_item');
				galleryfeed.createArrays(galleryArr);
			},
			error : function(){
				alert("Epic fail. Ajax request denied");
			}
		});
	},
	
	resetArrays : function(){
		galleryfeed._images = [];
		galleryfeed._titles = [];
		galleryfeed._descriptions = [];
	},
	
	createArrays : function(gallery_items){
		
		this.resetArrays();
		
		$(gallery_items).each(function(){
			galleryfeed._images.push($(this).find('img').attr('src'));
			galleryfeed._titles.push($(this).find('span.title').text());
			galleryfeed._descriptions.push($(this).find('p').text());
		});
		
		this.openLightbox();
	},
	
	openLightbox : function(){
		var gimages = ["/templates/justicerd-farm/images/gallery/justice_road_01.jpg", "/templates/justicerd-farm/images/gallery/justice_road_03.jpg", "/templates/justicerd-farm/images/gallery/justice_road_04.jpg", "/templates/justicerd-farm/images/gallery/justice_road_07.jpg", "/templates/justicerd-farm/images/gallery/justice_road_06.jpg", "/templates/justicerd-farm/images/gallery/justice_road_05.jpg", "/templates/justicerd-farm/images/gallery/justice_road_08.jpg", "/templates/justicerd-farm/images/gallery/justice_road_09.jpg", "/templates/justicerd-farm/images/gallery/justice_road_02.jpg"];

		var gtitles = ["", "", "", "", "", "", "", "", ""];

		var gdescriptions = ["Westernport Bay and Mornington Peninsula views available from some lots at Justice Road Farm", "Perfectly located 600m from beautiful (and dog-friendly) Red Rocks Beach", "Perfectly located 600m from Red Rocks Beach", "Aerial image of Justice Road farm identifies the sites relationship to Phillip Island's rural landscape", "Only 4 minutes drive to main street Cowes", "Positioned alongside Phillip Island's rural landscape", "The Farm will remain operable as the development progresses", "Established eucalypts located on lots in first release", "The natural habitat of Justice Road Farm will be preserved for locals to enjoy"];
		
		$.prettyPhoto.open(gimages,gtitles,gdescriptions);
		
		return false;
	}
};

var initGallery = function(){
	var dataUrl = "/gallery.html";
	//galleryfeed.getGalleryData(dataUrl);
	galleryfeed.openLightbox();
	return false;
};

// MEDIA JS - duplicate of Gallery Feed for new section

var mediafeed = {

	_images : [],
	_titles : [],
	_descriptions : [],
	
	getMediaData : function(dataUrl){
		$.ajax({
			url: dataUrl,
			cache: false,
			type: "GET",
			processData: false,
			success: function(data){
				var mediaArr = $(data).find('#media_images .media_item');
				mediafeed.createArrays(mediaArr);
			},
			error : function(){
				alert("Epic fail. Ajax request denied");
			}
		});
	},
	
	resetArrays : function(){
		mediafeed._images = [];
		mediafeed._titles = [];
		mediafeed._descriptions = [];
	},
	
	createArrays : function(media_items){
		
		this.resetArrays();
		
		$(media_items).each(function(){
			mediafeed._images.push($(this).find('img').attr('src'));
			mediafeed._titles.push($(this).find('span.title').text());
			mediafeed._descriptions.push($(this).find('p').text());
		});
		
		this.openLightbox();
	},
	
	openLightbox : function(){
		var mimages = ["/templates/justicerd-farm/images/media_library/media01.jpg", "/templates/justicerd-farm/images/media_library/media02.jpg", "/templates/justicerd-farm/images/media_library/media03.jpg","/templates/justicerd-farm/images/media_library/media04.jpg","/templates/justicerd-farm/images/media_library/media05.jpg","/templates/justicerd-farm/images/media_library/media06.jpg"];

		var mtitles = ["", "", "", "", "", ""];

		var mdescriptions = ["Press Advert - Jan 22 2011- The Age", "Press Advert - Jan 22 2011 - Gazette Real Estate, SERE-Berwick Cranbourne, Pakenham & Dandenong", "Press Advert - Labour day weekend 12-14th March 2011 - Gazette Real Estate, SERE-Berwick Cranbourne, Pakenham & Dandenong", "Press Advert - Labour day weekend 12-14th March 2011 - Herald Sun, LaTrobe Valley Express, Gazette Real Estate, SERE-Berwick Cranbourne, Pakenham & Dandenong & Morwell", "Press Advert - Easter weekend 23-26th April 2011 - Gazette Real Estate, SERE-Berwick Cranbourne, Pakenham & Dandenong", "Press Advert - Easter weekend 23-26th April 2011 - Herald Sun, LaTrobe Valley Express, Gazette Real Estate, SERE-Berwick Cranbourne, Pakenham & Dandenong & Morwell"];
		
		$.prettyPhoto.open(mimages,mtitles,mdescriptions);
		
		return false;
	}
};

var initMedia = function(){
	var dataUrl = "/media.html";
	//galleryfeed.getGalleryData(dataUrl);
	mediafeed.openLightbox();
	return false;
};


// DOM Ready
$(function(){
	
	// Lot plan & Detail
	$lotdetail = $('#lotdetail_wrap');
	$siteplan = $('#siteplan');
	
	$lothover = $('#siteplan_hover');
	$lotnum = $('#siteplan_hover > span.lot_no');
	$lotsqm = $('#siteplan_hover > span.lot_sqm');
	
	$('.hitpoint > a').bind('hover', { speed:500, delay:300 },
		function(){ 
			_this = this;
			$lotnum.text($(this).attr('rel'));
			$lotsqm.text($(this).attr('title'));
			$lothover.fadeIn('fast');
			
			if( $(this).hasClass('sold') == true ){ 
				$lothover.addClass('sold');
			}
			
		}).bind('hoverend',
		function(){ 
			$lothover.fadeOut('fast');
			
			$lothover.removeClass('sold');
			
	});

	$('.detail_link > a').bind('hover', { speed:500, delay:300 },
		function(){ 
			$lotnum.text($(this).attr('rel'));
			$lotsqm.text($(this).attr('title'));
			$lothover.fadeIn('fast');
			
		}).bind('hoverend',
		function(){ 
			$lothover.fadeOut('fast');
	});
	
	$('.hitpoint > a').click(function(){
		$lotdetail.show();
		$siteplan.hide();
	});
	
	$('.detail_link > a').toggle(
		function(){
			$lotdetail.show();
			$siteplan.hide();
			//$(this).text('View Site Plan');
		},
		function(){
			$lotdetail.hide();
			$siteplan.show();
			//$(this).text('View Stage Detail');
		}
	);
	
	$('a.view_detail').click(function(){
		$lotdetail.show();
		$siteplan.hide();
	});
	
	$('a.view_plan').click(function(){
		$lotdetail.hide();
		$siteplan.show();
	});
	
	// Location
	map2 = $('#map_wrap .map2'); // Local Map
	map1 = $('#map_wrap .map1'); // Arial Photo
	map3 = $('#map_wrap .map3'); // Google Map
	maps = $('#map_wrap .map1, #map_wrap .map2, #map_wrap .map3');
	
	$('.maplinks > a').click(function(){
		var map = $(this).attr('class');
		maps.hide();
		$('#map_wrap div.'+map).show();
		
	});
	
	// Gallery Lightbox Init
	$(".gallerylink a[rel^='prettyPhoto']").prettyPhoto({theme:'dark_square'});
	
	$('li.gallery a').click(function(){
		var dataUrl = "gallery.html";
		galleryfeed.openLightbox();
		return false;
	});
	
	// Media Lightbox Init
	$(".medialink a[rel^='prettyPhoto']").prettyPhoto({theme:'dark_square'});
	
	$('li.media a').click(function(){
		var dataUrl = "media.html";
		mediafeed.openLightbox();
		return false;
	});
	
	// Privacy toggler
	$('a.privacy_toggle').hover(
		function(){
			clearTimeout(timerItem);
			$('p.privacy_policy').slideDown();
		},
		function(){
			carousel.initTimeout("$('p.privacy_policy').slideUp();", "7000");
		}
	);

	$('p.privacy_policy').hover( function(){
		clearTimeout(timerItem);
	});
	
	$('form#register').validate({
		errorElement: "em",
		errorPlacement: function(error, element) {
     		error.appendTo( element.parent("div").children("div.error") );
		},

		submitHandler: function(form) { form.submit(); }
	});
	
});

