/**
 *	Main JavaScript Control Object
 *	@author Carl Ogren
 */



var main = {
	
	page: null,	// Current page
	section: null, // Current section
	
	init: function() {
		// Retrieve current page
		this.page = $('#currentPage').val();
		
		// show loading msg when sending ajax requests
		$('#loading')
		.bind('ajaxSend', function() { $(this).fadeIn('fast'); })
		.bind('ajaxComplete', function() { $(this).fadeOut('fast'); });
		
		if (this[this.page] && this[this.page].init) {
			this[this.page].init();
		}
		
		// adjust content when navigator window changes size and once now
		$(window).resize(function() { main.adjust(); });
		this.adjust();
	},
	
	
	/**
	 *	Page controls
	 */
	main: {
		init: function() {
			main.menu.init();
		}
	},
	
	
	/**
	 *	Menu controls
	 */
	menu: {
		width: 0,
		
		init: function() {
			this.load();
		},
		
		load: function() {
			$.ajax({
				type: 'get',
				data: { act: 'printMenu', ajax: 1 },
				success: function(response) {
					if (/error~/.test(response)) {
						common.showMsg('error', response);
					}
					else {
						$('#menu').html(response);
						
						var width = 0;
						$('li', '#menu').each(function() {
							width += $(this).outerWidth(true);
						});
						width -= 40;
						main.menu.width = width;
						
						$('a', '#menu').click(function() {
							$(this).addClass('selected')
							.parent().siblings('li').children('a').removeClass('selected');
							
							var info = $(this).attr('rel');
							info = info.split('~');
							
							main.load(info[0], info[1], info[2]);
							
							return false;
						});
						
						$('a.firstLoad', '#menu').triggerHandler('click');
					}
				}
			});
		}
	},
	
	
	/**
	 *	Load section
	 */
	load: function(section, type, groupId) {
		$.ajax({
			type: 'get',
			data: {
				act: 'printSection',
				section: section,
				type: type,
				groupId: groupId
			},
			success: function(response) {
				if (/error~/.test(response)) {
					common.showMsg('error', response);
				}
				else {
					$('#sectionCtnr').html(response);
					
					main.section = section;
					
					switch (section) {
						case 'info':
						case 'links':
							$('#sectionCtnr').css('overflow', 'auto');
					}
					
					setTimeout(function() {
						if (main[section] && main[section].init) {
							main[section].init(type, groupId);
						}
					}, 100);
				}
			}
		});
	},
	
	
	
	
	/**
	 *	Section control
	 */
	viewer: {
		type: 'photo',
		groupId: null,
		photoId: null,
		videoId: null,
		
		init: function(type, groupId) {
			this.type = type;
			this.groupId = groupId;
			
			this.list.init();
		},
		
		list: {
			init: function() {
				if (main.viewer.type == 'photo') {
					//$('#photoListCtnr').width(main.menu.width);
					
					var width = 0;
					$('#photoListCtnr div.inner span.separation').each(function() {
						width += $(this).outerWidth(true);
					});
					
					$('#photoListCtnr div.inner a').each(function() {
						width += $(this).outerWidth(true);
						
						$(this).click(function() {
							var id = $(this).attr('rel');
							main.viewer.load(id);
							return false;
						});
					}).eq(0).triggerHandler('click');
					
					$('#photoListCtnr div.inner').width(width);
					
					$('#photoListCtnr').hoverScroll();
				}
				else if (main.viewer.type == 'video') {
					var width = 0;
					$('#videoListCtnr div.inner span.separation').each(function() {
						width += $(this).outerWidth(true);
					});
					
					$('#videoListCtnr div.inner a').each(function() {
						width += $(this).outerWidth(true);
						
						$(this).click(function() {
							var id = $(this).attr('rel');
							main.viewer.load(id);
							return false;
						});
					}).eq(0).triggerHandler('click');
					
					$('#videoListCtnr div.inner').width(width);
					
					$('#videoListCtnr').hoverScroll();
				}
			}
		},
		
		load: function(id, action) {
			var act;
			if (this.type == 'photo') {
				act = 'printPhoto';
				this.photoId = id;
			}
			else if (this.type == 'video') {
				act = 'printVideo';
				this.videoId = id;
			}
			
			$.ajax({
				type: 'get',
				data: {
					act: act,
					type: main.viewer.type,
					groupId: main.viewer.groupId,
					id: id,
					action: action
				},
				success: function(response) {
					if (/error~/.test(response)) {
						common.showMsg('error', response);
					}
					else {
						$('#viewer').html(response);
						
						if (main.viewer.type == 'photo') {
							main.viewer.photoId = $('#viewedPhoto').css({ opacity: 0 }).attr('rel');
							
							$('#viewer .controls a.prev').unbind('click').click(function() {
								main.viewer.load(main.viewer.photoId, 'prev');
								return false;
							});
							
							$('#viewer .controls a.next').unbind('click').click(function() {
								main.viewer.load(main.viewer.photoId, 'next');
								return false;
							});
							
							setTimeout(function() {
								main.adjust();
								$('#viewedPhoto').css('opacity', 100);
							}, 100);
						}
						else if (main.viewer.type == 'video') {
							main.viewer.videoId = $('#viewedVideo').attr('rel');
							
							$('#viewer .controls a.prev').unbind('click').click(function() {
								main.viewer.load(main.viewer.videoId, 'prev');
								return false;
							});
							
							$('#viewer .controls a.next').unbind('click').click(function() {
								main.viewer.load(main.viewer.videoId, 'next');
								return false;
							});
							
							main.adjust();
							$('#sectionCtnr').css('overflow', 'auto');
						}
					}
				}
			});
		}
	},
	
	
	
	/**
	 *	Adjust main content size to display
	 */
	adjust: function() {
		var width = $(window).width();
		var height = $(window).height();
		
		if (this.page == 'intro') {
			
			var imgMaxWidth = parseInt($('#imgMaxWidth').val());
			var imgMaxHeight = parseInt($('#imgMaxHeight').val());
			
			var imgMinWidth = parseInt($('#imgMinWidth').val());
			var imgMinHeight = parseInt($('#imgMinHeight').val());
			
			var intro = $('#intro');
			if (intro.length) {
				var img = $('img.intro', intro);
				if (img.length) {
					var maxWidth = width - 100;
					var maxHeight = height - 150;
					
					if (maxWidth > imgMaxWidth) { maxWidth = imgMaxWidth; }
					if (maxHeight > imgMaxHeight) { maxHeight = imgMaxHeight; }
					
					var oldWidth = img.width();
					var oldHeight = img.height();
					var newWidth = oldWidth;
					var newHeight = oldHeight;
					
					newWidth = maxWidth;
					newHeight = (newWidth / oldWidth) * oldHeight;
					
					if (newHeight > maxHeight) {
						newHeight = maxHeight;
						newWidth = (newHeight / oldHeight) * oldWidth;
					}
					
					var isSmaller = false;
					if (newWidth < imgMinWidth) {
						newWidth = imgMinWidth;
						newHeight = (newWidth / oldWidth) * oldHeight;
						
						isSmaller = true;
					}
					
					if (newHeight < imgMinHeight) {
						newHeight = imgMinHeight;
						newWidth = (newHeight / oldHeight) * oldWidth;
						
						isSmaller = true;
					}
					
					if (isSmaller) {
						$('#sectionCtnr').css('overflow', 'auto');
					}
					else {
						$('#sectionCtnr').css('overflow', 'hidden');
					}
					
					img.width(newWidth).height(newHeight);
				}
			}
		}
		else if (this.page == 'main') {
			var sectionWidth = width - 20;
			var sectionHeight = height - 85;
			
			var sectionCtnr = $('#sectionCtnr');
			if (sectionCtnr.length) {
				sectionCtnr.width(sectionWidth).height(sectionHeight);
			}
			
			var imgMaxWidth = parseInt($('#imgMaxWidth').val());
			var imgMaxHeight = parseInt($('#imgMaxHeight').val());
			
			var imgMinWidth = parseInt($('#imgMinWidth').val());
			var imgMinHeight = parseInt($('#imgMinHeight').val());
			
			if (this.section == 'viewer') {
				var img = $('#viewedPhoto img');
				if (img.length) {
					var maxWidth = sectionWidth;
					var maxHeight = sectionHeight - 85;
					
					if (maxWidth > imgMaxWidth) { maxWidth = imgMaxWidth; }
					if (maxHeight > imgMaxHeight) { maxHeight = imgMaxHeight; }
					
					var oldWidth = img.width();
					var oldHeight = img.height();
					var newWidth = oldWidth;
					var newHeight = oldHeight;
					
					newWidth = maxWidth;
					newHeight = (newWidth / oldWidth) * oldHeight;
					
					if (newHeight > maxHeight) {
						newHeight = maxHeight;
						newWidth = (newHeight / oldHeight) * oldWidth;
					}
					
					var isSmaller = false;
					if (newWidth < imgMinWidth) {
						newWidth = imgMinWidth;
						newHeight = (newWidth / oldWidth) * oldHeight;
						isSmaller = true;
					}
					
					if (newHeight < imgMinHeight) {
						newHeight = imgMinHeight;
						newWidth = (newHeight / oldHeight) * oldWidth;
						isSmaller = true;
					}
					
					if (isSmaller) {
						$('#sectionCtnr').css('overflow', 'auto');
					}
					else {
						$('#sectionCtnr').css('overflow', 'hidden');
					}
					
					img.width(newWidth).height(newHeight);
				}
			}
			
		}
		
		
		var loading = $('#loading');
		if (loading.length) {
			loading.css({
				left: (width - loading.width()) / 2,
				top: (height - loading.height()) / 2
			});
		}
	}
};


