
var AjaxLoad = {
	currentFocus: undefined,
	blend: undefined,
	centeredElement: undefined,
	currentLocation: window.location.href.split("?")[0],
	preLoadView: "replace",
	configuration: undefined,
	
	init: function (configuration) {
		this.configuration = {
				EffectLightbox: {
					appear: {
						effect: "Appear",
						duration: 0.7,
						transition: "linear" 		
					},
					disappear: {
						effect: "DropOut",
						duration: 0.3,
						transition: "linear" 		
					}						
				}
		};
	
	
	},
	
	setConfiguration: function (configuration, configurationPath) {
		/*called by controller*/
		
		
		/*override the defaultvalues*/
		if (!configurationPath) configurationPath = "AjaxLoad.configuration";
		$H(configuration).each(function(pair){
			if (typeof(pair.value)=="object") {
				AjaxLoad.setConfiguration(pair.value, configurationPath+"."+pair.key);				
			} else {
				eval(configurationPath+"."+pair.key+"='"+pair.value+"';");
			}
		});
		
	},
	
	handleClick: function (event) {
	
       var data = $A(arguments); var componentPath = data[1];var effect = data[2];
      
      
       if (this.href==window.location.href.split("?")[0]+"#" || this.target=="_blank") { return;}
		
		
		if (this.href.split("?")[0] != AjaxLoad.currentLocation) {
			var loadMode = "partialAsk";
		} else {
			var loadMode = "partial";
		}
	
		
		/*use suitable effect from rel*/		
		if (this.rel !="redirect" && this.rel !="close" && this.href) {
			/*Strenght: Skin, Rel, setLoadInPlace */
			if(this.rel=="submit") {
				
				var a=this;
				var myForm;
				
				$$("form").each(function(f) {
				
					if (Element.descendantOf(a,f)) {
						
						myForm = f;
					} 
				});
				
				if (!myForm) alert("a not in form");
				
				AjaxLoad.doLoad(a.href, "post",  "partial", myForm.serialize(true), componentPath, (effect ? effect: this.rel), "", this);
				Event.stop(event);
				
			} else {
				if (this.rel) {effect = this.rel;} 
		
				AjaxLoad.doLoad(this.href, "get",  loadMode,"", componentPath, effect, "", this);					
				Event.stop(event);
			}
		}
	
	},
		
	
	initComponent: function (componentPath, effect) {
		
		$$("div#"+componentPath+" a").each(function(element) {
			Event.observe(element, 'click', AjaxLoad.handleClick.bindAsEventListener(element, componentPath, effect));
		});

		
		$$("div#"+componentPath+" a").invoke("observe", "mouseover", function(event) {
	
			if (this.rel =="hover") {
				
				//AjaxLoad.preLoadView = "classRewrite";
				AjaxLoad.doLoad(this.href, "get",  "partial","", componentPath, "hover", "", this);					
				Event.stop(event);
			}
		
		});	
		
		$$("div#"+componentPath+" form").invoke("observe", "submit", function(event) {

			var form2 = this;
			
			$$("div#"+componentPath+" form input, div#"+componentPath+" form textarea").each(function(input) {
				//input.disable();
			});
		
			AjaxLoad.doLoad(form2.action, "post",  "partial", form2.serialize(true), componentPath, (effect ? effect: this.rel), "", this);				
			
			Event.stop(event);		
			
		});		
		
		//AjaxLoad.applyEffect($(componentPath));
		
		
	}, 
	refreshComponent: function (componentPath, effectType) {
		
		/*no implemented*/
		
	},
	
	doLoad: function (url, method, loadMode, parameters, componentPath, effectType, onSuccess, caller) {
		
			if (effectType=="replace") AjaxLoad.preLoadView = "classRewrite";
			if (effectType=="inline") AjaxLoad.preLoadView = "classRewrite";
		
			if (AjaxLoad.preLoadView =="replace") {
				
				divId = AjaxLoad.updateContent(componentPath, "<div class='overlayLoading'><div>loading ... </div></div>", effectType,caller);

			} else if (AjaxLoad.preLoadView =="classRewrite") {
				
				$(componentPath).addClassName ("loading");
			}
			
			caller.addClassName("active");
		
			new Ajax.Request(url+(url.indexOf("?")>=0 ? "&": "?")+"loadMode="+loadMode, {
					method: method,
					parameters: parameters,
					onSuccess: function(transport) {
			
						if (transport.responseText.substr(0,9)=="REDIRECT:") {
							if (transport.responseText.substr(9)) {
								window.location.href = transport.responseText.substr(9);
							} else {
								window.location.href = url;
							}
							
						} else {
							
							
							try {
								var json = transport.responseText.evalJSON(true);
						    }
						    catch(e) {
								var div = document.createElement("div");
								document.body.appendChild(div);
								div.style.textAlign="left";
								div.innerHTML = transport.responseText;
								return;
						    }
									
						
							json.componentResults.each(function (componentResult) {
							
								
								if (componentResult.isError) {/*Exceptions*/
									var div = document.createElement("div");
									document.body.appendChild(div);
									div.style.textAlign="left";
									div.innerHTML = componentResult.content;
									return;
								}
								//if (componentResult.effect) effectType = componentResult.effect;
						
								
								
								
								divId = AjaxLoad.updateContent(componentResult.location, componentResult.content, effectType,caller);

								
								$(divId).removeClassName ("loading");
								
								if ($(componentResult.location)) $(componentResult.location).removeClassName ("loading");
							
								
								AjaxLoad.initComponent(divId, effectType);
								
								
								
								AjaxLoad.applyEffect($(divId), componentPath == divId);
								
								
							});
						}
						
						

					}
				});		
			
	}, 
	
	addCloseHandler: function (effect, element) {
		
		AjaxLoad.closeHandlerEffect = effect; 
	
	},
	
	executeCloseHandlers: function () {
		
		if (AjaxLoad.closeHandlerEffect) eval(AjaxLoad.closeHandlerEffect+".hide()");
	
	},	
	
	updateContent: function (divId, newContent, effectType, caller) {
		//AjaxLoad.executeCloseHandlers();


		if (this.newDiv && (this.newDiv.innerHTML.indexOf("loading")!=-1 || effectType =="overlay")) this.newDiv.remove();	

		
		this.newDiv = new Element('div', {id: "effectBox"+ effectType +Math.round(Math.random()*10000)}); 
		this.newDiv.innerHTML = newContent;	
		
		
		var baseEffect = AjaxLoad.getBaseEffect(this.newDiv);
		
		
		if (baseEffect) var newEffect = eval(baseEffect+".effectType") ;
		if (newEffect) {effectType =newEffect;}
			
		if (!effectType) {effectType ="replace";}
		
	
		
		if (effectType =="overlay") {
			
	
			this.blendScreen(true);
			this.newDiv.setStyle({float:"left"});	
			$$("body")[0].insert(this.newDiv);	
			AjaxLoad.centerElement(this.newDiv);
			
			
		} else if (effectType =="replace") {
			
			this.newDiv = $(divId);
			
			$(divId).innerHTML = this.newDiv.innerHTML;	
			this.newDiv.innerHTML = newContent;	
	
		} else if (effectType =="inline") {

			 caller.insert({after: this.newDiv});
			
		} else if (effectType =="hover") {
			
			caller.insert({after: this.newDiv});
			
		}
		

		
		newContent.evalScripts();		
		
		
		/*preload images*/
		
		$$("div#"  +this.newDiv.id + " img").each(function (img) {
			var imgNew = new Image()
			imgNew.src = imgNew.src;
		});
		
		
		return this.newDiv.id;
			
	},
	
	getBaseEffect: function(element) {
		var effect;
		element.descendants().each(function (subElement){
			if (!effect) {
				$w(subElement.className).each(function (c){
	
					if(c.substr(0,6)=="Effect") {
						
						effect = c;
					}
				});
			}
					
		});
		
		
		return effect;
		
		
	},	
	
	applyEffect: function(element, changeOnly) {

		var effect = this.applyEffectOnElement(element, element, changeOnly);
		
	
		element.descendants().each(function (subElement){

		
			var subEffect =  AjaxLoad.applyEffectOnElement(element, subElement, changeOnly);
			
			
			if (!effect && subEffect) effect = subEffect;
			
			if(subElement.rel=="close") {
				
				subElement.observe("click", function(event) {
					
					AjaxLoad.preLoadView = "replace";
					eval("Effect"+effect+".hide()");
					
					Event.stop(event);
					
				});
			}
		

		});
		
		
		AjaxLoad.addCloseHandler("Effect"+effect);
		
		
	},
	applyEffectOnElement: function(baseElement, element, changeOnly) {
		
		var effect;

		$w(element.className).each(function (c){
			
			if(c.substr(0,6)=="Effect") {
				
				effect = c.substr(6);
				if (changeOnly) {
				
					eval("Effect"+effect+".change(baseElement, element)");
				} else {
					
					eval("Effect"+effect+".show(baseElement, element)");
				}
			}
					
		
		});
	
		return effect;
		
	
	},
	blendScreen: function(visible) {
	
		var preVisible = (this.blend && this.blend.getStyle("display")=="block");
		if (preVisible==visible) {/*allready visible is upto date*/
			
			return false;
		}	
		
		
		if (!this.blend) {
			
			this.blend = new Element("div", {className: "overlay"});
			var size = AjaxLoad.getPageSize();
			this.blend.setStyle({width: size[0]+"px"});
			this.blend.setStyle({height: size[1]+"px"});
			this.blend.setStyle({top: "0px"});
			this.blend.setStyle({left: "0px"});	
			this.blend.setStyle({position:"absolute"});
			$$("body")[0].insert({"bottom": this.blend});	
		
		}
		
				
		if (visible) {
			this.blend.setStyle({display:"block"});		
		} else {
			Effect.Fade(this.blend, {duration:0.3});
			
		}
		return true;
		
		
		
	},
	centerElement: function(div) {		

		if (AjaxLoad.centeredElement) {/*destroy previous centered elements*/
			
			AjaxLoad.centeredElement.setStyle({top:"0px"});
			AjaxLoad.centeredElement.setStyle({left:"0px"});
			AjaxLoad.centeredElement.setStyle({width:"auto"});
			AjaxLoad.centeredElement.setStyle({height:"auto"});
		}
				
		var topOffset=-10; /*%*/
		
		var height = div.getHeight();
		var width = div.getWidth();	
		
		div.style.zIndex="99999";
		div.style.position="absolute";
		
		div.style.top=(((document.viewport.getScrollOffsets()["top"] +(document.viewport.getHeight()-height)/2))*(1+topOffset/100))+"px";
		div.style.left=(document.viewport.getScrollOffsets()["left"] +(document.viewport.getWidth()-width)/2)+"px";

		div.style.width=width+"px";
		div.style.height=height+"px";	
		

		AjaxLoad.centeredElement = div;
		
	},
	getPageSize: function() {
		        
		     var xScroll, yScroll;
			
			if (window.innerHeight && window.scrollMaxY) {	
				xScroll = window.innerWidth + window.scrollMaxX;
				yScroll = window.innerHeight + window.scrollMaxY;
			} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
				xScroll = document.body.scrollWidth;
				yScroll = document.body.scrollHeight;
			} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
				xScroll = document.body.offsetWidth;
				yScroll = document.body.offsetHeight;
			}
			
			var windowWidth, windowHeight;
			
			if (self.innerHeight) {	// all except Explorer
				if(document.documentElement.clientWidth){
					windowWidth = document.documentElement.clientWidth; 
				} else {
					windowWidth = self.innerWidth;
				}
				windowHeight = self.innerHeight;
			} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
				windowWidth = document.documentElement.clientWidth;
				windowHeight = document.documentElement.clientHeight;
			} else if (document.body) { // other Explorers
				windowWidth = document.body.clientWidth;
				windowHeight = document.body.clientHeight;
			}	
			
			// for small pages with total height less then height of the viewport
			if(yScroll < windowHeight){
				pageHeight = windowHeight;
			} else { 
				pageHeight = yScroll;
			}
		
			// for small pages with total width less then width of the viewport
			if(xScroll < windowWidth){	
				pageWidth = xScroll;		
			} else {
				pageWidth = windowWidth;
			}

			return [pageWidth,pageHeight];
		}		
	
}







