$(document).ready(function(){

	if($("form.validate").length) {

		$("form.validate").live('submit', function() {

			var self = $(this);
			var alert = false;

			var msg = '';
			var valid = true;
			var email_pattern = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,4})+$/i;

			$(".alert", this).removeClass("alert");

			$(this).find(".required").each(function() {

				if($(this).hasClass("inputgroup")) {
					var value = $(":checked", this).length ? "Checked" : "";
				} else {
					var value = $(this).val();
				}

				if(value == "" || ($(this).prop("title") && value == $(this).prop("title")) || ($(this).is(".email") && !email_pattern.test(value))) {
					valid = false;
					var label = $(this).prev("label").text() || $(this).prop("title");
					msg += '\n-> '+label.replace(/[\*|:]/gi, "")+' is required';
					$(this).addClass("alert");
				}
			});

			if(!valid) {

				if(alert) {
					alert('Please fill in these fields. Thanks.'+msg);
				}

				$("#alert", this).show().css('visibility', 'visible');
				return false;
			}

			if($(".curtain", self).length) {

				var action = self.attr("action");

				var datastr = self.serialize() + "&form=" + self.attr("id");
				var query =  ((action.substr(0,3) == '../') ? '../' : '') + "ph"+"p_inclu"+"des/mai"+"ler"+"."+"php";

				$(".notice", self).hide();

				$.ajax({
					type: "POST",
					url: query,  
					data: datastr,
					dataType: "json",
					success: function(data){

						var type = data.type.toString();
						
						$(".curtain .result", self).html(data.msg);
						$(".notice." + type, self).show();

						if(data.type == 'success') {
							self.clearForm();
						}
					},
					complete: function(jqXHR, textStatus) {

						$(".curtain", self).fadeIn(function(){
							$(".alert", self).hide();
							$("#alert", self).hide();
						});
					}
				});

				$(".curtain a[href='#close']").live("click",function(){
					$(this).closest(".curtain").fadeOut();
					return false;
				});

				return false;

			}

		});

		$("form.validate input, form.validate textarea").bind("focus", function(){
			$(this).addClass("focus");
			if($(this).val() == $(this).attr("title")) {
				$(this).val("");
			}
		});

		$("form.validate input, form.validate textarea").bind("blur", function(){
			$(this).removeClass("focus");
			if($(this).val() == "") {
				$(this).val($(this).attr("title"));
			}
		});

		$("form.validate .submit, form.validate [href='#submit']").click(function(){
			var f = $(this).closest("form");
			f.submit();
			return false;
		});

		$("form.validate input.reset").click(function(){
			var f = $(this).closest("form");
			f.clearForm();
			return false;
		});

	}
	
	if($("#kwicks1").length) {

		$('#kwicks1').kwicks({  
			defaultKwick: 0,
			min: 57,
			event: 'click',
			sticky: true,
			duration: 1000
		});
	}
	
	if($('a[rel="colorbox"]').length) {
		$('a[rel="colorbox"]').colorbox({
			opacity: .3,
			top: "-10px"
		});
	}

	
	var locationstr = document.location.toString();

	if(locationstr.match('#')) {

		var fragment = locationstr.split('#')[1];

		if($("#"+fragment).length) {
			$("#"+fragment).click();
		}
	}

});

$.fn.clearForm = function() {
  return this.each(function() {
	var type = this.type, tag = this.tagName.toLowerCase();
	if (tag == 'form')
	  return $(':input',this).clearForm();
	if (type == 'text' || type == 'password' || tag == 'textarea') {
	  if($(this).attr('title')) {
		  this.value = $(this).attr('title');
	  } else {
		this.value = '';
	  }
	} else if (type == 'checkbox' || type == 'radio') {
	  this.checked = false;
	} else if (tag == 'select') {
	  this.selectedIndex = 0;
	}
  });
};
