﻿$(document).ready(function() {

	var scrolling = true;
	var scrollInterval = 0;
	
	// Handle the address setup and section scrolling
	$.address
		.crawlable(true)
		.init(function (event) {
			$("ul.nav a").address();
		}).change(function (e) {
			var sectionName = e.path.replace(/^\//g, "");
			
			if (sectionName == "") {
				sectionName = $(".nav a:first").attr("href").replace(/^#!/g, "");
			}
			
			var section = $("#" + sectionName);
			
			if (section.length > 0) {
				setActiveNav(sectionName);
				
				scrolling = false;
				
				$("html, body")
		        	.stop()
		        	.animate({       
			            scrollLeft: section.offset().left
			        }, scrollInterval, function () {
			        	scrolling = true;
			        });
		    }
		       
		    scrollInterval = 3000;
		}).history(true);
	
	function setActiveNav (sectionName) {
		$.each($("ul.nav a"), function () {
			$(this).removeClass("selected");
		});
		
		$("ul.nav a#" + sectionName + "-nav").addClass("selected");
	}
	
	// Set the active menu item when the age is scroller
	$(window).bind("scroll", function () {
		var section = null;
		var scrollLeft = $(this).scrollLeft();
		
		$.each ($(".section"), function () {
			if ($(this).offset().left <= scrollLeft) {
				section = $(this);
			}
		});
		
		if (section != null && scrolling) {
			var sectionName = section.attr("id");
			
			setActiveNav(sectionName);
		}		
	});
		
    // Poll
    function renderPoll(container) {
    	var labelWidth = 0;
    	var labels = $(".poll-answer-text, .poll-result-text");
    	var values = $(container).find(".poll-result-responses-value");
    	
		$.each(labels, function () {
			var width = $(this).width();
			
			if (width > labelWidth) {
				labelWidth = width;
			}
		});
		
		$.each(labels, function () {
			var width = $(this).width();
			var padding = labelWidth - width;
			
			if (padding > 0) {
				$(this).css("padding-right", padding + 10 + "px");
			}
		});
	
    	$.each(values, function () {
    		var value = $(this).html();
    		    		
    		$(this).closest(".poll-result-responses").css("padding-left", value + "px");
    	});
    }
    
    function bindSubmit(container) {
    	var pollSubmit = $(container).find(".poll-submit");

    	$("<img class=\"poll-submit-button\" src=\"images/poll-submit.png\" alt=\"Submit\" />").appendTo(pollSubmit);
    	
    	pollSubmit.find(".poll-submit-button").unbind("click").bind("click", function () {
    		var id = $(".poll-answer-selector input:checked").val();
    	
    		$(".poll").html(
    			"<div class=\"poll-loader\">" +
	      	  	"<img src=\"images/ajax-loader.gif\" alt=\"Submitting answer\" /> Submitting answer..." +
    	  		"</div>");
    		    	
    		$.post("poll/manage.php?action=submit", { id: id }, function (data) {
    			$(".poll").html(data);
    			
    			renderPoll(".poll");
    			bindSubmit(".poll");
    		});
    	});
    }
    
    function getQuery(key) {
    	var queryPos = window.location.href.indexOf('?');
    	
    	if (queryPos > -1) {
	    	var query = window.location.href.slice(queryPos + 1);
			var items = query.split("&");
			
			for (var i = 0; i < items.length; i++) {
				var item = items[i].split("=");
				
				if (item[0] == key) {
					return "?" + items[i];
				}
			}    	
		}
		
		return "";	
    }
    
    $(".poll").load("poll/" + getQuery("np"), function () {
		renderPoll(this);
    	bindSubmit(this);
    });
    
	// Carosel 
	$(".slider .carosel").jCarouselLite({
		btnNext: ".slider .next",
		btnPrev: ".slider .prev"
	});
    
    // Image animations
    $(".homeimage").animator({
    	type: "vertical",
		offset: 3,
		interval: 1200
    });
	
    $(".aboutimage").animator({
    	type: "vertical",
		offset: 8,
		interval: 750
    });
    
    $(".contactlinks").animator({
    	type: "vertical",
		offset: 1,
		interval: 30
    });
    
    $(".sharkimage").animator({
    	type: "horizontal",
		offset: 50,
		interval: 5000,
		complete: function (element) {
			var image = element.find("img");
			var path = image.attr("src");
			var direction = $.data(element, "direction");

			if ((direction ? direction : "left") == "left") {
				direction = "right";
			}
			else {
				direction = "left";
			}
			
			element.html("<img src=\"images/shark" + (direction == "left" ? "" : "_flipped") + ".png\" width=\"69\" height=\"86\" alt=\"shark\" />");
			element.show();
			
			$.data(element, "direction", direction);		
		}
    }).hide();   
    
	if (!$.browser.msie && false) {
		$("#boat-image").ready(function () {
			$("#boat-image").animator({
				type: "rotate",
				offset: 3,
				interval: 70
			});
		});
		
		$("#balloon").ready(function () {
			$("#balloon").animator({
				type: "rotate",
				offset: 0.5,
				interval: 250
			});
		});
	}	
	
	// Scrolling panel
	$('.scroll-pane').jScrollPane();

	// Tooltips
	$('a.clickhome').aToolTip({
		clickIt: true,
		tipContent: 'Watch out! Polar Bears might look cute and cuddly but they have got 42 razor sharp teeth which pack a fearsome bite. Looks like you need to subscribe to our free newsletter <span class="adlink"><a href="http://eepurl.com/fjgdw">(click here)</a></span> for more useful tips...' 
	});	
	
	$('a.clickportfolio').aToolTip({
		clickIt: true,
		tipContent: 'Wow a rocket...! Did you know that the Saturn V rocket, which carried Neil and Buzz to the Moon, needed 7.5 million pounds of thrust to get into orbit. Luckily you do not need to spend £7.5 million to get your advertising campaign into a higher orbit - just give us a call!' 
	});	
	
	$('a.clickservices').aToolTip({
		clickIt: true,
		tipContent: 'Awww, look Pengiuns, everyone loves them (even when they are not covered in chocolate...). If you would like to sweeten your companies marketing approach but are not sure how then give us a call!' 
	});	
	
	$('a.clickabout').aToolTip({
		clickIt: true,
		tipContent: 'Cool - a hot air balloon. What a perfect way to travel around the world... well, perhaps not - it has only been done twice! Never mind, let AnsteyDesign take you on a creative journey instead full of colour, inspiration, fun and adventure.' 
	});	
	
	$('a.clickcontact').aToolTip({
		clickIt: true,
		tipContent: 'Sun, sea, sand and a Shark! Luckily you are safe at your desk so not to panic. But did you know sharks are one of the oldest creatures on the planet? They were even around before the dinosaurs! Does it feel like your business branding has become a bit of a shark - all teeth and no Jaws? Why not talk to us about a rebrand!' 
	});	
	
	$('a.clicklinks').aToolTip({
		clickIt: true,
		tipContent: 'Once upon a time the Kraken was the most feared creature of the sea. Today it is considered a bit of a squid - literally. While many myths and legends still remain you can be sure that AnsteyDesign will always deliver on what we promise. For more information, give us a call!' 
	});
});


