/***************************************************************
*  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+'#'+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://localhost/storz/#'+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("*");
	//alert(typeof(tags[5]));
	for(var i=0, len= tags.length; i<len; i++) {
		
		if (typeof(tags[i]) == 'object') {
			if (tags[i].className==c) {
				linkvar=tags[i].firstChild.getAttribute("href");
				linktext=tags[i].firstChild.firstChild.nodeValue;
				linktext2 = tags[i].firstChild.innerHTML;
				tags[i].innerHTML='<a href="javascript:void(0);" onClick=\"javascript:sndReq(\''+linkvar+'\')\;\">'+linktext2+'</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 *************
		if(resObjekt.readyState == 1 || resObjekt.readyState == 2 ||resObjekt.readyState == 3)resObjekt.abort();
		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 || resObjekt.readyState == 0){
		var response=resObjekt.responseText;
		var stringArrayContent = response.split('xxSPLITRESPONSEHERExx');
		document.getElementById("ajaxcontent").innerHTML = stringArrayContent[0];
		document.title = stringArrayContent[1];
		/*
		document.getElementById("printlink").style.display='block';
		document.getElementById("printlink_a").onclick = function() {
			window.open('index.php?id='+stringArrayContent[3]+'&type=98','print_win','resizable=no, toolbar=no, scrollbars=yes, menubar=no, width=640, height=500');
		};
		*/
		sIFR.replaceElement("h1", "heading.swf", "#005596", null, null, null, 0, 18, 0, 0, null, null, "transparent");
		myLightbox.initialize();
		
		document.getElementById('flash').innerHTML = stringArrayContent[2];
		if (stringArrayContent[2].indexOf('SWFObject') != -1) {
			var tmp = stringArrayContent[2];
			tmp = tmp.substring(tmp.indexOf('/*<![CDATA[*/')+13, tmp.indexOf('/*]]>*/'));
			eval(tmp);
		}
	} else if(resObjekt.readyState == 1 ) {
		document.getElementById("ajaxcontent").innerHTML ='<div style="text-align:center; width:100%;height:200px; padding-top:60px;"><img src="fileadmin/storz/images/loading.gif" alt="loading"/></div>';
		//document.getElementById("printlink").style.display='none';
	}
}
/** END Change partial page content with Ajax **/ 
