//<![CDATA[

function alignSubmenu()
{
	var nav = new Object();
	nav.offset = $("#nav").offset();
	nav.width = $("#nav").outerWidth();
	nav.combined = nav.offset.left + nav.width;
	
	$("#nav ul ul").each(function(){
		var elm = $(this);
		var ul = new Object();
		if($(this).css("display") == "block") {
			ul.offset = $(this).offset();
			ul.width = $(this).outerWidth();
			ul.combined = ul.offset.left + ul.width;
			
			if(nav.combined < ul.combined) {
				var diff = ul.combined - nav.combined;
				$(this).css({'margin-left': '-'+diff+'px'});
			}
		}
	});
}

$(function(){
	// IE6 annoyances
	//$(document).pngFix( );
	// Pre-load images
	//$.preloadCssImages();
	
	// Navigation
	$("#nav li").hover(
		function(){
			$(this).addClass("hover");
			alignSubmenu();
		},
		function(){
			$(this).removeClass("hover");
		}
	);
	
	$("#sidenav li:last").css({"border-bottom":"none"});
	$("#sidenav li:first").css({"border-top":"none"});
	
	$("#contact_form input, #contact_form textarea").each(function() {
	
		if( $(this).val() == '' || $(this).val() == $(this).attr("title") ) {
			$(this).val($(this).attr("title")).addClass("blank");
		} else {
			$(this).removeClass("blank");
		}
		
		if( ! $(this).hasClass("submit") ) {
			
			$(this).focus(function(){
				if( $(this).val() == $(this).attr("title") ) {
					$(this).val('');
					$(this).removeClass("blank");
				}
			})
			.blur(function(){
				if( $(this).val() == $(this).attr("title") || $(this).val() == '' ) {
					$(this).val($(this).attr("title")).addClass("blank");
				}
			});
			
		}
		
	});
	
	if( $("#sidemenu").children().size() < 1 ) { $("#sidemenu").remove(); }
	
});


$(function() {
	
	$('#contact .submit').click(function () {
	
		$('#ajax_message').removeClass("error");
		$('#ajax_message').removeClass("success");
		$('#ajax_message').html('').slideUp();
		
		//Get the data from all the fields
		var fields = [];
		$("#contact_form form input, #contact_form form textarea").each(function(){
			if( !$(this).hasClass("submit") ) {
				val = $(this).val() == $(this).attr("title") ? '' : $(this).val();
				if($(this).attr("name") == "message") {
					val = encodeURIComponent(val); 
				}
				fields[ $(this).attr("name") ] = val;
			}
		});
		
		//alert(fields['name']);
		var get = ''
		for ( var n in fields ) {
			get += "&"+ n + "=" + fields[n]; 
		}

		//$('form input').attr('disabled', 'true');
		$('.submit').fadeOut();
		$('#loading').fadeIn();
		
		$.ajax({
			url: "/submit",	
			type: "POST",
			data: get,		
			cache: false,
			success: function (rsp) {
				rsp = $.evalJSON(rsp);
				if(rsp.status == false) {
					$('#ajax_message').addClass("error");
				} else {
					$('#ajax_message').addClass("success");
				}
				
				$('#ajax_message').html(rsp.message).slideDown();
				$('#loading').hide();
				$('.submit').fadeIn();
			}
						
		});
		
		return false;
	});	
});	


// FAQ
var id;
$(function(){
	var anchors = '<ul>';
	var i = 0;
	$(".faq dt").each(function(){
		i++;
		var id = "dt_"+i;
		$(this).attr("id", id);
		anchors += '<li><a href="#' + id + '">' + $(this).html() + '</a></li>';
		//$(this).html('<a id="'+ rtrim($(this).text())+'">'+$(this).html()+'</a>');
	});
	anchors += '</ul>';
	$(".faq dl").before(anchors);

	// Animated scroll
	
	$(".faq ul li a").click(function(e){
		if ( !($.browser.msie && $.browser.version <= 7) ) {
			e.preventDefault();
			id = $(this).attr("href").substring(1);
			goToByScroll(id);
		}
	});

});
function goToByScroll(id){
	var top = $("dt[id=" + id + "]").offset().top;
 	$('html, body').animate({scrollTop: top-20}, top-(top/2), blink);
}

function blink()
{
	var elm = $("dt[id='" + id + "']");
	var col = elm.css('color');
	elm.addClass('highlight')
	elm.animate({opacity:1}, 600, function(){ elm.removeClass('highlight'); });
}

function rtrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}
//]]>