$(function(){

	$('a[href=#]').live('click', function(){return false;});

	$("#nav-btns li a").addClass("js");
	$("#nav-btns li a").hover(
		function(){
			$(this).stop(true, true).animate({backgroundPosition:"(50% -1px)"}, 200);
		},
		function(){
			$(this).animate({backgroundPosition:"(50% -30px)"}, 200);
		}
	);
	
	// Using default configuration
	$("#hero-img").carouFredSel({
	circular: "false",
	direction: "up",
	height: 343,
	width: 960,
	items: 1,
	scroll: {
		duration: 1000,
		pauseOnHover: true
	},
	auto: 7000,
	pagination: {
		container: '#hero-pagination',
		anchorBuilder: function( nr ) {
			return "<a id='p"+nr+"' href='#'></a>"
			}
		}
	});
	
	
	
	/*
	//Show the paging and activate its first link
	$("#hero-pagination").show();
	$("#hero-pagination a:first").addClass("active");

	//Get size of the image, how many images there are, then determin the size of the image reel.
	var imageHeight = $("#hero-img a:first").height();
	var imageSum = $("#hero-img img").size();
	var imageReelHeight = imageHeight * imageSum;

	//Adjust the image reel to its new size
	$("#hero-img").css({'height' : imageReelHeight});
	
	//Paging  and Slider Function
	rotate = function(){
    	var triggerID = $active.attr("rel") - 1; //Get number of times to slide
    	var image_reelPosition = triggerID * imageHeight; //Determines the distance the image reel needs to slide

    	$("#hero-pagination a").removeClass('active'); //Remove all active class
   		$active.addClass('active'); //Add active class (the $active is declared in the rotateSwitch function)

    	//Slider Animation
    	$("#hero-img").animate({
        	top: -image_reelPosition
    	}, 700 );

	}; 

	//Rotation  and Timing Event
	rotateSwitch = function(){
    	play = setInterval(function(){ //Set timer - this will repeat itself every 7 seconds
        	$active = $('#hero-pagination a.active').next(); //Move to the next paging
        	if ( $active.length === 0) { //If paging reaches the end...
            	$active = $('#hero-pagination a:first'); //go back to first
        	}
        	rotate(); //Trigger the paging and slider function
    	}, 7000); //Timer speed in milliseconds (7 seconds)
	};

	rotateSwitch(); //Run function on launch
	
	//On Hover
	$("#hero-img a").hover(function() {
    	clearInterval(play); //Stop the rotation
	}, function() {
    	rotateSwitch(); //Resume rotation timer
	});	

	//On Click
	$("#hero-pagination a").click(function() {
    	$active = $(this); //Activate the clicked paging
    	//Reset Timer
    	clearInterval(play); //Stop the rotation
    	rotate(); //Trigger rotation immediately
    	rotateSwitch(); // Resume rotation timer
    	return false; //Prevent browser jump to link anchor
	});
	*/
		
	String.prototype.parseURL = function() {
		return this.replace(/[A-Za-z]+:\/\/[A-Za-z0-9-_]+\.[A-Za-z0-9-_:%&\?\/.=]+/, function(url) {
			return url.link(url);
		});
	};
	
	String.prototype.parseUsername = function() {
		return this.replace(/[@]+[A-Za-z0-9-_]+/, function(u) {
			var username = u.replace("@","")
			return u.link("http://twitter.com/"+username);
		});
	};
	
	String.prototype.parseHashtag = function() {
		return this.replace(/[#]+[A-Za-z0-9-_]+/, function(t) {
			var tag = t.replace("#","%23")
			return t.link("http://search.twitter.com/search?q="+tag);
		});
	};
	
	var username='TangentOnDemand'; // set user name
	var format='json'; // set format, you really don't have an option on this one
	var url='http://api.twitter.com/1/statuses/user_timeline/'+username+'.'+format+'?callback=?'; // make the url

	$.getJSON(url,function(tweet){ // get the tweets
		var tweettext = (tweet[0].text).parseURL().parseUsername().parseHashtag() + '<br /><a id="follow" href="http://www.twitter.com/TangentOnDemand">Follow us&nbsp<img src="img/follow_arrow.png" alt="Follow us border="0" /></a>';
		$("#tweet").html(tweettext);
	});
	
	$('#spring-back a').smoothScroll();
	
	
	
	$('#contact-submit-btn').click(function() {
    	
		$('#contact-form').validate({
			rules: {
				contact_email_field: {
					required: true,
					email: true
				},
				contact_name_field: {
					required: true
				}
			},
			
			messages: {
				contact_email_field: {
					required: "An email address is required",
					email: "Please check your email address for errors"
				},
				contact_name_field: {
					required: "Please tell us your name"
				}
			},
			
			errorLabelContainer: "#messagebox",
			wrapper: "li",
			
			submitHandler: function(){
				
				var name = $('#contact_name_field').attr('value');
				var from = $('#contact_email_field').attr('value');
				var comments = $('#contact_comments_field').attr('value');
				
				var dataString = "name=" + name + "&from=" + from + "&comments=" + comments;
				
				$.ajax(
					{
						type: "POST",
						url: "contact.php",
						data: dataString,
						success: function(){
							$('#contact-form').html("<div id='message'></div>");
							$('#message').addClass('message');
        					$('#message').html("Thanks, <em style='text-transform: capitalize;'>" + name + "</em>. Your details have been submitted.<br />Good luck!")
        					.hide()
        					.fadeIn(1500);
						}
					}
				)
			}
						
		});
	
	});
	
	function initialize() {
	
		if (typeof google=='undefined') {
			/* custom functions to alert the user to the error */
			return 0; 
    	}
	
    	var latlng = new google.maps.LatLng(51.518557, -0.141814);
    	var myOptions = {
      		zoom: 15,
      		center: latlng,
     		mapTypeId: google.maps.MapTypeId.ROADMAP,
     		disableDefaultUI: true,
     		streetViewControl: false
    	};
    	var map = new google.maps.Map(document.getElementById("map"),
        myOptions);
        var image = "img/map_marker.png";
        var marker = new google.maps.Marker({
      		position: latlng,
      		title:"Tangent On Demand",
      		icon: image
  		});
  		marker.setMap(map);
  	};
	
	initialize();
	
});
