var primaryNav = {
	init: function() {
		jQuery('div.block').prepend('<div class="block-top"></div>');
		jQuery('div.block').append('<div class="block-bottom"></div>');

		jQuery('ul.nav>li>ul').hide();	
		jQuery("ul.nav > li").hover(
			function () {
				jQuery(this).find('ul:hidden').slideDown("fast");
			}, 
			function () {
				jQuery(this).find('ul:visible').slideUp("fast");
			}
		);

		jQuery('.item p').hide();	
		jQuery(".item").hover(
			function () {
				jQuery(this).addClass('hover');
				jQuery(this).find('p:hidden').show();
			}, 
			function () {
				jQuery(this).removeClass('hover');
				jQuery(this).find('p:visible').hide();
			}
		);		
	}
};

var landingPage = {
	itemCount: '',
	currentKey: '1',
	init: function() {
		this.itemCount = jQuery('#landingPage .thumb').length;
		if(this.itemCount > 0) {
			this.initEvents();
		} else {
			return false;
		}
	},
	initEvents: function() {
		jQuery('#imagePaging a#nextImageButton').click(function(){landingPage.nextImage(); return false;});
		jQuery('#landingPage .thumb img').each(function (i) {
			var thumb = $(this);
			if(i == 0) {
				landingPage.update(landingPage.currentKey);
			}
			thumb.click(function(){
				var key = thumb.attr('rel');
				landingPage.update(key);
			});
		});
	},
	update: function(key) {
		jQuery('#landingPage .thumb').each(function (j) {
			if(j+1 == key) {
				$(this).addClass('active');
			} else {$(this).removeClass('active');}
		});
		this.currentKey = key;
		this.updateLargeImage(key);
		this.updatePagination(key);
		this.updatePhotoCaption(key);
		this.updateText(key);
	},
	nextImage: function() {
		var key = this.currentKey < this.itemCount ? Number(this.currentKey) + 1 : 1;
		landingPage.update(key);
	},
	updateText: function(key) {
		var newText = jQuery('#lpHiddenText-'+key).html();
		jQuery('#lpText').html(newText);

	},
	updateLargeImage: function(key) {
		var newSrc = jQuery('#lpHiddenLargeImage-'+key).attr('value');
		var newHref = jQuery('#lpHiddenUrl-'+key).attr('value');
		jQuery('#lpPhotoImage').attr('src',newSrc);
		jQuery('#lpPhotoImage').removeAttr('width');
		jQuery('#lpPhotoImage').removeAttr('height');
		jQuery('#lpPhotoImageLink').attr('href',newHref);
	},
	updatePhotoCaption: function(key) {
		var newText = jQuery('#lpHiddenCaption-'+key).attr('value');
		jQuery('#lpCaption').html(newText);
	},
	updatePagination: function(key) {
		jQuery('#imagePaging span').text(this.currentKey + ' / ' + this.itemCount);
	}
};

var mapPage = {
	init: function() {
		if($('#mapImage')) {
			this.prepLinks();
			this.initEvents();
		}
	},
	prepLinks: function() {
		$('.mapLinks a').each(function(i){
			$(this).find('span').addClass('hideText');
		});
	},
	initEvents: function() {
		$('#mapControl li').each(function(i) {
			$(this).click(function() {
				mapPage.switchMap($(this).attr('rel'));
			});
		});
	},
	switchMap: function(key) {
		$('#mapImage').attr('src', $('#mapImage-'+key).attr('value'));
		$('#mapLinks').html($('#mapLinks-'+key).html());
	}
};

jQuery(document).ready(function(){
	landingPage.init();
	primaryNav.init();
	mapPage.init();
});