var menu = {
	MAIN_PICTURE_ROTATION_DELAY: "5", //in seconds
	init: function () {
		sub_title = $("#submenu_title");
		sub_menu = $("#submenu_menu");
		$("#menu_desc").hide();
		$("#paging").hide();
		$("#main_text").hide();
		$_current_picture_index = 0;
		$_current_image_queue = [];
		$_current_menu = false;
		sub_menu.hide();
		
		$.ajax({
		  url: "/data/menu.json",
		  dataType: 'json',
		  success: menu.activateMenu
		});
		
		$("#prevImage").click(function() {
				$_current_picture_index = (--$_current_picture_index < 0) ? $_current_image_queue.length - 1: $_current_picture_index;
				$("#current_image_index").text($_current_picture_index + 1);
				menu.loadImage();
		});
		
		$("#nextImage").click(menu.loadNextImage);
	},
	activateMenu: function(data) {
		$_contents = data;
				
		$.each($_contents.menus, function(index, _menu) {
			
			if(_menu.location == "Work") {
				$("<li>").appendTo("#submenu_menu ul");
			} else {
				$("<li>").appendTo("#submenu_info ul");	
			}
			
			
			_menu_location = (_menu.location == "Work") ? $("#submenu_menu li:last-child") : $("#submenu_info li:last-child");
			
			$("<a>").attr("href", "javascript:;").text(_menu.name).appendTo(_menu_location).click(function() {
				if($_playSlideshow)
					clearInterval($_playSlideshow);
				if(_menu.type == "Gallery") { //Gallery style link
					
					$("#main_text").slideUp('slow', function() {
						$_current_picture_index = 0;
						$_current_image_queue = _menu.pictures;
						$_current_menu = _menu;
						menu.loadImage();
						$("#current_image_index").text("1");
						$("#total_image_size").text(_menu.pictures.length);
						$("#paging").show();
						$("#paging_list").hide();
						$("#paging_list a").remove();
						$.each(_menu.pictures, function(idx, pic){
							$picture_url = pic.name;
							$("<a>").attr("href", "javascript:;").click(function(){
								$_current_picture_index = idx;
								$("#current_image_index").text($_current_picture_index + 1);
								menu.loadImage();
							}).appendTo("#paging_list");
							 
							$("<img>").attr("src", '/photos/' + menu.parseUrl($picture_url).filename + '_s.' + menu.parseUrl($picture_url).ext)
								.appendTo("#paging_list a:last-child")
							//	.error(function(){
							//	$(this).parent().remove();
							//});
							
						});
						
						$("#paging_list").show('slow');
						$("#main_image").slideDown('slow', function(){
						});						
					});
					
				} else { //html style link
					$("#paging_list").hide('slow', function() {
						$("#paging").hide();
					});
					$("#main_text").html(_menu.html);
					$("#main_image").slideUp('slow', function() {
						$("#main_text").slideDown('slow');
					}); 
				}
				
			}).mouseout(function(){
				$("#menu_desc").hide(5, function(){
					if($_current_menu && $_current_menu.description) {
						_menu_desc = $_current_menu.description.replace(/\n/g, "<br/>");
						$(this).html(_menu_desc).show();
					}	
				});
			}).mouseover(function(){
				if(_menu.description) {
					_menu_desc = _menu.description.replace(/\n/g, "<br/>");
					$("#menu_desc").html(_menu_desc).show();
				}
			});
		});
		
		if($_contents.main_pictures.length > 0) {
			$_current_image_queue = $_contents.main_pictures;
			$_playSlideshow = setInterval( menu.loadNextImage, menu.MAIN_PICTURE_ROTATION_DELAY * 1000 ); 
			menu.loadNextImage();
		} else {
			$_playSlideshow = false;
		}
		
		if (sub_menu.is(":hidden")) {
			sub_menu.slideDown("slow");
		} else {
			sub_menu.slideUp();
		}
	},
	parseUrl: function(data) {
		data = data.replace(/^\s|\s$/g, ""); //trims string

		if (/\.\w+$/.test(data)) {
			if (data.match(/([^\/\\]+)\.(\w+)$/) )
				return {filename: RegExp.$1, ext: RegExp.$2};
			else
				return {filename: "no file name", ext:null};
		}
		else {
			if (data.match(/([^\/\\]+)$/) )
				return {filename: RegExp.$1, ext: null};
			else
				return {filename: "no file name", ext:null};
		}
	}, 
	loadNextImage: function() {
			$_current_picture_index = (++$_current_picture_index >= $_current_image_queue.length) ? 0 : $_current_picture_index;
			$("#current_image_index").text($_current_picture_index + 1);
			menu.loadImage();
		
	},
	loadImage: function() {
		//$("#main_image").addClass('loading');
		$("#main_image").css('opacity', '0.5');

		$("#main_image img:first-child").removeAttr('style');
		var img = new Image();
		
		$(img).load(function () {
			
			$(img).hide();
			$("#main_image").css('opacity', '');
			//$('#main_image').removeClass('loading');
			
			$('#main_image a').append($(img));
			
			//$current_image = $("#main_image_link img:first-child");
			$current_image = $(this).prev();
			
			$current_image.hide(10, function() {
				$(this).remove();
				$(img).show();
			});
        }).error(function () {
			// notify the user that the image could not be loaded
        }).attr('src', '/photos/' + $_current_image_queue[$_current_picture_index].name);
		
	}
};

$(document).ready(menu.init);
