function Hijax() {

 var container,url,canvas,data,loading,callback,request,blnFading=false;

 this.setContainer = function(value) {
  container = value;
 };
 this.setUrl = function(value) {
  url = value;
 };
 this.setCanvas = function(value) {
  canvas = value;
 };
 this.setLoading = function(value) {
  loading = value;
 };
 this.setCallback = function(value) {
  callback = value;
 };
 this.blnFading=blnFading;

 this.captureData = function() {
  if (container.nodeName.toLowerCase() == "form") {
   container.onsubmit = function() {
    var query = "";
    for (var i=0; i<this.elements.length; i++) {
     query+= this.elements[i].name;
     query+= "=";
     query+= encodeURIComponent(this.elements[i].value);
     query+= "&";
    }
    data = query + "&ajax=true";
    url = this.action;
    return !start();
   };
  } else {
   var links = container.getElementsByTagName("a");
   for (var i=0; i<links.length; i++) {
	links[i].onclick = function() {
	    var query = this.getAttribute("href").split("?")[1];
	    url=this.getAttribute("href") + "&ajax=true";
	    //dhtmlHistory.add(( this.innerText?this.innerText:this.textContent?this.textContent:"" ),this.getAttribute("id"));
	    return !start();
	};
   }
   links = null;
  }
 };

 var start = function() {
  request = getHTTPObject();
  if (!request || !url) {
   return false;
  } else {
   initiateRequest();
   return true;
  }
 };

 var getHTTPObject = function() {
  var xmlhttp = false;
  if (window.XMLHttpRequest) {
   xmlhttp = new XMLHttpRequest();
  } else if(window.ActiveXObject) {
   try {
    xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
   } catch (e) {
    try {
     xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    } catch (e) {
     xmlhttp = false;
    }
   }
  }
  return xmlhttp;
 };

 var initiateRequest = function() {
  if (loading) {
   loading();
  }
  request.onreadystatechange = completeRequest;
  if (data) {
   request.open("POST", url, true);
   request.setRequestHeader("Content-Type","application/x-www-form-urlencoded;charset=UTF-8");
   request.send(data);
  }else{
   request.open("GET", url, true);
   request.send(null);
  }
 };

 var completeRequest = function() {
  if (request.readyState == 4) {
   if( (request.status == 200 || request.status == 304) || (request.status == 404 && request.responseText!=null && ""!=request.responseText))  {
    if (canvas) {
     canvas.innerHTML = request.responseText;
    }
    if (callback) {
     callback(url);
    }
   }else if(request.status == 404){
    alert('Error al processar dades des del web:\n' + url);
   }
  }
 };

}

function webCom() {

 var prepareAnchor = function(element) {
  var xhr = new Hijax();
  //Determinem la URL actual
  var currentURL=window.location.href;

  if(currentURL!=null && currentURL.length>0 && currentURL.indexOf("?")>0)
      currentURL=currentURL.split("?")[0];

  xhr.setContainer(element);
  xhr.setUrl(currentURL);
  xhr.setCanvas(document.getElementById("contingut"));
  xhr.setLoading(function() {
   displayLoading(document.getElementById("contingut"));
  });
  xhr.setCallback(function(url) {
   if(xhr.blnFading)
       fadeUp(document.getElementById("contingut"),255,255,204);

    var navRoot=document.getElementById('menu').getElementsByTagName('UL')[0]; //ul/li/a
    for (i=0; i<navRoot.childNodes.length; i++) {
      node = navRoot.childNodes[i].childNodes[0];
      if(node.nodeName=="A"){
          if((node.href+"&ajax=true")==url){
          node.className="seleccionat";
          if(document.title.indexOf('-')>0)
              document.title=document.title.substring(0,document.title.indexOf('-') + 1 ) + " " + ( node.innerText?node.innerText:node.textContent?node.textContent:"" );
          }else
          node.className="";
      }
    }
    if(document.getElementById('theForm')){
      prepareAnchor(document.getElementById('theForm'));
    }
    Shadowbox.setup();
    
  });
  xhr.captureData();
 };

 var displayLoading = function(element) {
  var image = document.createElement("img");
  image.setAttribute("alt","carregant...");
  image.setAttribute("src","../../img/loading.gif");
  image.className = "loading";
  element.innerHTML="";
  element.appendChild(image);
 };

 var fadeUp = function(element,red,green,blue) {
  if (element.fade) {
   clearTimeout(element.fade);
  }
  element.style.backgroundColor = "rgb("+red+","+green+","+blue+")";
  if (red == 255 && green == 255 && blue == 255) {
   return;
  }
  var newred = red + Math.ceil((255 - red)/10);
  var newgreen = green + Math.ceil((255 - green)/10);
  var newblue = blue + Math.ceil((255 - blue)/10);
  var repeat = function() {
   fadeUp(element,newred,newgreen,newblue)
  };
  element.fade = setTimeout(repeat,100);
 };

 prepareAnchor(document.getElementById('menu'));
}

var gestionaHistory = function(newLocation, historyData){
    if(document.getElementById(historyData)!=null){
	document.getElementById(historyData).onclick();
	//document.getElementById(historyData).getAttributeNode('onclick').value;
	//eval(str.replace(/return false;/gi,""));
    }else if(historyData==null && (newLocation==null || ""==newLocation))
	document.getElementById("aPrimer").onclick();
}

function initHijaxAndMore(){
    Shadowbox.init();
    //dhtmlHistory.initialize();
    //dhtmlHistory.addListener(gestionaHistory);
    webCom();
}

//dhtmlHistory.create();
Shadowbox.loadSkin('classic', '../../js/shadowbox/skin');

function addEvent(obj, evType, fn, useCapture){
	if (obj.addEventListener) {
		obj.addEventListener(evType, fn, useCapture);
		return true;
	}
	else if (obj.attachEvent){
		var r = obj.attachEvent("on"+evType, fn);
		return r;
	}
	else {
		alert("No es pot carregar addEvent");
        window.onload=useCapture;
        return false;
	}
}
addEvent(window, 'load', initHijaxAndMore);
//window.onload = initHijaxAndMore;