(function($){ 
$.fn.ajaxify = function(options) {
var defaults = {  
		event:'click',
		link:false,
		target:'body',
		method: 'POST',
		tagToload:false,
		loading_image:'./images/loading.gif',
		loadHash:false,
		params:'ajax=true',
		timeout:false,
		contentType:"application/x-www-form-urlencoded",
		dataType:'html',
		cache:false,
		username:false,
		password:false,
		onStart:function(){},
		onerror:function(){},
		onSuccess:function(){},
		onComplete:function(){}
	};
	var current = $.extend(defaults, options);
	return this.each(function() {
		$(this).ajaxify_loadHash(current);
			$(this).bind(current.event,function(){
			current.link = $(this).attr('href').replace(/^#/, "") || current.link;
			var tempWhere = $(this).attr('target').split(',');
			current.where = tempWhere[0] || current.target;
			current.tagToload = tempWhere[1] || current.tagToload; 
			$(this).ajaxify_load(current);
			return false;
			});
	});
};
$.fn.ajaxify_load = function(current) {
	$.ajaxSetup({global:false});
	$.ajax({
		  type: current.method,
		  url: current.link,
		  dataType: current.dataType,
		  data: current.params,
		  contentType:current.contentType,
		  timeout:current.timeout,
		  cache:current.cache,
		  username:current.username,
		  password:current.password,
		  error:current.onerror,
		  complete: current.onComplete,
		  beforeSend: current.onStart,
		  success: function(data){
		  var tmpData = data;
		  
		  var parent_tag = $(current.where).parent();
		  parent_tag.css("height",parent_tag.height());
		  $(current.where).fadeOut(300);
		  setTimeout(function(){
			  if(current.tagToload){
				$(current.where).html('<div id="temp" style="display:none">'+data+'</div>');			
				data = $('#temp').find(current.tagToload);
				$('#temp').remove();
			  }
			  $(current.where).html(data);
			  current.onSuccess();
			  $(current.where).fadeIn(300);
			  parent_tag.css("height",'');
							  },300);
		  }
		});
	if(current.loadHash)
		location.hash = current.link;		
	else if(location.hash.length > 1){
	document.location.hash = "#";
	return false; 
	}
};
$.fn.ajaxify_loadHash = function(current){
	if(current.loadHash && location.hash.replace(/^#/, "")){
		current.where = current.target;
		current.link = location.hash.replace(/^#/, "");
		$(current.target).ajaxify_load(current);
		return true;
	}else
		return false;
};
})(jQuery);