var EffectSlide = {
	overlay: undefined,
	currentDiv: undefined,
	lastHeight: undefined,

	change: function (baseElement, div) {
		
		div.setStyle({display:"block"});
	},	
	
	
	show: function (baseElement, div) {
	
		this.currentDiv = div;
		Effect.SlideDown(div,{duration:1});
			
		
		/*
		new PeriodicalExecuter(function(pe) {
			Effect.ScrollTo(div,{duration:1});
			pe.stop();
		}, 1);			
		*/
			
	},
	hide: function (div) {

		Effect.SlideUp(this.currentDiv,{duration:0.5});
	
		this.currentDiv = "";
		AjaxLoad.newDiv.remove();
		AjaxLoad.newDiv = "";
	
		
	}	
}		


var EffectAppear = {
	overlay: undefined,
	currentDiv: undefined,
	lastHeight: undefined,
	
	
	show: function (div) {
		
		if (this.currentDiv) {
			div.setStyle({display:"block"});

		} else {
			
			this.currentDiv = div;
			Effect.Appear(div,{duration:1});
			
		}
		
			
	},
	hide: function (div) {
		Effect.Fade(this.currentDiv,{duration:0.5});
		this.currentDiv = "";
	
	}	
}		
var EffectScrollTo = {
		overlay: undefined,
		currentDiv: undefined,
		lastHeight: undefined,
		
		
		
		
		show: function (div) {
		
			if (!this.lastHeight) {
				this.lastHeight = div.getHeight();
			}
		
			div.setStyle({height:this.lastHeight+"px"});
			
			new PeriodicalExecuter(function(pe) {
				Effect.ScrollTo(div, {duration:1});
				pe.stop();
			}, 0.2);				
			
		
				
		},
		hide: function (div) {
			Effect.Fade(this.currentDiv,{duration:0.5});
			this.currentDiv = "";
		
		}	
}		






