(function($) {
	// Preloading images
	var cache = [];
	// Arguments are image paths relative to the current page.
	$.preLoadImages = function() {
		var args_len = arguments.length;
		for (var i = args_len; i--;) {
			var cacheImage = document.createElement('img');
			cacheImage.src = arguments[i];
			cache.push(cacheImage);
		}
	};
	
	// detect touch capability
	if(document.ontouchmove === undefined){
	    $.u_event = "click";
	}else{
	    $.u_event = "touchstart";
	}
	
	$.get_hash = function () {
		return window.location.hash.replace(/^.*#|\//g, '');
	};
	
	$.set_hash = function (h) {
		return window.location.hash = h;
	};
	
	$.article_offset = {};	
	$.body_scrollHeight;
		$.resize_article = function(el){
		var padding_top = el.attr("id") == "home" ? 140 : 90
		var padding_bottom = el.attr("id") == "home" ? 80 : 20
		el.css({padding:padding_top+"px 0 "+padding_bottom+"px 0",height:"auto"});
//		el.css({padding:padding_top+"px 0 20px 0",height:"auto"});
		var el_inner_height = el.height();
		var el_padding = parseInt(el.css("padding-top"))+parseInt(el.css("padding-bottom"));
		var el_height = el_inner_height + el_padding +1;
		var window_height = $(window).height();
		var el_final_height = window_height - el_padding;

		if(el[0].scrollHeight < window_height){
			
			if(el.attr("id") == "beta"){
				el_final_height = el_final_height-$("footer").last().outerHeight();
			}
			
			var el_final_padding = ((el_final_height-el_inner_height)/2)-padding_bottom;
			
			el_final_height -= el_final_padding+padding_bottom;
			el_final_padding = el_final_padding+el_padding;
						
			el.css({
				"height":el_final_height+"px",
				"padding-top":el_final_padding+"px"
			})
		}

		var dif = ($.body_scrollHeight-document.body.scrollHeight);
		var scrollTop=($(window).scrollTop()-dif)
		
		if (el.attr("id") == "beta") $(window).scrollTop(scrollTop)
		
		
		$.article_offset[el.attr("id")] = el.offset().top;
		$.body_scrollHeight = el.attr("id") == "beta" ? document.body.scrollHeight : $.body_scrollHeight;
	};
	
	$.resize_articles = function(){
		$("article").each(function(){
			$.resize_article($(this))
		});
	}
	
	$.validator.addMethod("checkbox_or_input", function(value, element) { 
		return ($(this.currentForm).find("input[type=checkbox]:checked").length) || value;
	});
	
	$.validator.addMethod("contact_question", function(value, element) { 
		return value && value.toLowerCase() == form_messages.question.contact_answer ;
	});
	
	$.validator.addMethod("beta_question", function(value, element) { 
		return value && value.toLowerCase() == form_messages.question.beta_answer ;
	});

	$.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')
	      this.value = '';
	    else if (type == 'checkbox' || type == 'radio')
	      this.checked = false;
	    else if (tag == 'select')
	      this.selectedIndex = -1;
	  });
	};
	
})(jQuery)
