

/**************************************************************

	Script		: Page Loader
	Version		: 1.1
	Authors		: Samuel Birch, Markus Timtner
	Desc		: load pages via AJAX with mootools 1.2
	Licence		: Open Source MIT Licence

	*additional functionality    *
		BY primel.net
		for http://undwein.de
	*               2008-09-10                  *

	
**************************************************************/

var pageLoader = new Class({
	
	Implements: [Events, Options],
    options: {
		links: 		'.loadMe',
		loadInTo: 	'realcontent',
		loadFrom: 	'realcontent',
		onStart: 	$empty,
		onComplete: function(){ 
			this.initialize();
			initMinimizeRealcontent();
			initWeinTrigger();
 			initTagButtons();
 			initClearRealcontent();
 			//initThemenButtons();
 			//initAutoComplete();
 			initTips();
		}
    },	

	initialize: function(options){
		this.setOptions(options);
		this.links = $$(this.options.links);
		
		this.links.each(function(el,i){
			//console.log(el);
			el.addEvent('click',function(e){
				if(e != undefined){
					new Event(e).stop();
				}
				this.start(el);
				// die Tips abschalten, wenn einmal geklickt wurde
				if(el.hasClass('ShowTipFilter') || el.hasClass('ShowTipSortierung')){
					showTips = false;
				}
				
			}.bind(this));
		}.bind(this));
		
		// alternative zu destroy-binding, classe umbennen, wenn 
		// bind erfolgt, dadurch werden die links kein zweites mal 
		// angesprochen, ihr altes binding bleibt aber erhalten
		this.links.each(function(el,i){
			el.removeClass('loadMe');
			el.addClass('loadMeTrue');
				
		});
		
		
		
		//console.log(this);
	},
	
	checkTarget: function(){
		
		// wenn realcontent minimiert ist, groß machen
		if($('getcontentback')){
			removedContent.inject('content', 'bottom');
			$('getcontentback').destroy();
		}
		
		//console.log($('minimize_content'));
		// close button erzeugen
		if($('minimize_content') == null){
		
			var rundboxoben = new Element('div', {
				'id': 'oben_breit'
			}).inject($('realcontent'), 'top');
			
			var minimize_contentBoxBut = new Element('div', {
				'id': 'boxfunction',
				'class': 'boxfunction inline right'
			}).inject($('realcontent'), 'top');
	
			var minimize_contentBut = new Element('div', {
				'id': 'minimize_content',
				'class': 'self_minimize_realcontent pointer',
				'html' : '<img width="16" height="16" src="/fileadmin/templates/images/minimize.png" alt="minimize" title="minimize" >'
			}).inject($('boxfunction'), 'top');
			
			var close_contentBut = new Element('div', {
				'id': 'clear_realcontent',
				'class': 'pointer',
				'html' : '<img width="16" height="16" title="close" alt="close" src="/fileadmin/templates/images/close.png"/>'
			}).inject($('boxfunction'), 'top');
			
			
			
			
			var rundboxunten = new Element('div', {
				'id': 'unten_breit'
			}).inject($('realcontent'), 'bottom');
			
		}
		//console.log($('minimize_content'));
		
		
		
	},

	
	setContent: function(reqResponse){
		//console.log(reqResponse);
		
		var tempContent = $(this.options.loadInTo).set({ id:'tempContent' });
		
		var temp = new Element('div').set({ id:'temp', display:'block' }).injectInside(document.body);
		temp.set({html:reqResponse});
		
		var newEl = $('temp').getElement('#'+this.options.loadFrom);
		newEl.replaces(tempContent);
		newEl.set({ id:this.options.loadInTo });
		$(this.options.loadInTo).setStyle('height','');
		this.checkTarget();
		temp.destroy();
		
	},
	
	
	
	start: function(el){
		// explicitly wait some time for stupid ie
		(function(){ this.fireEvent('onStart'); }).delay(256);
		
		// here the previously selected element is reset.
		// if( $('selected')!= null ) $('selected').set({ id:'' });
		
		this.content = new Request({  
			method: 	'get',  
			url: 		el.href,
            onRequest:	function() {
            	
            	if($('getcontentback')){
            			// wenn realcontent minimiert ist, groß machen
						realcontent = document.createElement("div");
						realcontent.setAttribute('id', 'realcontent');
						realcontent.inject('content', 'bottom');
						$('getcontentback').destroy();
						
            	}

				// wenn warenkorb angezeigt wird, klein machen
				if($('bestellung')){
				
					minimizeForm();
				}
            		var hohe_tmp = $('realcontent').getStyle('height');
            		if(hohe_tmp == '0px'){
            			hohe_tmp = '100px';
            		}
            		$('realcontent').setStyle('height',hohe_tmp);
					$('realcontent').getChildren('.contentfade').each(function(el){
						el.fade('out');
					});
					if($('unten_breit')){
						$('unten_breit').destroy();
					}
					if($('oben_breit')){
						$('oben_breit').destroy();
					}
					if($('boxfunction')){
						$('boxfunction').destroy();
					}
					$('realcontent').setStyle('background', '#ffffff url(/fileadmin/templates/images/loader1.gif) no-repeat center center');
				
				
								
			},
			onComplete: function(result) {
				myresult = result;
				this.complete(myresult);
				// explicitly wait some time for stupid ie
				(function(){ $('realcontent').fade('in'); }).delay(384);
								
			}.bind(this),
			autoCancel: true
		}).get({'rand':$random(0, 4096)});

		// current element should be formatted differently
		// el.set({ id:'selected' });
		
	},
	
	complete: function(reqResponse){
		this.setContent(reqResponse);
		this.fireEvent('onComplete');
		
		//this.initialize();
	}

});

/*************************************************************/