var EffectLightbox = {
	overlay: undefined,
	currentDiv: undefined,
	effectType: "overlay",
	
	change: function (baseElement, div) {
		
		div.setStyle({display:"block"});
		AjaxLoad.centerElement(div);
	},	
	
	show: function (baseElement, div) {
		
	
		var didSomething = AjaxLoad.blendScreen(true); 
			

		AjaxLoad.preLoadView = "classRewrite";
		div.setStyle({display:"none"});		
		
		var parameters = {duration:AjaxLoad.configuration.EffectLightbox.appear.duration, transition: eval("Effect.Transitions."+AjaxLoad.configuration.EffectLightbox.appear.transition)};
			
		if ((AjaxLoad.configuration.EffectLightbox.appear.effect)=="Appear") Effect.Appear(div, parameters);
		if ((AjaxLoad.configuration.EffectLightbox.appear.effect)=="SlideDown") Effect.SlideDown(div, parameters);
		if ((AjaxLoad.configuration.EffectLightbox.appear.effect)=="Grow") Effect.Grow(div, parameters);
		
		AjaxLoad.centerElement(div);
		this.currentDiv = div;
		
		div.setStyle({visibility:"visible"});	
		
	
	},
	hide: function () {
		
		
		var parameters = {duration:AjaxLoad.configuration.EffectLightbox.disappear.duration, transition: eval("Effect.Transitions."+AjaxLoad.configuration.EffectLightbox.disappear.transition)};
		
		if ((AjaxLoad.configuration.EffectLightbox.disappear.effect)=="SlideUp") {Effect.SlideUp(this.currentDiv, parameters);	}
		if ((AjaxLoad.configuration.EffectLightbox.disappear.effect)=="DropOut") {Effect.DropOut(this.currentDiv, parameters);	}
		if ((AjaxLoad.configuration.EffectLightbox.disappear.effect)=="Fold") {Effect.Fold(this.currentDiv, parameters);	}
		if ((AjaxLoad.configuration.EffectLightbox.disappear.effect)=="Squish") {Effect.Squish(this.currentDiv, parameters);	}
		if ((AjaxLoad.configuration.EffectLightbox.disappear.effect)=="SwitchOff") {Effect.SwitchOff(this.currentDiv, parameters);	}	
		if ((AjaxLoad.configuration.EffectLightbox.disappear.effect)=="Fade") {Effect.Fade(this.currentDiv, parameters);	}	
	
		//this.currentDiv = "";
		new PeriodicalExecuter(function(pe) {
			AjaxLoad.blendScreen(false);
			pe.stop();
		}, AjaxLoad.configuration.EffectLightbox.disappear.duration+0.2);	
	}	
}		
AjaxLoad.init();






document.observe('dom:loaded', function () {
	
	//AjaxLoad.applyEffect($("main-main"));
	
	//EffectLightbox.show($("lightbox"));
	
	new PeriodicalExecuter(function(pe) {
		
		//EffectLightbox.hide($("lightbox"));
		
		pe.stop();
	}, 3);		
});


	
       

		          
