jQuery.myEngine = {				
	clone: function clone(o) {
		if(!o || 'object' !== typeof o)  {
			return o;
		}
		var c = 'function' === typeof o.pop ? [] : {};
		var p, v;
		for(p in o) {
			if(o.hasOwnProperty(p)) {
				v = o[p];
				if(v && 'object' === typeof v) {
					c[p] = this.clone(v);
				} else {
					c[p] = v;
				}
			}
		}
		return c;
	},
	
	/**
	 * 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(){
		    	  $.ajax({
		    		  url: hash,
		    		  success: function(data) {
		    		  	if(data.redirect) {
							url = data.redirect;
							if(document.location.hash == url) {
								location.reload(true);
							} else {
								document.location = url;
							}
		    		  	} else {
		    		  		$(currentQuery).html(data);
		    		  		$(currentQuery).animate({ 
		    		  			opacity: 1
		    		  		}, 600);
		    		  		handle(hash);
		    		  	}
		    		  }
		    		});
			});
			
		});
		
		$("a[rel='" + rel + "'],.ajaxLoad").live('click', function(e) {
			if(e.button==0) {		
				var hash = this.href || this.id;
				hash = hash.replace(/%20/gi, '+');
				hash = hash.replace(/^.*#/, '');
				hash = hash.replace(location.protocol + '//' + location.host, '');
				$.historyLoad(hash);
				return false;
			}
		});
	}	
};

jQuery.fn.getField = function(url) {
	var id = $(this).attr('id')+'_result';
	$('<span id="'+id+'"></span>').insertAfter(this);
	var t = this;
	handler = function(e){
		if($(t).attr('value')) {
			$.post(url, {
				"id": $(t).attr('value')
			}, function(data){
				$("#"+id).html(data);
			});
		} else {
			$("#"+id).html('');
		}
	};		
	handler();
	return this.live('keyup', handler);
};

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(){
		location.reload(true);
	};	
	this.bind('submit', function(e){
    	var formId = this.id;
    	
    	var prefix = ''; 
    	if($("#window #" + formId + " dd").get(0)) {    		
    		prefix = '#window ';
    	}
    	
    	$(prefix + "#" + formId + " dd").each(function(){
			var id = this.id.replace('-element', '');
        	if ($(prefix + "dl #" + id + "-errors").get(0)) {
            	$(prefix + "dl #" + id + "-errors").html(null);
        	} else {
        		$(this).after($('<div id="' + id + '-errors">'));
        	}
		});
    	
		var id = this.id;
		$(this).ajaxSubmit({
			data: {
				'ajax':1
			},
			url: this.action,
	        dataType: 'json',
	        success: function(data) {
				if(data.redirect) {
					url = data.redirect;
					if(document.location.hash == url) {
						location.reload(true);
					} else {
						document.location = url;
					}
				} else {
					if(data.success) {
						$('div#window').dialog('close');
						if(data.message) {
							alert(data.message);
						}
						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) {
		        				$(prefix + "#" + formId + " #" + field + "-errors").html(html);	    					
		        			}
		        		}
					}
				}
			}	 
	    }); 	
		return false;			
	});
};
