/**
 * @author gavinwilliams
 */

$(document).ready(function(){
	
	var first = null;
	var current = null;
	var time = 0;
	var msPerWord = 250;
	var interval;
	var xml;
	
	if($("body#home").length){
	
		if (GBrowserIsCompatible()) {
	        var map = new GMap2(document.getElementById("map"));
			var area = {
				longitude: $("#areas li:first .longitude").text(),
				latitude: $("#areas li:first .latitude").text()
			}
			map.setCenter(new GLatLng(parseFloat(area.latitude), parseFloat(area.longitude)), 12);
	    }
		
		var wait = 0;
		var timeout = 2000;
		var areaCount = $("#areas li").length;
			
		$("#areas li").each(function(i){
			if(i+1 < areaCount){
				$(this).append(", ");
				$(this)
			}
			$(this).css({display: "inline", opacity: 0, cursor: "pointer", backgroundColor: "#FFFFFF"});
			var area = {
					latitude: $(".latitude", this).text(),
					longitude: $(".longitude", this).text(),
					element: this
			}
			setTimeout(function(){showArea(area)}, wait);
			wait = wait + timeout;
		});
		
		$("#areas").mouseout(function(){
			//$("li", this).css({fontWeight: "normal", color: "#000000"});
		});
	}
	
	if($("body#testimonials").length){
		
		$("ul.testimonials li").each(function(i){
			
			$(this).css({position: "absolute"});
			
			$(this).attr("time", wordCount(this) * msPerWord);
			
			if(i > 0){
				$(this).css({display: "none"});
			}else{
				first = this;
				current = this;
				time =  $(current).attr("time");
				interval = self.setInterval(function(){next(current)}, time);
			}
	
			$(this).click(function(){
				next(this);
			});
		});
	}
	
	if($(".lessonCalculator").length){
		
		$("#results").css({opacity: 0});
		$("#skillLevel").css({opacity: 0});

		
		$.ajax({
			type: "GET",
			url: "data/experience.xml",
			success: function(resp){
				xml = resp;

				
				// Populate the age field apply actions and enable it

				$("experience", xml).each(function(){		
					$("#age select").append("<option value=\"" + $(this).attr("age") + "\">" + $(this).attr("age") + "</option>\n\r");
				});
				
				$("#skillLevel select").change(function(){
					
					$("#results").animate({opacity: 0}, "slow", null, function(){
						calculateLessons();
						$("#results").animate({opacity: 1}, "slow");
					});

				});
				
				$("#age select").change(function(){
					$("#skillLevel select").removeAttr("disabled");
					
					if($("#skillLevel select").val() != ""){
						$("#results").animate({opacity: 0}, "slow", null, function(){
							calculateLessons();
							$("#results").animate({opacity: 1}, "slow");
						});
					}
					
					$("#skillLevel").animate({opacity: 1}, "slow");
					
				});
				
				$("#age select").removeAttr("disabled");
			}			   
		});
	
	}

	function calculateLessons(){
							
		minLessons = $("experience[@age=" + $("#age select").val() + "]/" + $("#skillLevel select").val(), xml).attr("min");
		maxLessons = $("experience[@age=" + $("#age select").val() + "]/" + $("#skillLevel select").val(), xml).attr("max");
		
		minWeeks = minLessons / 2;
		maxWeeks = maxLessons / 2;
		
		minCost = minLessons * 21;
		maxCost = maxLessons * 21;
		
		$("#results .minLessons").html(minLessons);
		$("#results .maxLessons").html(maxLessons);
		
		$("#results .minWeeks").html(minWeeks);
		$("#results .maxWeeks").html(maxWeeks);
		
		$("#results .minCost").html("&pound;" + minCost);
		$("#results .maxCost").html("&pound;" + maxCost);
		
	}

	function next(el){
		var ul = $(el).parent('ul');
		var listLength = $('li', ul).length;
	
		/*
		$(el).fadeOut(50, function(i){
				var randomItem = getRandomNumber(i, listLength);
				$('li:eq(' + randomItem + ')', ul).fadeIn();
				current = $('li:eq(' + randomItem + ')', ul);
		});
		*/

		var element = {
			index: $(el).attr("index"),
			next: $(el).next("li"),
			previous: $(el).prev("li"),
			hasNext: $(el).next("li").length,
			hasPrev: $(el).prev("li").length
		};
		
		if(element.hasNext){
			$(element.next).fadeIn("slow", function(){
				$(el).fadeOut();
				current = element.next;
			});
		}else{
			$(first).css("display", "block");
			$(el).fadeOut();
			current = first;
		}
				
		resetInterval();
	}
	
	function getRandomNumber(index, limit){
		
		randomNumber = 0;
		randomNumber = Math.floor(Math.random() * limit);
		// alert(randomNumber + ' ' + index);
		if(randomNumber == index){
			return getRandomNumber(index, limit);
		}
		
		
		return randomNumber;
	}
		
	function wordCount(el){
		
		text = $(el).text();
		
		wordcounter=0;
		
		for (x=0;x< text.length;x++) {
			if (text.charAt(x) == " " && text.charAt(x-1) != " ") {wordcounter++}  // Counts the spaces while ignoring double spaces, usually one in between each word.
		}
				
		return wordcounter;
	
	}

	function resetInterval(){
		window.clearInterval(interval);
		time = $(current).attr("time");
		interval = self.setInterval(function(){next(current)}, time);
	}

	function showArea(area){
		
		$("#areas li span.fn").css({fontWeight: "bold", color: "#CCCCCC"});
		$("#areas li").css({fontWeight: "bold", color: "#CCCCCC"});
		$("span.fn", area.element).css({fontWeight: "bold", color: "#000000"});
		
		var point = new GLatLng(parseFloat(area.latitude), parseFloat(area.longitude));
		var icon = new GIcon();
		var icon2 = new GIcon();
		
		icon.image = "http://kh.google.com:80/flatfile?lf-0-icons/773_n.png";
		icon.iconSize = new GSize(32,32);
		icon.iconAnchor = new GPoint(16,16);
		
		icon2.image = "http://kh.google.com:80/flatfile?lf-0-icons/773_h.png";
		icon2.iconSize = new GSize(32,32);
		icon2.iconAnchor = new GPoint(16,16);
		
		var marker = new GMarker(point, icon);
		var marker2 = new GMarker(point, icon2);
		
		var zoom = 0;
		
		map.addOverlay(marker);
				
		$(area.element).animate({opacity: 1});
		map.panTo(point);
		
		GEvent.addListener(marker, "click", function(){
			map.panTo(point);
			$("#areas li span.fn").css({fontWeight: "bold", color: "#CCCCCC"});
			$("#areas li").css({fontWeight: "bold", color: "#CCCCCC"});
			$("span.fn", area.element).css({fontWeight: "bold", color: "#000000"});
		});
		
		$(area.element).click(function(){
			map.zoomIn();
			zoom++;
		});
		
		$(area.element).hover(
		function(){
			map.panTo(point);
			map.removeOverlay(marker);
			map.addOverlay(marker2);
			$("#areas li span.fn").css({fontWeight: "bold", color: "#CCCCCC"});
			$("#areas li").css({fontWeight: "bold", color: "#CCCCCC"});
			$("span.fn", area.element).css({fontWeight: "bold", color: "#000000"});
		},
		function(){
			for(var i = 0; i < zoom; i++){
				map.zoomOut();
				zoom--;
			}
			map.removeOverlay(marker2);
			map.addOverlay(marker);
		});
		
		return true;
	}
	
});