/**
 *	jQuery plugins
 */
(function($) {

/**
 *	hoverScroll plugin
 *
 *	Only supports horizontal scroll so far
 */
$.fn.hoverScroll = function(params) {
	if (!params) {
		params = { orientation: 'horizontal' };
	}
	
	this.each(function() {
		var $this = $(this);
		
		var width = $this.width();
		
		// determine container hover zones
		var zone = {
			1: { action: 'move', from:0, to:0.06 * width, direction:'left' , speed: 16 },
			2: { action: 'move', from:0.06 * width, to:0.15 * width, direction:'left' , speed: 8 },
			3: { action: 'move', from:0.15 * width, to:0.25 * width, direction:'left' , speed: 4 },
			4: { action: 'move', from:0.25 * width, to:0.4 * width, direction:'left' , speed: 2 },
			5: { action: 'stop', from:0.4 * width, to:0.6 * width },
			6: { action: 'move', from:0.6 * width, to:0.75 * width, direction:'right' , speed: 2 },
			7: { action: 'move', from:0.75 * width, to:0.85 * width, direction:'right' , speed: 4 },
			8: { action: 'move', from:0.85 * width, to:0.94 * width, direction:'right' , speed: 8 },
			9: { action: 'move', from:0.94 * width, to:width, direction:'right' , speed: 16 }
		}
		
		function checkMouse(x, y) {
			x = x - $this.offset().left;
			y = y - $this.offset().top;
			
			for (i in zone) {
				if (x >= zone[i].from && x < zone[i].to) {
					if (zone[i].action == 'move') {
						startMoving(zone[i].direction, zone[i].speed);
					}
					else {
						stopMoving();
					}
				}
			}
		}
		
		function startMoving(direction, speed) {
			if ($this[0].direction != direction) {
				stopMoving();
				$this[0].direction  = direction;
				$this[0].isChanging = true;
				move();
			}
			$this[0].speed = speed;
		}
		
		function stopMoving() {
			$this[0].isChanging = false;
			$this[0].direction  = null;
			$this[0].speed      = 1;
			clearTimeout($this[0].timer);
		}
		
		function move() {
			if ($this[0].isChanging == false) { return; }
			
			if ($this[0].direction == 'left') { $this[0].scrollLeft -= $this[0].speed; }
			else if ($this[0].direction == 'right') { $this[0].scrollLeft += $this[0].speed; }
			
			$this[0].timer = setTimeout(function() { move(); },50);
		}
		
		$(this).mousemove(function(e) {
			checkMouse(e.pageX, e.pageY);
		});
		$(this).mouseleave(function() {
			stopMoving();
		})
	});
};


/**
 *	log to filezilla console if exists
 */
$.log = function(msg) {
	if (console && console.log) {
		console.log(msg);
	}
};


})(jQuery);


/**
 *	init library when page is ready
 */
$(document).ready(function() {
	main.init();
});
