var contentProcessor = function(){
     var ua = navigator.userAgent.toLowerCase();
     this.isOpera = (ua.indexOf('opera') > -1);
     this.isSafari = (ua.indexOf('safari') > -1);
     this.isGecko = (!isOpera && !isSafari && ua.indexOf('gecko') > -1);
     this.isIE = (!isOpera && ua.indexOf('msie') > -1); 

};

contentProcessor.init = function(){
   this.xmlContent = this.retrieveXMLContent("/demo/db/demo/1/module.xml", false);
   var pageType = this.getMetadataItem("pagetype");
   this.xmlNewComponents = contentProcessor.retrieveXMLContent("/demo/xml/ui/newComponents.xml", false);
   contentProcessor.getTransFormers(pageType);
};

contentProcessor.getTransFormers = function(pageType){
   this.emuTransform = contentProcessor.retrieveXMLContent("/demo/xml/"+pageType+"/emulator.xsl", false);
   this.xmlTransform = contentProcessor.retrieveXMLContent("/demo/xml/"+pageType+"/ui.xsl", false);
   this.menuTransform = contentProcessor.retrieveXMLContent("/demo/xml/"+pageType+"/menu.xsl", false);
}

contentProcessor.resetPageType = function(){
   var pageName = this.getMetadataItem("pagename");
   var pageType = this.getMetadataItem("pagetype");
   var colourScheme = this.getMetadataItem("colourscheme");
   this.xmlContent = this.retrieveXMLContent("/modules/xml/"+pageType+"/template.xml", false);
   contentProcessor.updateANodesText("metadata", "pagename", pageName);
   contentProcessor.updateANodesText("metadata", "colourscheme", colourScheme);
   contentProcessor.getTransFormers(pageType);
};

contentProcessor.placeContent = function(content, transformer, where){
   if (document.implementation && document.implementation.createDocument)
	{
	 try{
		var processor = new XSLTProcessor();
		processor.importStylesheet(transformer);
		document.getElementById(where).innerHTML = "";
        document.getElementById(where).appendChild(processor.transformToFragment(content, document));
       }catch(err){
         alert(err)
       }
	}
	else if (window.ActiveXObject)
	{	
	    transformer.resolveExternals = true;	 
		document.getElementById(where).innerHTML = content.transformNode(transformer);
 	}
    else
	{
		alert('Your browser can\'t handle this script');
		return;
	}
};

contentProcessor.getMetadataItem = function(metadataItem){
  var returnString = "default";
  var xpath =  "//item[@id='metadata']/metadata/" + metadataItem;
  var theNode = contentProcessor.selectUsingXpath(contentProcessor.xmlContent, xpath);
  if (window.ActiveXObject){
     returnString = theNode.text;
   }else{
     try
       {
	 returnString = theNode.textContent;
      }catch(e){
         alert("Some add-ons to Firefox 3 may interfere with the rendering of this page.\n Extensions known to cause problems include firebug and torbutton.\n Disable these Addons to properly view this page")
     }
   }
   
   return returnString
};

contentProcessor.updateXMLAfterMenuDrop = function(xmlFile, componentType, newId){
  	 var addedXMLContent = contentProcessor.xmlNewComponents.getElementsByTagName(componentType).item(0).cloneNode(true);
	 var item = xmlFile.createElement("item");
	 var theID = xmlFile.createAttribute("id")
	 item.setAttribute("id", newId);
	 item.appendChild(addedXMLContent); 
	 contentProcessor.placeContent(item, this.xmlTransform, newId);
	 var rootNode = xmlFile.getElementsByTagName('page')[0];
	 rootNode.appendChild(item);
	 contentProcessor.reorderXML();
	 contentProcessor.placeContent(this.xmlContent,this.emuTransform, "emulator");
};

contentProcessor.reorderXML = function(){
  var alist = utils.getElementsByClassName(document.getElementById("listGroup"), "div", "sortList");
  for (var i=0; i<alist.length; i++){
	   contentProcessor.updateANodesText(alist[i].id, "order",i);
  }	
};

contentProcessor.returnNumberOfComponents = function(){
  var children = this.xmlContent.getElementsByTagName('item');
  var thisId;
  var topId = 0;
  for (var i=0; i<children.length; i++){
     thisId = children[i].getAttribute("id");
     if (utils.IsNumeric(thisId)){
       if(parseInt(thisId)>parseInt(topId)){
         topId = thisId;
       }
     }
  }
  return topId;
};

contentProcessor.updateANodesText = function(inId, attributeToUpdate, newValue){
  var xpath =  "//item[@id='"+inId+"']/*/" + attributeToUpdate;
  var theNode = contentProcessor.selectUsingXpath(contentProcessor.xmlContent, xpath);
   var textNode = contentProcessor.xmlContent.createTextNode(newValue)
   if (theNode.hasChildNodes()){
		var oldNode = theNode.childNodes[0]
	    theNode.replaceChild(textNode, oldNode ) 
   }else{
       theNode.appendChild(textNode)
   }
};

contentProcessor.removeComponents = function(xpath){
  var nodeList
  if (window.ActiveXObject){
     nodeList  = this.xmlContent.selectNodes(xpath);
     for(i=0;i<nodeList.length;i++) {
       nodeList(i).parentNode.removeChild(nodeList(i));
    }
  }else{
     nodeList = contentProcessor.xmlContent.evaluate(xpath, 
	                                             this.xmlContent, 
												 null, 
												 XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, 
												 null);											
	  var pN;
	  var currentNode
	  for ( var i=0 ; i < nodeList.snapshotLength; i++ )
       {
          currentNode = nodeList.snapshotItem(i) ;
          pN = currentNode.parentNode
          pN.removeChild(currentNode);
       } 
  }
};

contentProcessor.xmlContains = function(component){
  var theNode = contentProcessor.selectUsingXpath(contentProcessor.xmlContent,"//" + component + "[1]");
  return theNode!=null ? true : false;
};

contentProcessor.selectUsingXpath = function(doc, xpath){
  var theNode
  if (window.ActiveXObject){
     theNode  = doc.selectSingleNode(xpath);
   }else{
     theNode = doc.evaluate(xpath, 
	                           doc,
								 null, 
								 XPathResult.FIRST_ORDERED_NODE_TYPE, 
												 null).singleNodeValue;
   }
   return theNode;
};

contentProcessor.retrieveXMLContent = function(xmlfile, async){
  if (window.ActiveXObject){
     var oXml = new ActiveXObject("Microsoft.XMLDOM");
     oXml.resolveExternals = true; //allows includes in xslt files
  }else{
    var oXml=document.implementation.createDocument("","",null);
  }
  oXml.async = false;
  oXml.load(xmlfile)
  return oXml;
};

contentProcessor.insertNewComponent = function(xmlFile, newId, componentType){
     var addedXMLContent = contentProcessor.xmlNewComponents.getElementsByTagName(componentType).item(0).cloneNode(true);
	 var item = xmlFile.createElement("item");
	 var theID = xmlFile.createAttribute("id")
	 item.setAttribute("id", newId);
	 item.appendChild(addedXMLContent); 
	 var rootNode = xmlFile.getElementsByTagName('page')[0];
	 rootNode.appendChild(item);
};

