$(function() {	
	$("body").prepend('<div id="toolbar"></div>');
	var toolbarOpen = false;
	var currentTab = "";
	$('a[rel=ajax]').click(function(e) {
	if(toolbarOpen == false)
	{
		if(e.target == currentTab)
		{
			$("#toolbar").slideToggle(function() {
				$(currentTab).addClass("active");
			});
		}
		else
		{		
			currentTab = e.target;
			$("#toolbar").load($(this).attr("href"), function () {
				$('a[class=close]').click(function(e) {
					$("ul#sub-navigation a").removeClass("active");
					$("#toolbar").slideToggle();
					toolbarOpen = false;
					return false;
				});
				$("#toolbar").slideToggle(function() {
					$(currentTab).addClass("active");
				});
				return labelReplacement();
			});
		}
		toolbarOpen = true;
	}
	else if(toolbarOpen == true && e.target == currentTab)
	{
		$("#toolbar").slideToggle();
		$(currentTab).removeClass("active");
		toolbarOpen = false;
	}
	else if(toolbarOpen == true && e.target != currentTab)
	{
		
		$("#toolbar").slideToggle(function () {
			$("#toolbar").load($(e.target).attr("href"), function () {
				$('a[class=close]').click(function(e) {
					$("ul#sub-navigation a").removeClass("active");
					$("#toolbar").slideToggle();
					toolbarOpen = false;
					return false;
				});
				return labelReplacement();
			});	
			$(currentTab).removeClass("active");
			currentTab = e.target;
		});
		
		$("#toolbar").slideToggle(function() {
			$(currentTab).addClass("active");
		});
		
		toolbarOpen = true;
	}
	return false;
	});
	  $('#nav-work').not('.active').css( {backgroundPosition: "0 0"} ) .mouseover(function(){ $(this).stop().animate({backgroundPosition:"(0 -17px)"}, {duration:450, easing: "easeInOutBack"}) })
    .mouseout(function(){ $(this).stop().animate({backgroundPosition:"(0 0)"}, {duration:450, easing: "easeOutBack"}) })
    
    $('#nav-clients').not('.active').css( {backgroundPosition: "-56px 0px"} ) .mouseover(function(){ $(this).stop().animate({backgroundPosition:"(-56px -17px)"}, {duration:450, easing: "easeInOutBack"}) })
    .mouseout(function(){ $(this).stop().animate({backgroundPosition:"(-56px 0)"}, {duration:450, easing: "easeOutBack"}) })
    
    $('#nav-process').not('.active').css( {backgroundPosition: "-139px 0px"} ) .mouseover(function(){ $(this).stop().animate({backgroundPosition:"(-139px -17px)"}, {duration:450, easing: "easeInOutBack"}) })
    .mouseout(function(){ $(this).stop().animate({backgroundPosition:"(-139px 0)"}, {duration:450, easing: "easeOutBack"}) })

    $('#nav-services').not('.active').css( {backgroundPosition: "-233px 0px"} ) .mouseover(function(){ $(this).stop().animate({backgroundPosition:"(-233px -17px)"}, {duration:450, easing: "easeInOutBack"}) })
    .mouseout(function(){ $(this).stop().animate({backgroundPosition:"(-233px 0)"}, {duration:450, easing: "easeOutBack"}) })

    $('#nav-blog').not('.active').css( {backgroundPosition: "-329px 0px"} ) .mouseover(function(){ $(this).stop().animate({backgroundPosition:"(-329px -17px)"}, {duration:450, easing: "easeInOutBack"}) })
    .mouseout(function(){ $(this).stop().animate({backgroundPosition:"(-329px 0)"}, {duration:450, easing: "easeOutBack"}) })

    $('#nav-about-us').not('.active').css( {backgroundPosition: "-392px 0px"} ) .mouseover(function(){ $(this).stop().animate({backgroundPosition:"(-392px -17px)"}, {duration:450, easing: "easeInOutBack"}) })
    .mouseout(function(){ $(this).stop().animate({backgroundPosition:"(-392px 0)"}, {duration:450, easing: "easeOutBack"}) })    
    
	$('ul.featured-work li').hover(function(){
		$(this).find('img').fadeOut('normal');
		//$(this).find('img').animate({top:'182px'},{queue:false,duration:500, easing: "easeInOutBack"});
	}, function(){
		$(this).find('img').fadeIn('slow');
		//$(this).find('img').animate({top:'0px'},{queue:false,duration:500, easing: "easeInOutBack"});
	});
	
	$("a").each(
			function ()
			{
				if(this.rel=="external")
				{
					this.target="_blank";
				}
			}
		);
    $('#downloads #desktop ul li').hover(function() {
      $(this).children().addClass('hovered');
    }, function() {
      $(this).children().removeClass('hovered');
    }
   );
});

function labelReplacement(){
	var md = {};

	md.click_label = function (match_class) {
	   jQuery('label.' + match_class).each(function () {
	       jQuery(this).hide();
	       jQuery('input#' + jQuery(this).attr("for")).val(jQuery(this).text());
	   });
	   jQuery('input.' + match_class).focus(function () {
	       jQuery(this).val() == jQuery('label[for="' + jQuery(this).attr('id') + '"]').text() ? jQuery(this).val('') : '';
	   });
	   jQuery('input.' + match_class).blur(function () {
	       jQuery(this).val() == '' ? jQuery(this).val(jQuery('label[for="' + jQuery(this).attr('id') + '"]').text()) : '';
	   });
	}
	jQuery(function () {
	   md.click_label('text-input');
	});
}


$(function(){
	 $(this).nextAll().css("display","none");
	$("#blog #sidebar h4").click(function(){
		$(this).nextAll().toggle("slow");
		$(this).toggleClass("inactive");
	})
});	