$(function() {
	NavSecondary.init();
	Thumbs.init();
	Overlay.init();
});

var NavSecondary = { 
	Api: null,
	Next: null,
	Prev: null,
	Pages: null,
	PageNo: null,
	init: function() {
		var cc = this;
		cc.Size = cc.getSize();
		cc.Prev = $('#NavSecondary li a.prev');
		cc.Next = $('#NavSecondary li a.next');
		cc.PageNo = $('#NavSecondary li.page-no');
		cc.Api = $("div#Content div.scrollable").scrollable({
			size: cc.Size,
			loop: false,
			api: true,
			clickable: false,
			easing: 'easeInOutExpo',
			speed: 600,
			keyboard: false,
			onStart: function() {
				if(this.getPageIndex()==(cc.Pages-1)) {
					cc.Next.addClass('disabled');
				}
				else {
					cc.Next.removeClass('disabled');
				}
				if(this.getPageIndex()==0) {
					cc.Prev.addClass('disabled');
				}
				else {
					cc.Prev.removeClass('disabled');
				}
				cc.setPage();
			}
		}); 
		cc.Prev.addClass('disabled');
		cc.Pages = cc.Api.getPageAmount();
		cc.setPage();
		cc.events();
	},
	events: function() {
		var cc = this;
		cc.Next.click(function() {
			cc.Api.nextPage();
			return false;
		});
		cc.Prev.click(function() {
			cc.Api.prevPage();
			return false;
		});
		$(window).resize(function() {
			var size = cc.getSize();
			if(size!=cc.Size) {
				cc.Api.getConf().size = size; 
				cc.Api.reload(); 
				cc.Pages = cc.Api.getPageAmount();
				cc.setPage();
				cc.Size = size;
			}
		});
	},
	setPage: function() {
		var cc = this;
		cc.PageNo.html((cc.Api.getPageIndex()+1)+' <strong>of</strong> '+cc.Pages);
	},
	getSize: function() {
		var cc = this;
		var width = $(document).width();
		return Math.floor(width/325);;
	}
};

var Thumbs = { 
	Items: null,
	Container: null,
	init: function() {
		var cc = this;
		cc.Items = $('#Content div.scrollable div.items a.image');
		cc.Container = $('#Content div.scrollable div.items');
		cc.events();
	},
	events: function() {
		var cc = this;
		cc.Items.bind('mouseenter', function(){
			$(this).parent().stop().css('opacity', 1);;
			cc.Items.not($(this)).parent().stop().css('opacity', .2);
		});
		cc.Items.bind('mouseleave', function(){
			$(this).parent().css('opacity', 1);
		});
		cc.Container.bind('mouseleave', function(){
			cc.Items.parent().stop().fadeTo(600, 1);
		});
		
		cc.Items.click(function(){
			Overlay.show($(this).attr('href'));
			return false;
		});
	}
}



var Overlay = {
	Container: null,
	Items: null,
	Api: null,
	Tabs: null,
	init: function() {
		var cc = this;
		cc.Container = $('#Overlay');
	},
	show: function(href) {
		var cc = this;
		cc.Container.fadeIn('fast', function(){
			cc.Container.addClass('loading');

			cc.Container.load(href, function() {
				cc.Items = cc.Container.find('div.images div.item');
				cc.Items.hide();
				cc.TabNo = $('#Overlay li.tab-no');
				cc.Prev = $('#Overlay li a.prev');
				cc.Next = $('#Overlay li a.next');
				
				if($('#Overlay ul.tabs').length>0){
					
					cc.Api = $("#Overlay ul.tabs").tabs("div.images > div.item", {
						effect: 'fade',
						api: true,
						onClick: function() {
							if(this.getIndex()==(cc.Tabs-1)) {
								cc.Next.addClass('disabled');
							}
							else {
								cc.Next.removeClass('disabled');
							}
							if(this.getIndex()==0) {
								cc.Prev.addClass('disabled');
							}
							else {
								cc.Prev.removeClass('disabled');
							}
							cc.setTab();
						}
					});

					cc.Tabs = cc.Api.getPanes().length;
					cc.resize();
					cc.Prev.addClass('disabled');
					cc.events();
					cc.setTab();

					$(window).resize(function(){
						cc.resize();
					});
					
				}
				
				$('#Overlay a.close').click(function(){
					cc.hide();
					return false;
				});
			})
	
		});	
	
	},
	hide: function() {
		var cc = this;
		cc.Container.hide();
		cc.Container.html('');
	},
	events: function() {
		var cc = this;
		cc.Next.click(function() {
			if(cc.Api.getIndex()!=(cc.Tabs-1)) {
				cc.Api.next();
			}	
			return false;
		});
		cc.Prev.click(function() {
			if(!cc.Api.getIndex()==0) {
				cc.Api.prev();
			}
			
			return false;
		});
		cc.Items.click(function() {
			if((cc.Api.getIndex()+1)==cc.Tabs) {
				cc.hide();
			}
			else {
				cc.Api.next();
			}
			return false;
		});
	},
	resize: function() {
		var cc = this;
		var imageWidth = 1680;
		var imageHeight = 1050;
		var navWidth = $(window).width();
		var navHeight = $(window).height();
		var navRatio = navWidth / navHeight;
		var imageRatio = imageWidth / imageHeight;
		if (navRatio > imageRatio) {
			var newHeight = (navWidth / imageWidth) * imageHeight;
			var newWidth = navWidth;
		} else {
			var newHeight = navHeight;
			var newWidth = (navHeight / imageHeight) * imageWidth;
		}	
		newTop = 0 - ((newHeight - navHeight) / 2);
		newLeft =  0 - ((newWidth - navWidth) / 2);
		cc.Items.css({height: navHeight, width: navWidth});
		cc.Items.find('img').css({height: newHeight, width: newWidth, top: newTop, left: newLeft});
	},
	setTab: function() {
		var cc = this;
		cc.TabNo.html((cc.Api.getIndex()+1)+' <strong>of</strong> '+cc.Tabs);
	}
}
