jQuery.myEngine = {
		
	/**
	 * Ajax Links loader
	 */
	ajaxLinksInit : function(query, rel, handle) {
		var handle = handle || function (hash){};
    	 
		$.historyInit(function(hash) {
			var hash = hash || location.pathname;
			
			if(typeof(query) == 'object') {
				for(i in query) {
					if($(query[i])[0]) {
						currentQuery = query[i];
					}
				}
			}
			
			$(currentQuery).animate({ 
		        opacity: 0.1
		      }, 600, false, function(){
				$(currentQuery).load(hash, null, function(){
					$(currentQuery).animate({ 
				        opacity: 1
				    }, 600);
					handle(hash);
				});				
			});
			
		});
		
		$("a[rel='" + rel + "']").live('click', function(e) {
			if(e.button==0) {			
				var hash = this.href;
				hash = hash.replace(/%20/gi, '+');
				hash = hash.replace(/^.*#/, '');
				hash = hash.replace(location.protocol + '//' + location.host, '');
				$.historyLoad(hash);
				return false;
			}
		});
	}	
};

jQuery.fn.myEngineConfirm = function() {	
	return this.die('click').live('click', function(e){
		if(e.button==0) {
			e.preventDefault();
			if(confirm($(this).attr('msg'))) {
				document.location = this.href;
			}
		}
	});
};

jQuery.fn.myEngineDisplay = function() {
	this.each(function(){
		var id = this.href.replace(/^.*#/, '#');
		var obj = $(id);
		$(id).css('display', 'none');
    });

	return this.die('click').live('click', function(e){
		if(e.button==0) {
			var id = this.href.replace(/^.*#/, '#');
			var obj = $(id); 
			e.preventDefault();
			if(obj.css('display') == 'none') {
				obj.css('opacity', 0);
				obj.show();
				obj.animate({ 
			        opacity: 1
			    }, 600);
			} else {
				obj.animate({ 
			        opacity: 0
			    }, 600, false, function() {
			    	obj.hide();
			    });			
			}
		}
	});
};

jQuery.fn.myEngineWindow = function(handle) {	
	return this.die('click').live('click', function(e){
		if(e.button==0) {
			e.preventDefault();
			var div = $('<div id="window">');
			var link = $(this);
			
			var url = this.href;
			div.load(url, null, function () {
				$(div).dialog({
					closeOnEscape:false,
					open: function(event, ui) {
						handle();
					},
					close: function(){
						$(div).html(null);
						
					},
					dialogClass: 'overlay',
					width: parseInt(link.attr('width')) || 600,
					minHeight: 30,
					maxHeight: parseInt(link.attr('height')) || 300,
					bgiframe: true,
					draggable: false,
					resizable: false,
					modal: true
				});			
			});
			
		}
	});
};

jQuery.fn.myEngineSort = function(url, itemsClass) {
	$(this).sortable({
		stop: function(event, ui) {
			var index = 1, result = "[";
			$("."+itemsClass).each(function(){
				if(index>1) {
					result += ", "; 
				}
				result += '{"id":"' + this.id + '", "sequence":"' + index + '"}';
				index++;
			}); 
			result += "]";
			$.ajax({
				type: 'POST',
				url: url,
				data: { 
					data: result
				} 
			});				
		}		
	});
	$(this).disableSelection();
};

jQuery.fn.myEngineAutocomplete = function(url, options) {
	this.each(function(){
		$(this).autocomplete(url || $(this).attr('url'), options || {
			dataType:'json',
			parse:function(data) {
				var rows = new Array();
				for(var i=0;i<data.length;i++){
					rows[i] = {data:data[i], value:data[i].value, result:data[i].value};
				}	        
				return rows;
	    	},
	    	formatItem:function(row, i, n) {
	    		return row.text;
	    	},
	    	max:100,
	    	mustMatch:false
		});
	});	
};

jQuery.fn.myEngineForm = function(handle) {
	var handle = handle || function(){};
	return this.live('submit', function(e){
    	var formId = this.id;
		$("#" + formId + " dd").each(function(){
			var id = this.id.replace('-element', '');
        	if ($("dl #" + id + "-errors").get(0)) {
            	$("dl #" + id + "-errors").html(null);
        	} else {
        		$(this).after($('<div id="' + id + '-errors">'));
        	}
		});
		var id = this.id;
		$('.ui-dialog-title').addClass('loadAnimation');
		$(this).ajaxSubmit({
			url: this.action + '/ajax/1',
	        dataType: 'json',
	        success: function(data) {
			$('.ui-dialog-title').removeClass('loadAnimation');			
				if(data.success) {
					$('div#window').dialog('close');
					if(data.message) {
						alert(data.message);
					}
					if(data.url) {
						document.location = data.url;
					} else if(!data.norefresh){
						document.location = location.pathname;
					}
					handle(id, data);
				} else {
	        		for (field in data.fields) {
	        			flag = false;
	        			html = '<ul class="errors">';
	        			for (message in data.fields[field]['messages']) {
	        				html += "<li>" + data.fields[field]['messages'][message] + "</li>";
	        				flag = true;
	        			}
	        			html += "</ul>";
	        			if(flag) {
	        				$("#" + formId + " #" + field + "-errors").html(html);	    					
	        			}
	        		}
				}
			}	 
	    }); 
		return false;		
	});
};