
jQuery(function($){
	$('#collapse #collapse_close').click(
		function(){
			$("#collapse").hide("slow");
			return(false);
		}
	);
	
	$("#send_small,#contactMember").click(prompt_contact);

	$(".modal #cancel").click(
		function(){
			tb_remove();
		}
	);
	
	$("#printer_small").click(
		function(){
			var cvName=location.href.replace("http://","").split("/")[1].split("?")[0];
			window.print();
			var url="/"+cvName+"/print/"+location.search;
			$.getJSON(url);
			return(false);
		}
	);

	//Initialise the document.
	$(document).ready(
		function(){
			$(this).oneTime(8000, function() {
				$("#collapse").hide("slow");
			});
		
			var contactAction="/cv/display/contact/json/";
			$(".modal #contact").each(
				function(i){
					if($(this).is("form")){
						contactAction=this.action;
					}
				}
			);

			var options={
				beforeSubmit:function(formData, jqForm, options){
					//alert("Errors: "+contact_v.errors.length);
					if(contact_v.errors.length>0){
						//alert("Why Submit then??");
						return(false);
					}else{
						tb_showLoader("Please wait: sending message...");
						return(true);
					}
				},
				success:function(data){
					//alert("Hmmm:"+data.result);
					var opts={
						title:"message sent.",
						message:"<p>Your message was successfully sent.</p>",
						show:true,
						id:"message_result",
						timeout:8500,
						theme:"green"
					};

					if(data.result!=1){
						opts.title="error sending message";
						opts.message="<p>There were errors generated while trying to send that message.</p><p>Please try again later.</p>";
						opts.theme="red";
					}
					
					tb_hideLoader();
					
					showMessage(".container_row #webCV",opts);
				},
				error:function(){
					tb_hideLoader(
						function(){
							tb_show("contact form","#TB_inline?height=450&amp;width=450&amp;inlineId=modalContactForm&amp;titleBar=true&amp;modal=true",null);
							alert("There was an error trying to send that message.\n\nPlease try again or cancel.");
						}
					);
				},
				url:contactAction+"json/",
				dataType:"json"
			};
			$(".modal #contact").ajaxForm(options);
			
			contact_v.init();
		}
	);
});

function prompt_contact(){
	tb_show("contact form","#TB_inline?height=450&amp;width=450&amp;inlineId=modalContactForm&amp;titleBar=true&amp;modal=true",null);
	return(false);
}