/***************************************************************
*  Copyright notice
*
*  (c) 2007 Sven Waechli (sven@screenteam.ch)
*  All rights reserved
*
*  This script is part of the TYPO3 project. The TYPO3 project is
*  free software; you can redistribute it and/or modify
*  it under the terms of the GNU General Public License as published by
*  the Free Software Foundation; either version 2 of the License, or
*  (at your option) any later version.
*
*  The GNU General Public License can be found at
*  http://www.gnu.org/copyleft/gpl.html.
*
*  This script is distributed in the hope that it will be useful,
*  but WITHOUT ANY WARRANTY; without even the implied warranty of
*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*  GNU General Public License for more details.
*
*  This copyright notice MUST APPEAR in all copies of the script!
***************************************************************/
/**
 * @author	Sven Waechli	<sven@screenteam.ch>
 * @author	Mario Rimann	<typo3-coding@rimann.org>
 */

/**  unobtrusive javascript: dealing with existing window.onload event handler **/
if(window.onload){
	var existingOnload = window.onload;	//if window.onload=firstfunction; exists
	getElementsByClassNameAndReplace();
}else{
	var existingOnload = function(){};//do nothing
	getElementsByClassNameAndReplace();
}

function secondOnload() {
	existingOnload();
	initialize();/*RSH Framework*/
}
/* Check W3C DOM support with Object detection */
var W3CDOM=(document.createElement && document.getElementsByTagName); //additional tests like browser detection go here
if(W3CDOM){
	window.onload = secondOnload;
	//window.addEventListener("load", getElementsByClassNameAndReplace, false);
	if (window.addEventListener) //DOM method for binding an event
	window.addEventListener("load", getElementsByClassNameAndReplace, false)
	else if (window.attachEvent) //IE exclusive method for binding an event
	window.attachEvent("onload", getElementsByClassNameAndReplace)
	else if (document.getElementById) //support older modern browsers
	window.onload=getElementsByClassNameAndReplace;
}
/**  END unobtrusive javascript: dealing with existing window.onload event handler **/

         
	 
/**  Change normal links on page into Ajax links **/ 
function getElementsByClassNameAndReplace(c){//t for filtering tags, not necessary
	var tags=document.getElementsByTagName("*");
        for(var i=0, len= tags.length; i<len; i++) {
                if (tags[i].className=='ajaxLink') {
                        linkvar=tags[i].getAttribute("href");
                        linkvar=linkvar.replace("&", "%26");
			rq = 'sndReq(\''+linkvar+'\')';
			
			tags[i].setAttribute('href', 'javascript:' + rq);			
			tags[i].setAttribute('onmouseover', rq);

                        //linktext=tags[i].firstChild.firstChild.nodeValue;
                        //tags[i].innerHTML='<a href="javascript:void(0);" onClick=\"javascript:sndReq(\''+linkvar+'\')\;\">'+this.NodeValue+'</a>';
                }
}	
}
/** END Change normal links on page into Ajax links **/ 

/** Change partial page content with Ajax **/ 
function getXMLHttpRequest()
{
		if (window.XMLHttpRequest){
			//XMLHttpRequest for Firefox, Opera, Safari, ev. IE7
			return new XMLHttpRequest();
		} else
		if (window.ActiveXObject){
			try{
				//XMLHttp (new) for IE
				return new ActiveXObject("Msxml2.XMLHTTP");
			} 	catch(e) {
				try{
					//XMLHttp (old) for IE
					return new ActiveXObject("Microsoft.XMLHTTP");
				} 	catch(e){
					return null;
				}
			}
		}
		return null;
}
									
var resObjekt;
	resObjekt = getXMLHttpRequest();

function sndReq(linkvar) {
	test = /\?id=/.test(linkvar);//regex test: is ?id= substring of linkvar?
	if(test){//simulateStaticDocuments is NOT activated
		var stringArray = linkvar.split('?id=');	
		var idParams = stringArray[1];
	}else
	if(!test){//simulateStaticDocuments IS activated
		test2 = /\//.test(linkvar);//regex test: is / a substring of linkvar?
		if(test2){//linkvar is complete link (IE)
			var stringArray = linkvar.split('/');
			var lastPos=(stringArray.length)-1;
			idParams = stringArray[lastPos];
		}else
		if(!test2){//linkvar is NOT complete link (Firefox etc)
			idParams=linkvar;
		}
	}

//	dhtmlHistory.add(idParams, true);//RSH framework, extract page id and add to history-->enables backbutton+history	
	
	//*********** overall object detection at the start of the script *****************
	var supportCheck=document.createElement && document.getElementsByTagName && getXMLHttpRequest();
	if(!supportCheck) return;
	//*********** END overall object detection at the start of the script *************
	resObjekt.open('get','/fileadmin/users/2/ajax/parserscript.php?linkurl=' + linkvar,true);//asynchron
	resObjekt.onreadystatechange = handleResponse;
	resObjekt.send(null);
}

function handleResponse() {
	if(resObjekt.readyState < 4){
		document.getElementById("stuffToLoad").setAttribute('style','background: url(/fileadmin/users/2/ajax/ajax-loader.gif) 5% 5% no-repeat; height: 231px');
		document.getElementById("stuffToLoad").innerHTML = '';
	}
	else if(resObjekt.readyState == 4){
		var response=resObjekt.responseText;
		var stringArrayContent = response.split('xxSPLITRESPONSEHERExx');
		var responseContent = stringArrayContent[0];
		//var responseTitle = stringArrayContent[1];
		//var responseHeaderBackground = stringArrayContent[2];
		document.getElementById("stuffToLoad").setAttribute('style','background: none; height: auto');
		document.getElementById("stuffToLoad").innerHTML = responseContent;
		//document.title=responseTitle;
		//document.title=responseHeaderBackground;
		//document.getElementById("uniqueContent").style.visibility = "visible";
	}
}
/** END Change partial page content with Ajax **/ 
