var SITE_ROOT = '/';

$(document).ready(function() {
	//track stat with google analytics
	try {
		var pageTracker = _gat._getTracker("UA-10825451-1");
		pageTracker._trackPageview();
	} catch(err) {}
	
	//do not hold focus on the recently clicked link
	$("a").live("click", function() {
		$(this).blur();
	});
	
	//window behaviour
	$(".window").live("drag", {handle: ".window_title"}, function(e, dd) {
		$(this).css({
			top: dd.offsetY,
			left: dd.offsetX
		});
	});
	
	$(".window_contents").mousedown(function() {
		return false;
	});
	
	$(".close_window").live("click", function() {
		$(this).parent(".window").fadeOut("fast", function() {
			$(this).remove();
		});
	});
	
	//donate window
	var $donateWindow = null;
	$("#donate_btn").click(function(e) {
		//show the donate window if it is not already shown
		if (!$donateWindow) {
			$donateWindow = $('<div id="donate_window" class="window"><div class="window_title"></div><div class="close_window"></div></div>');
			var $windowContents = $('<div class="window_contents"></div>');
			$donateWindow.append($windowContents);
			
			$windowContents.html(['<p>All donations will contribute towards hosting costs for the website and development of future projects.</p>',
			'<div class="center">',
			'	<h4>Donate via Paypal</h4>',
			'	<form action="https://www.paypal.com/cgi-bin/webscr" method="post">',
			'		<input type="hidden" name="cmd" value="_s-xclick" />',
			'		<input type="hidden" name="hosted_button_id" value="BUJNMTYSBX7JG" />',
			'		<input type="image" src="https://www.paypalobjects.com/en_GB/i/btn/btn_donate_SM.gif" border="0" name="submit" alt="Donate" />',
			'		<img alt="" border="0" src="https://www.paypalobjects.com/en_GB/i/scr/pixel.gif" width="1" height="1" />',
			'	</form>',
			'</div>'].join(''));
		}
		
		$(document.body).append($donateWindow);
		$donateWindow.css({left: e.pageX - 300, top: e.pageY - 300 });
		$donateWindow.fadeIn("fast");
	});
	
	//contact window
	var $messageWindow = null;
	function loadMessageWindow(x, y, subject) {
		if (subject===undefined) subject = "";
		
		if (!$messageWindow) {
			$messageWindow = $('<div id="message_window" class="window"><div class="window_title"><h4>Send a message</h4></div><div class="close_window"></div></div>');
			var $windowContents = $('<div class="window_contents"></div>');
			$messageWindow.append($windowContents);
			
			$windowContents.html(['<form id="message_form">',
			'<div class="form_pair"><label for="message_name">Name</label>',
			'<input type="text" id="message_name" class="text required" value="" /></div>',
			'<div class="form_pair"><label for="message_email">Email</label>',
			'<input type="text" id="message_email" class="text required email" value="" /></div>',
			'<div class="form_pair"><label for="message_subject">Subject</label>',
			'<input type="text" id="message_subject" class="text required" value="" /></div>',
			'<div class="form_pair"><label for="message_text">Message</label>',
			'<textarea id="message_text" class="required"></textarea></div>',
			'<div class="form_pair"><input id="ajax_send_message" class="submit" type="submit" value="Send Message" />',
			'<div class="response" id="message_response"></div></div>',
			'</form>'].join(''));
		}
		
		$(document.body).append($messageWindow);
		$messageWindow.find("#message_subject").val(subject);
		$messageWindow.css({left: x, top: y});
		$messageWindow.fadeIn("fast");
		
		$("#message_form").validate({
			submitHandler: function() {}
		});
	}
	
	$("#sendmessage_btn").click(function(e) {
		loadMessageWindow(e.pageX - 300, e.pageY - 300);
	});
	
	$("#company_enquire_button").click(function(e) {
		loadMessageWindow(e.pageX+100, e.pageY-200, "Business website");
	});
	
	$("#custom_enquire_button").click(function(e) {
		loadMessageWindow(e.pageX+100, e.pageY-200, "Custom website");
	});
	
	//comment window
	var $commentWindow = null;
	$("#addcomment_btn").click(function(e) {
		if (!$commentWindow) {
			$commentWindow = $('<div id="comment_window" class="window"><div class="window_title"><h4>Add a comment</h4></div><div class="close_window"></div></div>');
			var $windowContents = $('<div class="window_contents"></div>');
			$commentWindow.append($windowContents);
			
			$windowContents.html(['<form id="comment_form">',
			'<div class="form_pair"><label for="comment_name">Name</label>',
			'<input type="text" id="comment_name" class="text required" value="" /></div>',
			'<div class="form_pair"><label for="comment_text">Comment</label>',
			'<textarea id="comment_text" class="text required"></textarea></div>',
			'<div class="form_pair"><input id="ajax_send_comment" class="submit" type="submit" value="Post Comment" />',
			'<div class="response" id="comment_response"></div></div>',
			'</form>'].join(''));
		}
		
		$(document.body).append($commentWindow);
		$commentWindow.css({left: e.pageX + 100, top: e.pageY - 100 });
		$commentWindow.fadeIn("fast");
		
		$("#comment_form").validate({
			submitHandler: function() {}
		});
	});
	
	//ajax
	$("#ajax_send_message").live("click", function() {
		if (!$("#message_form").valid()) return;
		
		$.ajax({
			url: SITE_ROOT + "ajax/sendmessage.php",
			type: "POST",
			cache: false,
			dataType: "json",
			data: {
				name: $("#message_name").val(),
				email: $("#message_email").val(),
				subject: $("#message_subject").val(),
				message: $("#message_text").val()
			},
			error: function() {
				$("#message_response").removeClass("valid loading").text("Error, please try again later.");
			},
			success: function(reply) {
				if (reply.result) {
					$("#message_subject").val("");
					$("#message_text").val("");
					$("#message_response").addClass("valid");
				} else {
					$("#message_response").removeClass("valid");
				}
				
				$("#message_response").removeClass("loading").text(reply.data);
			}
		});
		
		$("#message_response").addClass("loading");
	});
	
	$("#ajax_send_comment").live("click", function() {
		if (!$("#comment_form").valid()) return;
		if (typeof projid != "string") return;
		
		$.ajax({
			url: SITE_ROOT + "ajax/addcomment.php",
			type: "POST",
			cache: false,
			dataType: "json",
			data: {
				proj: projid,
				name: $("#comment_name").val(),
				comment: $("#comment_text").val()
			},
			error: function() {
				$("#comment_response").removeClass("valid loading").text("Error, please try again later.");
			},
			success: function(reply) {
				if (reply.result) {
					$("#comment_text").val("");
					$("#comment_response").addClass("valid");
				} else {
					$("#comment_response").removeClass("valid");
				}
				
				$("#comment_response").removeClass("loading").text(reply.data);
			}
		});
		
		$("#comment_response").addClass("loading");
	});
});
