/*
Autor: Daniel Valdivia Milanés
Email: dvaldivia@ienrage.com
version: 1.1
Notas: que haces con una copia de esto?
*/
var mCore = new Class({
	Implements: [Options,Events],
	options: {
		historyCalls:null
	},
	services:null,
	reqHistory:null,
	originalStyles:null,
    initialize: function(){
    	this.historyCalls = new Hash();
		window.addEvent('domready', function(){
 			HistoryManager.initialize();
			
			this.reqHistory = HistoryManager.register(
			'page-index',
			[0], // default, page 0
			function(values) {
				this.requestCall(values[0]);
			}.bind(this),
			function(values) {
				return '' + values[0] + '';
			},
			/(.*)/ // the regexp to match after the #
			);
			HistoryManager.start();
		}.bind(this));
		
		document.comCalls = new Hash();
       
    },
    placeVideoPlayer: function(video){
    
    
    		var autostart = 'false';
    		
    		if($type(video['autostart']) == 'boolean')
    		{
    			if(video['autostart'])
    			{
    				autostart = 'true';
    			}
    		}
    		
    		var width = 570;
    		var height = 352;
    		
    		if($type(video['width']) == 'number')
    		{
    			width = video['width'];
    		}

    		if($type(video['height']) == 'number')
    		{
    			height = video['height'];
    		}
    		
	    	var obj = new Swiff('players/player.swf', {
			    id: 'videoPlayer',
			    width: width,
			    height: height,
			    container: video['div'],
			    params: {
			        allowfullscreen: 'true',
			        allowscriptaccess: 'always',
			        flashvars:'file='+video['video']+'&image='+video['prev']+'&'+autostart,
			        bufferlength:'50'
			    }

			});
			//this.options.videoPlayer = obj;
		
    },
    request:function(options)
    {
    	this.displayNotification({clear:true});
    	if($type(options.url) != "string" && $type(options.form) != "string")
    	{
    		return false;
    	}
    	
    	var divLess = true;
    	var updateDiv;
    	
    	if($type(options.div) == "string" || $type(options.div) == "object")
    	{
    		if($type(options.scroll) == "boolean")
    		{
    			if(options.scroll)
    			{
    				this.scroll(0);
    			}
    		}
    		else
    		{
    			this.scroll(0);
    		}
    		
    		
    		if($type(options.div) == "string")
    		{
    			updateDiv = $(options.div);
    		}
    		else
    		{
    			updateDiv = options.div;
    		}
    		
    		divLess = false;
    	}
    	
    	if($type(options.noclear) == "boolean")
    	{
    		if(options.noclear)
    		{
    			//do nothing
    		}
    		else
    		{
    			//go back to norla
    			this.setStyles({original:true});
    		}
    	}
    	else
    	{
    		//back to normal
    		this.setStyles({original:true});
    	}
    	
    	var params;
    	
    	if($type(options.params) == "string")
    	{
    		params = options.params+"&isAjax=true";
    	}
    	else
    	{
    		params = "isAjax=true";
    	}
    	
    	var var_string = options.url;
			

		if($type(options.form) == "string" && options.form != "")
		{
			var myRequest = new Form.Request(options.form,updateDiv,{
				'requestOptions':{
					evalScripts:true
				},
				extraData:{
					'isAjax':'true'
				},
				onSend: function()
				{
					if(!divLess)
					{
						updateDiv.empty().addClass('loading');
					}
					
					var loading = new Element('div',{
						'html':'Cargando',
						'id':'loadingDiv'
					});
					
					loading.inject(document.body);
					
					$$('.hint').destroy();
					$$('.swiff-uploader-box').destroy();
					
				},
				onSuccess: function(updtDiv, responseText, responseXML)
				{
					if(!divLess)
					{
						//updateDiv.empty().removeClass('loading');
						//updateDiv.adopt(responseTree);
					}
					
					//eval(responseJavaScript);
				  
				  	if($type(options.onComplete) == 'function')
					{
						eval(options.onComplete);
					}
				    
				    $('loadingDiv').destroy();
				}
			});			
		}
		else
		{
			var myRequest = new Request.HTML({
    						method: 'post', 
    						url: options.url,
    						onRequest: function()
    						{
								if(!divLess)
								{
									updateDiv.empty().addClass('loading');
								}
								
								var loading = new Element('div',{
									'html':'Cargando',
									'id':'loadingDiv'
								});
								$$('.hint').destroy();
								$$('.swiff-uploader-box').destroy();
								loading.inject(document.body);
								
								
    						},
    						onSuccess: function(responseTree, responseElements, responseHTML, responseJavaScript)
							{
								if(!divLess)
								{
									updateDiv.empty().removeClass('loading');
									updateDiv.adopt(responseTree);
								}
								
								eval(responseJavaScript);
							  
							  	if($type(options.onComplete) == 'function')
								{
									eval(options.onComplete);
								}
							    
							    $('loadingDiv').destroy();
							},
							data:params
			});
		}
			
					
		
		
		if($type(options.history) == "boolean" && $type(options.uid) == "string")
		{
			if(options.history)
			{
				this.historyCalls.set(options.uid,myRequest);
				this.reqHistory.setValue(0,options.uid);
			}
		}


		if($type(options.ignited) == "boolean")
		{
			if(options.ignited)
			{
				
				this.historyCalls.set(options.url,myRequest);
				this.reqHistory.setValue(0,options.url);
			}
			
		}
		
		if($type(options.form) == "string" && options.form != "" && $type(options.history) == "boolean")
		{
			if(options.history)
			{
				var uid = target = $(options.form).action.replace('http://'+document.domain,'');
			
				this.historyCalls.set(uid,myRequest);
				this.reqHistory.setValue(0,uid);
			}
		}
		
		
		myRequest.send();

    },
    requestCall:function(rqst)
    {
    	if(rqst == '')
    	{
    		//this.request({url:'/',div:'ajascontent'});
    		return true;
    	}
    	//get the request and send it =P
    	//alert(rqst);
    	if(this.historyCalls.has(rqst))
    	{
    		this.historyCalls.get(rqst).send();
    	}
    	else
    	{
    		var partesPeticion = rqst.split("/");
    		
			//Pasamos la peticion a el servicio	si lo encontramos en el catalogo
			var service = rqst;
			
			//encontramos el servicio desconocido, mandemosle su peticion
			
			//serializemos el resto de la peticion
			var pars = "isAjax=true";
			
			
			this.request({url:service,div:'ajascontent',params:pars});
			
			
			
    	}
    	
    },
    displayNotification:function(options)
    {
    	var notifDiv = 'notificationDiv';
    	
    	if($type($(notifDiv)) != 'element')
    	{
    		var not = new Element('div',{'id':notifDiv});
    		
    		not.inject(document.body);
    	}
    	
    	if($type(options.clear) == "boolean")
    	{
    		if(options.clear)
    		{
    			$(notifDiv).empty();
    			$(notifDiv).removeClass('bad');
    			$(notifDiv).removeClass('good');		
    		}
    	}
    	
    	if($type(options.message) == "string")
    	{
    		
    		if($type(options.kind) == "string")
    		{
	    		$(notifDiv).addClass(options.kind);
    		}
    		
    		$(notifDiv).set('html',options.message);
    		this.scroll(0);
    	}
    	
    	return false;
    	
    },
    evalRemoteCall:function(options)
    {
    	
    	
    	if($type(options.responseHTML) == "string")
    	{
    		
    		if($type(options.div) == "string")
    		{
    			//alert(options.div + "a" + options.responseHTML);
    			$(options.div).set('html',options.responseHTML);
    		}
    		
    	}

    	if($type(options.responseJavaScript) == "string")
    	{
    		eval(options.responseJavaScript);
    	}
	
		if($type(options.cRname) == 'string')
		{

			if(document.comCalls.has(options.cRname))
			{
				//alert(document.comCalls.get(options.cRname));			
				document.comCalls.get(options.cRname).run();
				alert('aa');
			}
		}
		
    		
    },
    scroll:function(val)
    {
    	if($type(val) == "number")
    	{
    		scroll(val,0);
    	}
    	else if($type(val) == "string")
    	{
    		//var myFx = new Fx.Scroll(window).toElement(val);
			var myElement = $(document.body);
			var pos = $(val).getPosition();
			
			var myFx = new Fx.Scroll(myElement).start(pos.x, pos.y - 200);
    	}
    },
    setStyles:function(data)
    {
    	if($type(data) != "object")
    	{
    		return false;
    	}
    	if(this.originalStyles == null)
    	{
    	
    		this.originalStyles = new Hash();
    	}
    	
    	var objs = document.c.originalStyles;
    			
    			objs.each(function(val,key)
    			{

    				$each(val,function(valor,llave){
    					$(key).setStyle(llave,valor);
    				});
    				
    			});
    	
    	if($type(data.original) == "boolean")
    	{
    		if(data.original)
    		{
    			//regresamos todo a como estaba
    			
    			return true;
    			
    		}
    	}
    	
    	$each(data,function(val,key){
    		
    		if(document.c.originalStyles.has(key))
    		{
	    		var oriObject = document.c.originalStyles.get(key);
    		}
    		else
    		{
    			var oriObject = {};
    		}
    		
    		//alert(val.toString);
    		//recopilamos valores originales
    		$each(val,function(valor,llave){
    			
    			if($type(oriObject[llave]) == false)
    			{
    				oriObject[llave] = $(key).getStyle(llave);	
    			}
    			//aplica
    			//$(key).setStyle(llave,valor);
    		
    		});
    		
    		//guardar	
    		document.c.originalStyles.set(key,oriObject);
    	
    	});
    	
    	
    		//aplicamos
    	$each(data,function(val,key){
    		$each(val,function(valor,llave){
				$(key).setStyle(llave,valor);
    			
    		});
    	});
    	
    	
    	
    }
});
