function replaceURLWithHTMLLinks(text) {
  var exp = /(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig;
  return text.replace(exp,"<a href='$1'>$1</a>"); 
}

trenchless = {
  common : {
    init     : function(){
    	//nav
    	
    	$("#nav-holder").append('<div id="box"></div>');
		
		//Retrieve the selected item position and width
		var default_left = Math.round($('#nav-holder li.selected').offset().left - $('#nav-holder').offset().left);
		var default_width = $('#nav-holder li.selected').width();

		//Set the floating bar position and width
		$('#box').css({left: default_left,width: default_width});
		//$('#box .head').css({width: default_width});

		//if mouseover the menu item
		$('#nav-holder li').hover(function () {
			
			//Get the position and width of the menu item
			left = Math.round($(this).offset().left - $('#nav-holder').offset().left);
			width = $(this).width(); 

			//Set the floating bar position, width and transition
			$('#box').stop(false, true).animate({left:left,width:width},{duration:200});	
		
		//if user click on the menu
		}).click(function () {
			
			//reset the selected item
			$('#nav-holder li').removeClass('selected');	
			
			//select the current item
			$(this).addClass('selected');
	
		});
		
		//If the mouse leave the menu, reset the floating bar to the selected item
		$('#nav-holder').mouseleave(function () {

			//Retrieve the selected item position and width
			default_left = Math.round($('#nav-holder li.selected').offset().left - $('#nav-holder').offset().left);
			default_width = $('#nav-holder li.selected').width();
			
			//Set the floating bar position, width and transition
			$('#box').stop(false, true).animate({left: default_left,width:default_width},{duration:200});
			
		});
    	
    	//bring in latest tweet		
		$.ajax({
			url:'http://twitter.com/status/user_timeline/trenchlessuk.json?count=1&callback=?',
			dataType: 'json',
			success: function(data) {
				$.each(data, function(i, item) {
					var tweet = item.text;
				  	tweetWithLinks = replaceURLWithHTMLLinks(tweet);
				  	$("#tweetContent").html(tweetWithLinks);
				  	var escapedTweet = escape(tweet.replace(/ /g,"+"));
					$("#btnTweetThis").attr("href", 'http://twitter.com/home?status='+escapedTweet);
				});
			},
			error: function(){
				$("#latestTweet div.tweet").html('Error retrieving tweet. please visit <a href="http://www.twitter.com/trenchlessuk">http://www.twitter.com/trenchlessuk</a> for the latest competition tweet.');
				$("#tweetButton").hide();
			}
		});
    }
  },
  "contact-us" : {
  	init     : function(){
  		$("input[type=text],textarea").each(function(){
  			$(this).prev().hide();
  			$(this).attr('placeholder', $(this).prev().html());
  		});
  		$('input[placeholder],textarea[placeholder]').placeholder();
  	}
  }
}

UTIL = {

	fire : function(func,funcname, args){
		var namespace = trenchless;
		funcname = (funcname === undefined) ? 'init' : funcname;
		if (func !== '' && namespace[func] && typeof namespace[func][funcname] == 'function'){
			namespace[func][funcname](args);
		} 

	}, 

	loadEvents : function(){
		var bodyId = document.body.id;
		// hit up common first.
		UTIL.fire('common');
		// do all the classes too.
		$.each(document.body.className.split(/\s+/),function(i,classnm){
			UTIL.fire(classnm);
			UTIL.fire(classnm,bodyId);
		});
	}
}; 

$(document).ready(UTIL.loadEvents);


