/**
 *	Main JavaScript Control Object
 *	@author Carl Ogren
 */



var main = {
	
	page: null,	// Current page
	section: null, // Current section
	
	init: function() {
		// show loading msg when sending ajax requests
		$('#loading')
		.bind('ajaxSend', function() { $(this).fadeIn('fast'); })
		.bind('ajaxComplete', function() { $(this).fadeOut('fast'); });
		
		
		this.viewer.init();
		
		// adjust content when navigator window changes size and once now
		$(window).resize(function() { main.adjust(); });
		this.adjust();
	},
	
	
	
	
	/**
	 *	Section control
	 */
	viewer: {
		photoId: null,
		
		init: function(type) {
			this.type = type;
			this.list.load();
		},
		
		list: {
			init: function() {
				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');
				
				//width += 40;
				
				$('#photoListCtnr div.inner').width(width);
				
				$('#photoListCtnr').hoverScroll();
			},
			
			load: function() {
				$.ajax({
					type: 'get',
					data: { act: 'printPhotos' },
					success: function(response) {
						if (/error~/.test(response)) {
							common.showMsg('error', response);
						}
						else {
							$('#photoListCtnr').html(response);
							
							main.viewer.list.init();
						}
					}
				});
			}
		},
		
		load: function(id, action) {
			this.photoId = id;
			
			$.ajax({
				type: 'get',
				data: {
					act: 'printPhoto',
					id: id,
					action: action
				},
				success: function(response) {
					if (/error~/.test(response)) {
						common.showMsg('error', response);
					}
					else {
						$('#mainCtnr').css('overflow', 'hidden');
						$('#viewer').html(response);
						
						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;
						});
						
						var path = $('#photoPath').val();
						
						var flashvars = {
							path: "http://claremcgibbon.com/" + path
						};
						var params = {
							allowfullscreen: "true",
							allowscriptaccess: "always"
						};
						var attributes = {
							id: "fsFlash",
							name: "fsFlash"
						};
						
						swfobject.embedSWF("data/utils/celmcgFS.swf", "fsFlash", "20", "15", "9.0.0","expressInstall.swf", flashvars, params, attributes);
						
						setTimeout(function() {
							main.adjust();
							$('#viewedPhoto').css('opacity', 100);
							$('#mainCtnr').css('overflow', 'auto');
						}, 100);
					}
				}
			});
		}
	},
	
	
	
	/**
	 *	Adjust main content size to display
	 */
	adjust: function() {
		var width = $(window).width();
		var height = $(window).height();
		
		if (width < 1024) { width = 1024; }
		if (height < 600) { height = 600; }
		
		$('#main').width(width).height(height);
		
		var imgMaxWidth = parseInt($('#imgMaxWidth').val());
		var imgMaxHeight = parseInt($('#imgMaxHeight').val());
		
		var imgMinWidth = parseInt($('#imgMinWidth').val());
		var imgMinHeight = parseInt($('#imgMinHeight').val());
		
		var img = $('#viewedPhoto img');
		if (img.length) {
			var maxWidth = width - 20;
			var maxHeight = height - 110;
			
			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) {
				$('#mainCtnr').css('overflow', 'auto');
			}
			else {
				$('#mainCtnr').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.parent()
		.mousemove(function(e) {
			checkMouse(e.pageX, e.pageY);
		})
		.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();
});
