/***************************************************************
*  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
	var firstLoad = 1;
} 

function secondOnload() { 
	existingOnload();
	for (i = 1; i < maxMenueCount+1; ++i) {
	var linkcID = i;
  var linkIDname = 'ajaxLink'+linkcID.toString();
  getElementsByClassNameAndReplace(linkIDname,linkcID);
	}
	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(/#/,"");
		/*always start with index.php
		/*var newLocation='index.php?id='+idParams;
		/*var newLocation='http://yourdomain.tld/'+idParams;*///use this if simulateStaticDocuments is activated
		location.href = newLocation;
	}
}

/** Get cid variable for inital accordion use not used at the moment **/ 
function accordionActiveTab(name) {
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null )
    return "";
  else    
    return results[1];
}


/**  Change normal links on page into Ajax links **/ 
function getElementsByClassNameAndReplace(c,cid){//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")+'&cid='+cid;
			linktext=tags[i].firstChild.firstChild.nodeValue;
			if (linkvar.search(/void/) == -1){  // if existing href="javascript:void(0);" do nothing
			tags[i].innerHTML='<a class="divsubnavlistlink" 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;
		}
	}
// idParams nutzen für geöffnetes Menü im Accordion 
	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/template/php/parserscript.php?linkurl=' + linkvar,true);//asynchron 
	//resObjekt.open('get','fileadmin/template/php/parserscript.php?linkurl=index.php?id=41&cid=1&tx_photogals_elementid=72&tx_photogals_image=3&no_cache=1',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];
		responseContentID = stringArrayContent[2].toString();
    //document.getElementById("dmcconajaxloaded1").innerHTML = responseContent;    		
    // -> eigentlich erst, wenn alles geladen/gerendert
    document.getElementById('dmcconajaxloaded'+responseContentID).innerHTML = responseContent;
		document.title=responseTitle;
		if (firstLoad == 1){ // replace links also in content elements on finished ajax loading        	
        if (responseContent.search(/tx_photogals_elementid/) != -1){
        jQuery('.tx-photogals-pi1-big').hide();
	      jQuery('.tx-photogals-pi1-big').fadeTo('500', 1);
        } else {
        jQuery('.dmccontent').hide();
	      jQuery('.dmccontent').fadeTo('300', 1);
	      jQuery('#dmcconajaxloaded4 .dmccontentbg').css('background-color','#2e2e2e');
        jQuery('#dmcconajaxloaded4 .dmccontent').css('color','#fdfdfd');
        jQuery('#dmcconajaxloaded4 .dmccontent h1').css('color','#fdfdfd'); 
        jQuery('#dmcconajaxloaded4 .dmcnewsbox p').css('color','#fdfdfd'); 
        jQuery('#dmcconajaxloaded4 .dmcnewsbox h1').css('color','#fdfdfd'); 
        jQuery('#dmcconajaxloaded4 .dmcnewsbox span.newsdate').css('color','#cdcdcd'); 
        jQuery('#dmcconajaxloaded4 .dmccontent a:link').css('color','#cdcdcd');
        jQuery('#dmcconajaxloaded4 .dmccontent a:visited').css('color','#cdcdcd');
        jQuery('#dmcconajaxloaded4 .dmccontent a:focus').css('color','#cdcdcd');
        jQuery('#dmcconajaxloaded4 .dmccontent a:active').css('color','#cdcdcd');
        jQuery('#dmcconajaxloaded4 .csc-firstHeader').css('color','#eaeaea');

	      }
        for (i = 1; i < maxMenueCount+1; ++i) {
	       var linkcID = i;
         var linkIDname = 'ajaxLink'+linkcID.toString();
         getElementsByClassNameAndReplace(linkIDname,linkcID);
	       }  	       
        }
		/*document.getElementById("uniqueContent").style.visibility = "visible";*/
	}
}

/** END Change partial page content with Ajax **/ 
     
