/**
 * @author ajeffrey
 * 
 * Requires jQuery 1.3 or above
 */
//$.namespace("Prosim");
Prosim = {};
Prosim.CallRequestForm = function(){
	$("#contact-info p.request").after('<div id="request-form"></div>');
	var formContainer = document.getElementById("request-form");

	function addLinkEvents(){
		$("#contact-info p.request a").bind("click",function(){
			loadForm($(this).attr("href"));
			return false;
		});
	}

	function showForm(){
		$(formContainer).animate({left:"481px"},500);
	}

	function hideForm(){
		$(formContainer).animate({left:"833px"},500,function(){
			$(this).empty();
		});
	}

	function addFormEvents(){
		var form = $(formContainer).find("form.request-call").get(0);
		if (form){
			// Submit Button
			$(form).bind("submit",function(){
				submitForm(form);
				return false;
			});
	
			// Cancel Button
			$(form).find("input#cancel").bind("click",function(){
				hideForm();
				return false;
			});
		}

		// Close Button
		$(formContainer).find("a.close").bind("click",function(){
			hideForm();
			return false;
		});
	}

	function loadForm(formUrl){
		$.ajax({
			type: "GET",
			data: "useAjax=yes",
			url: formUrl,
			dataType: "html",
			beforeSend: function(){
			},
			success: function(data,textStatus){
				$(formContainer).append(data);
				addFormEvents();
				showForm();
			},
			error: function(data,textStatus){
			}
		});
	}

	function submitForm(form){
		$.ajax({
			type: "POST",
			data: $(form).serialize() + "&useAjax=yes",
			url: $(form).attr("action"),
			dataType: "html",
			beforeSend: function(){
			},
			success: function(data,textStatus){
				$(formContainer).fadeOut(500,function(){
					$(this).empty();
					$(this).append(data);
					addFormEvents();
					$(this).fadeIn(500);
				});
			},
			error: function(data,textStatus){
			}
		});
	}

	function init(){
		addLinkEvents();
	}

	init();
};
Prosim.ExternalLinkHandler = new (function(){
	$(function(){
		$("a[rel='external']").attr("target","blank");
	});
});

$(document).ready(function(){
	Prosim.CallRequestForm();
});