// -----------------------------------------------------------------------------------
//	PageTrans v 0.1 - 2006-11-17
//	by Sindre Wimberger - http://www.sindre.at
//
//	For more information on this script, visit:
//	
//
//	Licensed under the Creative Commons Attribution 2.5 License - http://creativecommons.org/licenses/by/2.5/
//	
//	Credit also due to those who have helped, inspired, and made their code available to the public.
//	Including:  Elisabeth Kühr and others.
// -----------------------------------------------------------------------------------
//
// PageTrans.init("x1",x2,x3,"x4",'x5');
//
// Configuration
// x1 || Container ID which content should be changed. must be identical in all sader links
// x2 || 1 - Fade/Appear, 2 - BlindUp/BlindDown, 3 - SlideUp/SlideDown, 4 - Shrink/Grow [Buggy], 5 - Pulsate
// x3 || true - loadingcontent appears, false - no loading message
// x4 || ID of the lodingcontainer 
// x5 || HTML which will be insertet in the lodingcontainer 
// -----------------------------------------------------------------------------------
var PageTrans = {
	init: function(ContainerID,effect,loadingMessage,loadingID,loadingContent) {
		scontainer = ContainerID;
		fx = effect;
		loadingmessage = loadingMessage; 
		loadingid = loadingID; 
		loadingcontent = loadingContent; 
		PageTrans.getLinks();
	},
    getLinks: function() {
		if (!document.getElementsByTagName){ return; }
		if (!document.getElementById(loadingid) && loadingmessage == true){PageTrans.createLoader(); }		
		var anchors = document.getElementsByTagName('a');
		// loop through all anchor tags
		for (var i=0; i<anchors.length; i++){
			var anchor = anchors[i];
			var relAttribute = String(anchor.getAttribute('rel'));
			// use the string.match() method to catch 'sader' references in the rel attribute
			if (anchor.getAttribute('href') && (relAttribute.toLowerCase().match('pagetrans'))){
				anchor.onclick =  
				function() {
					PageTrans.disappearFX(this.getAttribute('href'));
					StateManager.setState(this.getAttribute('href'));
					return false;
			     }
		    }
		}		
	}, 	
    request: function(url) {
			if(typeof _gaq != "undefined") {
//				console.info('track ' + url);
				_gaq.push(['_trackPageview', url]);
			}
			if (loadingmessage == true) {PageTrans.LoaderAppear();}		
//         alert("request");	
			if (PageTrans.lightbox) {
				params = 'ajax=1&path='+url;
				url = '/recommend/';
			} else {
				params = 'ajax=1';
			}
		new Ajax.Request(url, {
		 	parameters : params,
			onFailure : function(resp) {PageTrans.redirect(url,resp.statusText);},
			onException : function(resp,ex) {PageTrans.redirect(url,ex);},
			onComplete:function(req){				
				PageTrans.insert(req.responseXML);
			}});
	},
    insert: function(XML) {
		var page = PageTrans.getMarkupById(XML);
		$('nachtderwirtschaft-at').className = page["bodyclass"];
		document.title = page["title"];
		$(scontainer).innerHTML = page["content"];
		page["content"].evalScripts();
		if (loadingmessage == true) {
			PageTrans.LoaderDISAppear();			
		} else {
        	PageTrans.appearFX();
		}
	},
    disappearFX: function(url) {
		if (fx==1) {new Effect.Fade(scontainer,
									 { duration:'0.3',
 									   afterFinish: function(){PageTrans.request(url)},
									   queue: { scope: 'aktive', position: 'front'}
									 });
		}
		if (fx==2) {new Effect.BlindUp(scontainer,
									 { duration:'1.2',
									   afterFinish: function(){PageTrans.request(url)},
									   queue: { scope: 'aktive', position: 'front'}
									 });
		}
		if (fx==3) {new Effect.SlideUp(scontainer,
									 { duration:'1.2',
									   afterFinish: function(){PageTrans.request(url)},
									   queue: { scope: 'aktive', position: 'front'}
									 });
		}
		if (fx==4) {new Effect.Shrink(scontainer,
									{ duration:'1.2',
									  afterFinish: function(){PageTrans.request(url)},
									  queue: { scope: 'aktive', position: 'front'}
									});
		}
		if (fx==5) {PageTrans.request(url);}
		
	},
    appearFX: function() {
		if (fx==1) {new Effect.Appear(scontainer,
									 { duration:'0.3',
 									   afterFinish: function(){PageTrans.functionJS();},
									   queue: { scope: 'aktive', position: 'end'}
									 });
		}
		if (fx==2) {new Effect.BlindDown(scontainer,
									 { duration:'1.2',
 									   afterFinish: function(){PageTrans.functionJS();},
									   queue: { scope: 'aktive', position: 'end'}
									 });
		}
		if (fx==3) {new Effect.SlideDown(scontainer,
									 { duration:'1.2',
 									   afterFinish: function(){PageTrans.functionJS();},
									   queue: { scope: 'aktive', position: 'end'}
									 });
		}
		if (fx==4) {new Effect.Grow(scontainer,
									 { duration:'1.2',
 									   afterFinish: function(){PageTrans.functionJS();},
									   queue: { scope: 'aktive', position: 'end'}
									 });
		}
		if (fx==5) {new Effect.Pulsate(scontainer,
									 { duration:'1.2',
 									   afterFinish: function(){PageTrans.functionJS();},
									   queue: { scope: 'aktive', position: 'end'}
									 });
		}
	},
    getMarkupById: function(markup) {
		var pagedetail = Array();
		pagedetail["title"] = markup.getElementsByTagName("title")[0].childNodes[0].nodeValue;
		if(markup.getElementsByTagName("bodyclass")[0].childNodes[0]) {
			pagedetail["bodyclass"] = markup.getElementsByTagName("bodyclass")[0].childNodes[0].nodeValue;
		}
		pagedetail["content"] = markup.getElementsByTagName("content")[0].childNodes[0].nodeValue;
		if(markup.getElementsByTagName("nnav")[0].childNodes[0]) {
			pagedetail["nnav"] = markup.getElementsByTagName("nnav")[0].childNodes[0].nodeValue;
		} else {
			pagedetail["nnav"] = false;
		}
		if(markup.getElementsByTagName("snav")[0].childNodes[0]) {
			pagedetail["snav"] = markup.getElementsByTagName("snav")[0].childNodes[0].nodeValue;
		} else {
			pagedetail["snav"] = false;
		}
		if(markup.getElementsByTagName("module")[0].childNodes[0]) {
			pagedetail["module"] = markup.getElementsByTagName("module")[0].childNodes[0].nodeValue;
		}
		return pagedetail;
	}, 
    createLoader: function() {
		var objBody = document.getElementsByTagName("body").item(0);
		objLoading = document.createElement("div");
		objLoading.setAttribute('id',loadingid);
		objLoading.style.display = "none";
		objBody.appendChild(objLoading);
		Element.update(loadingid, loadingcontent);
	},
    LoaderAppear: function() {
		objLoading.style.display = "block";
	},
    LoaderDISAppear: function() {
		objLoading.style.display = "none";
		PageTrans.appearFX();
	},
    redirect: function(url,res) {
//		alert(res);
		window.location.href = url;
	},
	functionJS: function(){
	  
	  PageTrans.getLinks();
	  initLightbox();
	  if(PageTrans.lightbox) {  
		  myLightbox.start(PageTrans.lightboxURL,true)
	  }
	  (function(){ /*Use Object Detection to detect IE6*/ var m = document.uniqueID /*IE*/ && document.compatMode /*>=IE6*/ && !window.XMLHttpRequest /*<=IE6*/ && document.execCommand ; try{ if(!!m){ m("BackgroundImageCache", false, true) /* = IE6 only */ } }catch(oh){}; })();	  
	},	
	/*
	addEvent function from http://www.quirksmode.org/blog/archives/2005/10/_and_the_winner_1.html
	*/
	addEvent:function( obj, type, fn ) {
		if (obj.addEventListener)
			obj.addEventListener( type, fn, false );
		else if (obj.attachEvent)
		{
			obj["e"+type+fn] = fn;
			obj[type+fn] = function() { obj["e"+type+fn]( window.event ); }
			obj.attachEvent( "on"+type, obj[type+fn] );
		}
	}	
};


// "Import" the StateManager so we don't have to keep typing
// "EXANIMO.managers.StateManager".
var StateManager = EXANIMO.managers.StateManager;
StateManager.onstaterevisit = function(e) {
	if(e.id.indexOf("/media/image/") >= 0) {
		PageTrans.lightbox = true;
		PageTrans.lightboxURL = e.id;
	} else {
		PageTrans.lightbox = false;
	}
//	alert(PageTrans.lightbox);
	PageTrans.disappearFX(e.id);
	PageTrans.currenturl = e.id;
}        

//saddEvent(window, 'load', getSaderLinks);
PageTrans.addEvent(window, 'load', function(){
	PageTrans.init("c-content",1,true,"pagetransloading",'<p class="center"><img src="/assets/content/ajax-loader.gif" alt="Laden" /><br /><br />Seite wird geladen...</p>');
});
PageTrans.addEvent(window, 'load', function(){
	StateManager.defaultStateID = document.URL;
	StateManager.initialize();
});

