$(document).ready(function() {
	
	if($(".intro-more").length > 0) {
		introExt();
	}	
	if($(".info-more").length > 0) {
		infoExt();
	}	
	if($("#hero #slideshow li").length > 1) {
		heroSlideshow($("#hero"));
	}	
	if($("dl.services").length > 0) {
		servicesList();
	}	
	if($("#map-canvas").length > 0) {
		mapInit();
	}
	if($("#heroVideo").length > 0) {
		heroVideo();
	}
	
});

//HOMEPAGE MOVIE
function heroVideo() {	
	flowplayer("heroVideo", "video/flowplayer-3.2.7.swf", {
		clip: {
			url: "video/BodyandSoleUK.mp4",
			autoPlay: true
		},
		plugins: {
			controls: {
				autoHide: "always",
				durationColor: "#a3a3a3",
				timeBorder: "0px solid rgba(0, 0, 0, 0.3)",
				bufferColor: "#445566",
				bufferGradient: "none",
				volumeSliderGradient: "none",
				backgroundColor: "#000000",
				buttonOverColor: "#ffffff",
				sliderColor: "#000000",
				buttonColor: "#ffffff",
				progressGradient: "none",
				buttonOffColor: "rgba(130,130,130,1)",
				timeSeparator: " ",
				timeColor: "#ffffff",
				volumeColor: "#ffffff",
				sliderGradient: "none",
				tooltipColor: "#000000",
				backgroundGradient: "none",
				progressColor: "#9FA617",
				volumeBorder: "1px solid rgba(128, 128, 128, 0.7)",
				sliderBorder: "1px solid rgba(128, 128, 128, 0.7)",
				volumeSliderColor: "#ffffff",
				timeBgColor: "rgb(0, 0, 0, 0)",
				tooltipTextColor: "#ffffff",
				borderRadius: "0px",
				height: 24,
				opacity: 1.0
			}
		}
	});
}


//INTRO TEXT TOGGLE
function introExt() {
	
	$("a.intro-more").each(
		function() {
			
			$(this).unbind();
			
			$(this).toggle(
				function() {
					
					$(this).parent().next("#intro-ext").slideDown(400);
					$(this).css("background-position", "100% -198px");
					$(this).text("Hide");
					
					return false;
					
				}, function() {
					
					$(this).parent().next("#intro-ext").slideUp(400);
					$(this).css("background-position", "100% 2px");
					$(this).text("More");
					
					return false;
					
				}
			);
			
		}
	);
	
}

//MORE INFO TEXT TOGGLE
function infoExt() {
	
	$(".info-more a").each(
		function() {
			
			$(this).unbind();
			
			$(this).toggle(
				function() {
					
					$(this).parent().next(".info-ext").slideDown(400);
					$(this).css("background-position", "100% -198px");
					
					return false;
					
				}, function() {
					
					$(this).parent().next(".info-ext").slideUp(400);
					$(this).css("background-position", "100% 2px");
					
					return false;
					
				}
			);
			
		}
	);
	
}

//HERO SLIDESHOW
function heroSlideshow(thisHero) {
	
	var targetSlideshow = $(thisHero).children("#slideshow");
	targetNav = $(thisHero).children(".nav");
	
	slideshowLength = $("li", targetSlideshow).length;
	slideshowCurrent = 1;
	
	for(i=1; i<=slideshowLength; i++) {
		
		if(i == 1) {
			$(targetNav).append("<li><a class=\"current\" id=\"slideshow-nav-" + i + "\" href=\"#slideshow-" + i + "\">" + i + "</a></li>");
		} else {
			$(targetNav).append("<li><a id=\"slideshow-nav-" + i + "\" href=\"#slideshow-" + i + "\">" + i + "</a></li>");
		}
		
	}
	
	$("li a", targetNav).each(
		function() {
			
			$(this).unbind("click");
			
			$(this).click(
				function() {
					
					$(thisHero).stopTime();
					
					var targetSlideshowImage = $(this).attr("href");
					targetSlideshowImage = targetSlideshowImage.substr(11);
					
					if(targetSlideshowImage != slideshowCurrent) {
						
						navigateSlideshowImage(thisHero, targetNav, targetSlideshowImage, 1);
						
					}
					
					return false;
					
				}
			);
			
		}
	);
	
	slideshowTimer(thisHero);
	
}

function slideshowTimer(thisHero) {
	
	$(thisHero).everyTime(4000, function() {
		
		var targetSlideshowImage = slideshowCurrent + 1;
		
		if(targetSlideshowImage > slideshowLength) {
			
			targetSlideshowImage = 1;
			
		}
		
		navigateSlideshowImage(thisHero, targetNav, targetSlideshowImage);
		
	});
	
}

function navigateSlideshowImage(thisHero, thisNav, thisImage, startTime) {
	
	$("li a", thisNav).removeClass("current");
	$("#slideshow-nav-" + thisImage).addClass("current");
	
	var targetImage = $("#slideshow-" + thisImage);
	$(targetImage).addClass("next").fadeIn(800, function() {
		
		$("#slideshow-" + slideshowCurrent).hide();
		$(this).removeClass("next");
		
		slideshowCurrent = thisImage;
		
		if(startTime) {
			slideshowTimer(thisHero);
		}
		
	});
	
}

//SERVICES LIST
function servicesList() {
	
	$("dl.services dt").each(
		function() {
			
			$(this).unbind();
			
			$(this).hover(
				function() {
					
					$(this).addClass("hover");
					$(this).css("cursor", "pointer");
					
				}, function() {
					
					$(this).removeClass("hover");
					
				}
			);
			
			$(this).toggle(
				function() {
					
					$(this).next("dd").slideDown(400);
					$(this).addClass("open");
					
				}, function() {
					
					$(this).next("dd").slideUp(400);
					$(this).removeClass("open");
					
				}
			);
			
		}
	);
	
}

//GOOGLE MAP
function mapInit() {
	
	var map = new GMap2(document.getElementById("map-canvas"));
	//map.setCenter(new GLatLng(52.5873, -1.83147), 15);
	map.addControl(new GSmallZoomControl3D());
	map.addControl(new GMenuMapTypeControl());
	
	var geocoder = new GClientGeocoder();
	
	var address = "9 Belwell lane, Mere Green, Sutton Coldfield, West Midlands B74 4AA";
	
	showAddress(address);
	
	function showAddress(address) {
		geocoder.getLatLng(
			address,
			function(point) {
				if (!point) {
					alert(address + " not found");
				} else {
					map.setCenter(point, 15);
					var marker = new GMarker(point);
					map.addOverlay(marker);
					//marker.openInfoWindowHtml(address);
				}
			}
		);
	}
	
}

//ACTION TRACKING
function trackAction(action, page, title, type) {
	
	page = page.substr(page.indexOf("bodyandsoleuk.co.uk/"));
	
	var track_path = "/" + action + "/" + page + "/" + title;
	
	if(type) track_path += "/" + type;
	
	track_path = unescape(track_path);
	pageTracker._trackPageview(track_path);
	
}


