/**
 * Eldönti, hogy az adott oldal támogat-e jQuery-t.
 * Ha nem, akkor feltételezi, hogy Prototype-ot támogat.
 * 
 * Elvégzi a megfelelő keretrendszer műveleteit.
 * @version 0.0.6 (major.minor.modifications)
 * 
 * @author horvathg, sarkiroka, sajo, blajos
 */

var isJQueryPresentOnThisSite = typeof jQuery!='undefined';

var activeVisionCrossFrameworkUtility={};

var cfu=activeVisionCrossFrameworkUtility;

activeVisionCrossFrameworkUtility.version='0.0.6';

activeVisionCrossFrameworkUtility.removeClass = function (element, className) {
	if(typeof element=='undefined' || element==null){
		return false;
	}
	if (isJQueryPresentOnThisSite) {
		jQuery(element).removeClass(className);
	} else {
		if(typeof element.push == 'undefined'){
			$(element).removeClassName(className);
		}else{
			for(var i=0,i_max=element.length;i<i_max;i++){
				$(element[i]).removeClassName(className);
			}
		}
	}
};

activeVisionCrossFrameworkUtility.addClass = function (element, className) {
	if(typeof element=='undefined' || element==null){
		return false;
	}
	if (isJQueryPresentOnThisSite) {
		jQuery(element).addClass(className);
	} else {
		if(typeof element.push == 'undefined'){
			$(element).addClassName(className);
		}else{
			for(var i=0,i_max=element.length;i<i_max;i++){
				$(element[i]).addClassName(className);

			}
		}
	}
};

activeVisionCrossFrameworkUtility.hasClass = function (element, className) {
	if(typeof element=='undefined' || element==null)return false;
	if (isJQueryPresentOnThisSite) {
		return jQuery(element).hasClass(className);
	} else {
		return $(element).hasClassName(className);
	}
};

activeVisionCrossFrameworkUtility.value = function (element) {
	if(typeof element=='undefined' || element==null)return null;
	if (isJQueryPresentOnThisSite) {
		return jQuery(element).val();
	} else {
		return element.value;
	}
};

activeVisionCrossFrameworkUtility.valueUpdate = function (element, replacement) {
	if (isJQueryPresentOnThisSite) {
		return jQuery(element).val(replacement);
	} else {
		return element.value=replacement;
	}
};

activeVisionCrossFrameworkUtility.htmlUpdate = function (element, replacement) {
	if (isJQueryPresentOnThisSite) {
		jQuery(element).html(replacement);
	} else {
		if(typeof element.push == 'undefined'){
			$(element).update(replacement);
		}else{
			for(var i=0,i_max=element.length;i<i_max;i++){
				$(element[i]).update(replacement);
			}
		}
	}
};

activeVisionCrossFrameworkUtility.replace = function (element, replacement) {
	if (isJQueryPresentOnThisSite) {
		//TODO
	} else {
		if(typeof element.push == 'undefined'){
			$(element).replace(replacement);
		}else{
			for(var i=0,i_max=element.length;i<i_max;i++){
				$(element[i]).replace(replacement);
			}
		}
	}
};

activeVisionCrossFrameworkUtility.onKeyUp = function (element, func) {
	if (isJQueryPresentOnThisSite) {
		jQuery(element).keyup(func);
	} else {
		$(element).observe('keyup', func);
	}
};

activeVisionCrossFrameworkUtility.onBlur = function (element, func) {
	if (isJQueryPresentOnThisSite) {
		jQuery(element).blur(func);
	} else {
		$(element).observe('blur', func);
	}
};

activeVisionCrossFrameworkUtility.onChange = function (element, func) {
	if (isJQueryPresentOnThisSite) {
		jQuery(element).change(func);
	} else {
		$(element).observe('change', func);
	}
};

