$(document).ready(function () {
	
	if(location.hash.replace(/#/, '')) scrollTo(location.hash);
	
	jQuery('input').bind({
    	focus: function() {
			if($(this).val() == $(this).attr("title")){
				$(this).val('');
			}
			$(this).addClass("highlighted");
    	},
    	blur: function() {
			if($(this).val() == ''){
				$(this).val($(this).attr("title"));
			}
			$(this).removeClass("highlighted");
    	},
	});
	
	jQuery('textarea').bind({
    	focus: function() {
			if($(this).val() == $(this).attr("title")){
				$(this).val('').addClass("highlighted");
			}
    	},
    	blur: function() {
			if($(this).val() == ''){
				$(this).val($(this).attr("title")).removeClass("highlighted");
			}
    	},
	});

	
	$('a[href*=#]').live("click", function() {
		if (filterPath(location.pathname) == filterPath(this.pathname) && location.hostname == this.hostname && this.hash.replace(/#/,'') ) {
			scrollTo(this.hash);
			return false;
		}
	});
     

	
	$('select[name*=contact_type]').live("change", function() {
		if($(this).val() == "quote") {
			$("input.quote, select.quote, textarea.quote").fadeIn().removeClass("hide");
		} else {
			$("input.quote, select.quote, textarea.quote").fadeOut().addClass("hide");
		}
		return false;
	});
     
    var offset = $("#left").offset(); 
    var topPadding = 100; 
    $(window).scroll(function() {
    	var targetOffset = $(window).scrollTop() - offset.top + topPadding;
		$("#left").stop().animate({
			marginTop: targetOffset
		},500);
		$('div.content').each(function(index) {
			if($('div.content').eq(index+1).offset()){
				var next = $('div.content').eq(index+1).offset();
				var curr = $(this).offset();
				if(targetOffset >= curr.top-250 && targetOffset < next.top){
					$("#left li a").removeClass('active');
					$("#left li a."+$('div.content').eq(index).attr('id')).addClass('active');
				}
			}else{
				var curr = $(this).offset();
		    	if(targetOffset >= curr.top-250){
					$("#left li a").removeClass('active');
					$("#left li a."+$('div.content').eq(index).attr('id')).addClass('active');
		    	}
			}
		});
	});
});

function scrollTo(hash){
	var $targetId = $(hash), $targetAnchor = $('[name=' + hash.slice(1) +']');
	var $target = $targetId.length ? $targetId : $targetAnchor.length ? $targetAnchor : false;
	if ($target) {
		var targetOffset = $target.offset().top-100;
		var $c = $(this);
	}
	$('html, body').animate({scrollTop: targetOffset}, 1000, 'easeInOutExpo');
}

function filterPath(string) {
	return string
	.replace(/^\//,'')
	.replace(/(index|default).[a-zA-Z]{3,4}$/,'')
	.replace(/\/$/,'');
}

