/*
 | Main javascript file for metacue.com
 | (c) 2008-2011 David Södermark
 */
jQuery(document).ready(function(){

	// Lightbox för Referenser.
	jQuery("#list_referenser a").lightBox();

	// Dim some images.
	jQuery("img.ref_firstpage").css("opacity", ".8").css("filter", "alpha(opacity=80)");
	jQuery("a.waxbook_logo img").css("opacity", ".6").css("filter", "alpha(opacity=60)");

	// Highlight waxbook logo image on hover.
	jQuery("a.waxbook_logo img").hover(function(){
			jQuery(this).animate({"opacity": "1"}, {"duration": 200, "queue": false}).css("filter", "alpha(opacity=100)");
		}, function(){
			jQuery(this).animate({"opacity": ".6"}, {"duration": 200, "queue": false}).css("filter", "alpha(opacity=60)");
	});

	// Spamskydd för e-post på sidan Företaget.
	var mailstr = jQuery(".epost").html();
	if (mailstr != null){
		var mailarray = mailstr.split(" ");
		jQuery(".epost").html(
			"<a href=\"mailto:" + mailarray[0] + "@" + mailarray[2] + "." + mailarray[4] + "\">" +
			mailarray[0] + "@" + mailarray[2] + "." + mailarray[4] + "</a>"
		);
	}

	// Contact form.
	jQuery("#progress").append("<img id=\"loading\" src=\"images/ajax-loader.gif\" alt=\"loading...\" />").hide();
	jQuery("#formmessage").hide();
	jQuery("#changeimage").click(function(){
		changeImage();
	});
	jQuery("#contactform").ajaxForm({
		dataType:	"json",
		beforeSubmit:	function(){
							// Set size for #progress and #formmessage equal to #contactform.
							var contactFormWidth = jQuery("#contactform").width();
							var contactFormHeight = jQuery("#contactform").height();
							jQuery("#progress").css("height",contactFormHeight+"px");
							jQuery("#progress").css("width",contactFormWidth+"px");
							jQuery("#formmessage").css("height",contactFormHeight+"px");
							jQuery("#formmessage").css("width",contactFormWidth+"px");

							// Set position for animated "loading..."-gif.
							var loadingPositionTop = parseInt((contactFormHeight / 2) - 8);
							var loadingPositionLeft = parseInt((contactFormWidth / 2) - 8);
							jQuery("#loading").css("margin-top",loadingPositionTop+"px");
							jQuery("#loading").css("margin-left",loadingPositionLeft+"px");

							// Fade out contactform and show "loading..."-gif.
							jQuery("#contactform").fadeOut(400, function(){
								jQuery("#progress").show();
							});
						},
		success:	function(data,success,matchedset){
						var error = data["error"];
						if(error == ""){
							jQuery("#formmessage").
								append("<div id=\"messagesent\">"+data["message"]+"</div>");
						} else {
							jQuery("#formmessage").
								append("<div id=\"messagesent\" style=\"color:#900\">" +
									"Följande fel uppstod: "+error+"</div>");
						}
						jQuery("#formmessage").append(
							"<div id=\"sendagain\"><a href=\"javascript:removeFormMessage();\">" +
							"Visa formuläret igen</a></div>");

						// Make sure the loading-gif is visible before showing result message.
						jQuery("#progress").delay(1000).fadeOut(1, function(){
							jQuery("#formmessage").fadeIn();
						});
					}
	});

	// Drop in effect.
	jQuery("#list_tjanster li").hide().delay(1000).dropIn({selection:"#list_tjanster li"});;
	jQuery("#list_innovation li").hide().delay(1000).dropIn({selection:"#list_innovation li"});
	jQuery("#list_referenser li").hide().delay(1000).dropIn({selection:"#list_referenser li"});
});


/*
 | changeImage
 | Changes captcha image in the contact form.
 */
function changeImage(){
	jQuery("#captcha").attr("src","securimage/securimage_show.php?" + Math.random());
}

/*
 | removeFormMessage
 | Removes contact form result message and makes a new contact form visible.
 */
function removeFormMessage() {
	jQuery("#formmessage").fadeOut(400, function(){
		jQuery("#messagesent").remove();
		jQuery("#sendagain").remove();
		jQuery("#contactform").resetForm();
		changeImage();
		jQuery("#contactform").fadeIn();
	});
}