activeVisionCrossFrameworkUtility.onClick = function (element, func) {
	if(typeof element=='undefined' || element==null)return;
	if (isJQueryPresentOnThisSite) {
		jQuery(element).click(func);
	} else {
		if(typeof element.push == 'undefined'){
			$(element).observe('click', function(e){
				var r=func(e);
				if(!r){
					if (!e) var e = window.event;
					try{
						e.cancelBubble = true;
						if (e.stopPropagation) e.stopPropagation();
					}catch(ex){}
					try{e.stop();}catch(ex){}
					return false;
				}
			});
		}else{
			for(var i=0,i_max=element.length;i<i_max;i++){
				$(element[i]).observe('click', function(e){
					var r=func(e);
					if(!r){
						if (!e) var e = window.event;
						try{
							e.cancelBubble = true;
							if (e.stopPropagation) e.stopPropagation();
						}catch(ex){}
						try{e.stop();}catch(ex){}
						return false;
					}
				});
			}
		}
	}
};

activeVisionCrossFrameworkUtility.onLoad = function (func) {
	if (isJQueryPresentOnThisSite) {
		jQuery(document).ready(func);
	} else {
		Event.observe(window, 'load', func);
	}
};

activeVisionCrossFrameworkUtility.onSubmit = function (element, func) {
	if(typeof element=='undefined' || element==null)return;
	if (isJQueryPresentOnThisSite) {
		jQuery(element).submit(func);
	} else {
		if(typeof element.push == 'undefined'){
			$(element).observe('submit', function(e){
				var r=func(e);
				if(!r){
					if (!e) var e = window.event;
					try{
						e.cancelBubble = true;
						if (e.stopPropagation) e.stopPropagation();
					}catch(ex){}
					try{e.stop();}catch(ex){}
					return false;
				}
			});
		}else{
			for(var i=0,i_max=element.length;i<i_max;i++){
				$(element[i]).observe('submit', function(e){
					var r=func(e);
					if(!r){
						if (!e) var e = window.event;
						try{
							e.cancelBubble = true;
							if (e.stopPropagation) e.stopPropagation();
						}catch(ex){}
						try{e.stop();}catch(ex){}
						return false;
					}
				});
			}
		}
	}
};

activeVisionCrossFrameworkUtility.onHover = function (element, funcEnter, funcOut) {
	if (isJQueryPresentOnThisSite) {
		jQuery(element).hover(funcEnter, funcOut);
	} else {
		if(typeof element.push == 'undefined'){
			$(element).observe('mouseover', funcEnter);
			$(element).observe('mouseout', funcOut);
		}else{
			for(var i=0,i_max=element.length;i<i_max;i++){
				$(element[i]).observe('mouseover', funcEnter);
				$(element[i]).observe('mouseout', funcOut);
			}
		}
	}
};

activeVisionCrossFrameworkUtility.onMouseDown = function (element, func) {
	if (isJQueryPresentOnThisSite) {
		jQuery(element).mousedown(func);
	} else {
		if(typeof element.push == 'undefined'){
			$(element).observe('mousedown', func);
		}else{
			for(var i=0,i_max=element.length;i<i_max;i++){
				$(element[i]).observe('mousedown', func);
			}
		}
	}
};

activeVisionCrossFrameworkUtility.onMouseUp = function (element, func) {
	if (isJQueryPresentOnThisSite) {
		jQuery(element).mouseup(func);
	} else {
		if(typeof element.push == 'undefined'){
			$(element).observe('mouseup', func);
		}else{
			for(var i=0,i_max=element.length;i<i_max;i++){
				$(element[i]).observe('mouseup', func);
			}
		}
	}
};

activeVisionCrossFrameworkUtility.onMouseMove = function (element, func) {
	if (isJQueryPresentOnThisSite) {
		jQuery(element).mousemove(func);
	} else {
		if(typeof element.push == 'undefined'){
			$(element).observe('mousemove', func);
		}else{
			for(var i=0,i_max=element.length;i<i_max;i++){
				$(element[i]).observe('mousemove', func);
			}
		}
	}
};

