/**
 * @author Jeremy.Manoto
 */

	var slider;


	$(document).ready(function() {
		
		// Initialize the Header Banner images being cycled
		Header_Init();
		
		//Initialize Flash Map
		Flash_Init();
		
		// Initialize the Slider
		Slider_Init();
		
		// Accordion Nav
		Accordion_Init();
		
		//Content Init();
		Content_Init();
	});
	
	
	/**
	 * Initialize the Flash Map
	 * Uses swfobject to embed.
	 */
	function Flash_Init() {
		var flashVar = { xml_path: "flash/map-data.xml"	};
		var flashParams = {	wmode: "transparent" };
		var attributes = { id: "locationMap", name: "locationMap" };

		swfobject.embedSWF("flash/map.swf", "flashcontainer", "140", "120", "9.0.0", null, flashVar, flashParams, attributes); 	
	}
	//Minor function to get a flash movie element based on a name
 	function thisMovie(movieName) {
		if (navigator.appName.indexOf("Microsoft") != -1) {
			return window[movieName];
		} else {
			return document[movieName];
		}
	}


	/**
	 * Initialize the header banner images cycling.
	 */
	function Header_Init() {
		$('.header .images').cycle({
			fx: 'fade',
			timeout: 10000, // Wait Interval in ms
			pause: 1 //Pause the cycle on mouse over. 1=enable, 0=disable.
		}).show();

	}


	/** 
	 * Hide all content elements except the first one (Pure Tasmania)
	 */
	function Content_Init() {
		$(".main .content .item:first").show();
		
		
		/**
		 * Switch Content
		 */
		$(".menu .slider .content li").live("mouseover", function() {
			var li = $(this);
			var anchor = $(this).children("a");
			var flashRef = anchor.attr("location");
			var refID = anchor.attr("rel");
			var image = anchor.find("img");
			
			var transitionSpeed = "fast";
			
			$(".main .content .item:visible").fadeOut(transitionSpeed, function() {
				if ($(".main .content .item:visible").length == 0) {
					$("#" + refID).fadeIn(transitionSpeed);
				}
			});
			if ($(".main .content .item:visible").length == 0) {
				$("#" + refID).fadeIn(transitionSpeed);
			}
			
			//remove all other hover CSS classes
			li.siblings().removeClass("hover");
			//Apply hover class to current one
			li.addClass("hover");
			
			thisMovie("locationMap").gotoLocation(flashRef);
			
			slider.stop();
			
		}).live("mouseout", function() {
			slider.start();
			
		}).live("click", function() {
			slider.stop();
		});
		
		
	}

	
	/**
	 * Initialize the Experience Slider
	 */
	function Slider_Init() {
		
		//Auto Slider format: (<container>,<element>,<animationtime>,<units>,<waitinterval:seconds>)
		slider = new AutoSlider("#slider", "li", 1000, 5, 19, Slider.Direction.LEFT)
		slider.start();
		
		$(".menu .right.button").click(function() {
			$(".menu").data("sliding", true);
			slider.slide(Slider.Direction.LEFT);
		}).hover(function() {
			slider.slide(Slider.Direction.LEFT);
		}, function() {
			$(".menu").data("sliding", false);			
		});
		$(".menu .left.button").click(function() {
			slider.slide(Slider.Direction.RIGHT);
		}).hover(function() {
			$(".menu").data("sliding", true);
			slider.slide(Slider.Direction.RIGHT);
		}, function() {
			$(".menu").data("sliding", false);
		});
		
		/**
		 * Tool Tips
		 */
		var xOffset = 10;
		var yOffset = 20;	
		$(".menu .content img").hover(function(e) {
			var img = $(this);
			var tooltip = img.attr("title");

			if (tooltip != "") {
				img.data("title", tooltip);
				img.attr("title", "");
			}

			
			if (tooltip != "") {
				$("#tooltip").html(tooltip).show();
//					.css("top",(e.pageY - xOffset) + "px")
//					.css("left",(e.pageX + yOffset) + "px")
			}

			
		}, function() {
			var img = $(this);
			img.attr("title", img.data("title"));
			$("#tooltip").css("top", "-20px").css("left", "-100px");
			//$("#tooltip").hide();
			

		}).mousemove(function(e) {
			$("#tooltip")
				.css("top",(e.pageY - yOffset) + "px")
				.css("left",(e.pageX + xOffset) + "px").show();
		});
		
		$("#tooltip").mousemove(function(e) {
			$(this)
				.css("top",(e.pageY - yOffset) + "px")
				.css("left",(e.pageX + xOffset) + "px").show();
		})
		
		$(".menu .content").mouseout(function() {
			$("#tooltip").hide();
		})
		

		
	}
	
	
	/**
	 * Initialize the Accordion Nav.
	 * Animation & Click behavior are defined in here.
	 */
	function Accordion_Init() {
		
		$(".nav").show();
	
		$(".nav > ul > li").each(function() {
			var navItem = $(this);
			var subNav = navItem.find(".subnav");
			
			if (subNav.length > 0) {
				subNav.data("fullHeight", subNav.outerHeight({margin: "true"}));
				subNav.height(0).css({display: "none"});
			}
		});
		

		
		$(".nav > ul > li").hover(function() {
			var navItem = $(this);
			var subNav = navItem.find(".subnav");
			
			var openSubNav = navItem.find(".subnav");
			var isOpen = openSubNav.outerHeight() > 0 && openSubNav.css("display") == "block";
			
			var hasSubNav = subNav.length > 0;

			//alert(openSubNav.css("display"))

			if (hasSubNav && !isOpen) {
				var currentSubNav = $(".nav .subnav:visible");
				currentSubNav.stop(true, false).slideUp("slow", function() { $(this).height(0); }) //.animate({height: "0"});
				subNav.css({ display: "block"});
				subNav.height(0);
				subNav.animate({height: subNav.data("fullHeight") });
				
			}
		}, function() {}).click(function() {
			var navItem = $(this);
			var subNav = navItem.find(".subnav");
			var openSubNav = navItem.find(".subnav:visible");
			var isOpen = openSubNav.length > 0;
			
			if (isOpen) {
				subNav.slideUp("slow", function() { $(this).height(0).css({display: "none"}); });
			}
		});
		$(".nav ul").mouseleave(function(){
			
			$(".subnav:visible").slideUp("slow", function() { $(this).height(0).css({display: "none"}); });

		});
		
	}
