/***************************************************************
*  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
}else{
	var existingOnload = function(){};//do nothing
}

function secondOnload() {
	existingOnload();
	getElementsByClassNameAndReplace("ajaxLink");
	initialize();/*RSH Framework*/
	checkBookmarkedVisit();
}
/* 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;
}
/**  END unobtrusive javascript: dealing with existing window.onload event handler **/


/** RSH Framework (to enable History and Bookmarks) **/
function initialize() {// initialize the DHTML History framework (dhtmlHistory.js)
	dhtmlHistory.initialize();// subscribe to DHTML history change events
    dhtmlHistory.addListener(historyChange);
}
         
/** callback to receive history change events. */
function historyChange(idParams) {
	if(idParams){
		newLocation='index.php?id='+idParams;
		location.href = newLocation;
		/*location.href = idParams; */ //use this if staticDocuments is activated
	}
}	 
/** END RSH Framework (to enable History and Bookmarks) **/	

function checkBookmarkedVisit(){//svens workaround for bookmark visits
	if(window.location.hash){
		var hashString= window.location.hash;
		var idParams=hashString.replace(/#/,"");
		var newLocation='index.php?id='+idParams;
		/*var newLocation='http://yourdomain.tld/'+idParams;*///use this if simulateStaticDocuments is activated
		location.href = newLocation;
	}
}

/**  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==c) {
			linkvar=tags[i].firstChild.getAttribute("href");
			linktext=tags[i].firstChild.firstChild.nodeValue;
			tags[i].innerHTML='<a href="javascript:void(0);" onClick=\"javascript:sndReq(\''+linkvar+'\')\;\">'+linktext+'</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','typo3conf/ext/tut_unobtrusiveajax/media/parserscript.php?linkurl=' + linkvar,true);//asynchron
	resObjekt.onreadystatechange = handleResponse;
	resObjekt.send(null);
}

function handleResponse() {
	if(resObjekt.readyState == 4){
		var response=resObjekt.responseText;
		var stringArrayContent = response.split('xxSPLITRESPONSEHERExx');
		var responseContent = stringArrayContent[0];
		var responseTitle = stringArrayContent[1];
		document.getElementById("stuffToLoad").innerHTML =responseContent;
		document.title=responseTitle;
		/*document.getElementById("uniqueContent").style.visibility = "visible";*/
	}
}
/** END Change partial page content with Ajax **/ 