activeVisionCrossFrameworkUtility.getAttribute = function (element, attribute) {
	if (isJQueryPresentOnThisSite) {
		//TODO
	} else {
		return $(element).readAttribute(attribute);
	}
};

activeVisionCrossFrameworkUtility.getByCss3Selector = function (clazz) {
	if (isJQueryPresentOnThisSite) {
		return jQuery(clazz);
	} else {
		return $$(clazz);
	}
};

activeVisionCrossFrameworkUtility.getByCss3SelectorFromElement = function (element, clazz) {

	if (element == null || typeof element == 'undefined'){
		return [];	
	}else{
		if (isJQueryPresentOnThisSite) {
			return element.find(clazz);
		} else {
			return $(element).getElementsBySelector(clazz);
		}	
	}
};

activeVisionCrossFrameworkUtility.showOrHide = function (element, show) {
	if (isJQueryPresentOnThisSite) {
		if (show) {
			jQuery(element).show();
		} else {
			jQuery(element).hide();
		}
	} else {
		if(typeof element.push == 'undefined'){
			if (show) {
				$(element).show();
			} else {
				$(element).hide();
			}
		}else{
			for(var i=0,i_max=element.length;i<i_max;i++){
				if (show) {
					$(element[i]).show();
				} else {
					$(element[i]).hide();
				}
			}
		}
	}
};

activeVisionCrossFrameworkUtility.show = function (element) {
	activeVisionCrossFrameworkUtility.showOrHide(element, true);
};

activeVisionCrossFrameworkUtility.hide = function (element) {
	activeVisionCrossFrameworkUtility.showOrHide(element, false);
};

/**
 * @param param egy objektum mely a következő mezőkre figyel:
 * 		-url 		- a meghivandó url
 * 		-method 	- a hívás módja
 * 		-success 	- sikeres lekérés után lefutó függvény mely első és egyetlen paraméterében kapja a lekért contentet
 * 		-data		- a beküldendő adat objektumként 
 * @author sarkiroka
 */
activeVisionCrossFrameworkUtility.ajax = function (param) {
	if (isJQueryPresentOnThisSite) {
		jQuery.ajax({
			'url': param.url,
			'type': param.method,
			'dataType': param.dataType,
		    'contentType': param.contentType,
			'data':  param.data,
			'async': param.async,
			
			success: function (p) { return function(data) {
				p.success(data);
			};}(param),
			error: function (p) { return function(data) {
				p.error(data);
			};}(param)
		});
	} else {
		new Ajax.Request(param.url,
		  avExtend({
		    method: param.method,
		    parameters: param.data,
		    onSuccess: function (p) { return function(data) {
		    	var parsedData=data;
		    	if(p.dataType=='json'){
		    		try{
		    		eval('parsedData='+data);
		    		}catch(e){alert('failed to parse response as json\n'+e);}
		    	}
				p.success(typeof p.dataType == 'undefined' || p.dataType=='text' ? data.responseText : data);
			};}(param),
		    onFailure: function (p) { return function(data) {
				p.error(data);
			};}(param)
		  },param));
	}
};
/**
 * kiterjeszti a base objektumot param értékeivel és a kiterjesztettet adja vissza
 * @param base
 * @param param
 * @returns
 * @author sarkiroka
 */
function avExtend(base,param){
	var result=base;
	for(i in param){
		result[i]=param[i];
	}
	return result;
}

function object2Json(n){
	if(typeof n !='object'){
		return ''+n;
	}
	var result='{';
	var first=true;
	for(i in n){
		var o=n[i];
		if(!first)result+=',';
		first=false;
		if(typeof o=='object'){
			result+=i+':'+object2Json(o);
		}else{
			var str=false;
			if(typeof o=='string')str=true;
			result+=i+':'+(str?'\'':'')+o+(str?'\'':'');
		}
	}
	return result+'}';
}





