
var livesite ='<?php echo JURI::base(); ?>';

   var esIE6 = (window.external && typeof window.XMLHttpRequest == "undefined");


    var cargando = false;

    this.historyListener = function(historyHash) {
		//stateVar = historyHash;
	   if (!cargando){
		   cargando = true;
		 if (historyHash == ''){
		   cargando = false;
		   location.href='index.php';
		 }
		 else{    
			 linkAjax2(historyHash,$('contenidoAjax'));
		 }
	   }
    };
                
// subscribe to unFocus.History
unFocus.History.addEventListener('historyChange', this.historyListener);



         function botonVolver(){
             if(!esIE6){
               window.history.back();
             }
         }


	 var progressBar = new Class({
	 	initialize: function(){
			this.destino = $('contenidoAjax');
			var tam = this.destino.getSize();
			this.MyBarra=new Element('div',{
				'id':'barra',
				'styles':{
					'width': tam.x,
					'height':tam.y,
					'background': 'url("'+livesite+'templates/imasa/images/estructura/carga.gif") center no-repeat #000',
					'position':'absolute',
					'opacity': 0
				}
			});
		},
		mostrar: function(){
			$$('body').adopt(this.MyBarra);
			var pos = this.destino.getPosition();
			this.MyBarra.setStyle('z-index','99');
			this.MyBarra.setStyles({
				top: pos.y,
				left: pos.x,
				opacity: 0.6
			});
		},
		ocultar: function(){
			this.MyBarra.setStyle('opacity',0);
			this.MyBarra.destroy();
		}
	})


	   

	     function linkAjax(link, destino,tmpl,nohistorial){
		   
	       var linkoriginal = link;
               
               if(tmpl){
                  link = link + tmpl;
                }
               
                if((!nohistorial) && (!esIE6)){                    
                  //alert('antes');
	          unFocus.History.addHistory(link);

                  //$clear(periodicalFunctionVar);
                  //CODIGO SEGUIMIENTO GOOGLE
              	  this.pageTracker = _gat._getTracker('UA-4153643-32');			  
			      this.pageTracker._trackPageview(linkoriginal);

		
                }else{   
					
                         linkAjax2(link, destino);
                }

         }


function linkAjax2(link, destino){
			
			       var barra = new progressBar(destino);
				
				barra.mostrar(); 
				
                               // var scroller = new Fx.Scroll(document.body);
                			
				
				new Request.HTML({
					url:link,
					update:destino,
                                        useWaiter: true,
					evalScripts: true,
					onSuccess: function (responseTree, responseElements, responseHTML, responseJavaScript){
                                                barra.ocultar();
                                               
						cargando = false;
						 
						//scroller.scrollIntoView($('capa_contenedor'));
						
                        
						//pageTracker._trackPageview(link);
					}		   
				}).post();
          }

         function FormAjax(formulario,destino,tmpl){
			//alert('aca2');
			var barra = new progressBar(destino);
			//alert(barra);
			barra.mostrar();  
			new Request.HTML({
			   url:formulario.action,
			   update:destino,
                           useWaiter: false,
                           waiterOptions:{
                    fxOptions:{duration: 500}
               },
			   evalScripts: true,
			   onSuccess: function(responseTree, responseElements, responseHTML, responseJavaScript){
						barra.ocultar();  	  
				}
			}).post(formulario);
		
			return false;
          }    
		
		
	
    
	  

