/* ************************************************************************************* *\
 * The MIT License
 * Copyright (c) 2007 Fabio Zendhi Nagao - http://zend.lojcomm.com.br
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this
 * software and associated documentation files (the "Software"), to deal in the Software
 * without restriction, including without limitation the rights to use, copy, modify,
 * merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to the following
 * conditions:
 * 
 * The above copyright notice and this permission notice shall be included in all copies
 * or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
 * PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 * 
\* ************************************************************************************* */

var iCarousel = new Class({
	options: {
		animation: {
			type: "fadeNscroll",// fadeNscroll, scroll, fade
			direction: "left",// if type = scroll, set: top || left
			amount: 1,// if type = scroll, set the amount  scroll
			transition: Fx.Transitions.Cubic.easeInOut,
			duration: 500,
			rotate: {
				type: "manual",// auto || manual
				interval: 5000,// if type = auto, set the interval (ms)
				onMouseOver: "stop"// if type = auto, set the onmouseover behavior: stop || proceed
			}
		},

		item: {
			klass: "item",
			size: 100
		},

		idPrevious: "previous",
		idNext: "next",
		idToggle: "toggle",

		onClickPrevious: Class.empty,
		onClickNext: Class.empty,
		onPrevious: Class.empty,
		onNext: Class.empty,
		onGoTo: Class.empty
	},

	initialize: function(container, options) {
		this.setOptions(options);
		this.container = $(container);
		//this.aItems = $A($$('.'+ this.options.item.klass));
		this.aItems = $$('.' + this.options.item.klass);
		this.isMouseOver = false;

		if(this.options.idPrevious != "undefined" && $(this.options.idPrevious))
			$(this.options.idPrevious).addEvent("click", function(event) {
				new Event(event).stop();
				this._previous();
				this.fireEvent("onClickPrevious", this, 20);
			}.bind(this));// check if value is not "undefined" before start search the dom with $()
		if(this.options.idNext != "undefined" && $(this.options.idNext))
			$(this.options.idNext).addEvent("click", function(event) {
				new Event(event).stop();
				this._next();
				this.fireEvent("onClickNext", this, 20);
			}.bind(this));
		if(this.options.idToggle != "undefined" && $(this.options.idToggle))
			$(this.options.idToggle).addEvent("click", function(event) {new Event(event).stop(); this._toggle()}.bind(this));
		
		var oAn = this.options.animation; // short hand
		switch(this.options.animation.type.toLowerCase()) {
			case "fade":
				this.aItems.each(function(item) {
					//item.fx = item.effect("opacity", {duration: oAn.duration, transition: oAn.transition});
					item.fx = new Fx.Morph(item, {duration: oAn.duration, transition: oAn.transition, wait: false}).set({'opacity': 0});
					item.setStyle("opacity", 0);
					item.addEvents({
						"mouseenter": function() {
							this.isMouseOver = true;
							if(this.options.animation.rotate.type == "auto") this.timer = $clear(this.timer);
						}.bind(this),
						"mouseleave": function() {
							this.isMouseOver = false;
							if(this.options.animation.rotate.type == "auto") this.timer = this._autoRotate.periodical(this.options.animation.rotate.interval, this);
						}.bind(this)
					});
				}.bind(this));
				this.height = this.container.getStyle("height").toInt();
				this.atScreen = 0;
				this._animate(this.atScreen);
				break;

			default:
				var count = 0;
				(2).times(function() {
					this.aItems.each(function(item) {
						var it = item.clone();
						//it.set('class');
						it.getElement('a').rel= it.getElement('a').rel+count;
						it.injectInside(this.container);
					}.bind(this));
					count++;
				}.bind(this));
				this.aItems = $A($$('.'+ this.options.item.klass));
				this.aItems.each(function(item) {
					item.addEvents({
						"mouseenter": function() {
							this.isMouseOver = true;
							if(this.options.animation.rotate.type == "auto") this.timer = $clear(this.timer);
						}.bind(this),
						"mouseleave": function() {
							this.isMouseOver = false;
							if(this.options.animation.rotate.type == "auto") this.timer = this._autoRotate.periodical(this.options.animation.rotate.interval, this);
						}.bind(this)
					});
				}.bind(this));
				//this.fx = this.container.effects({duration: oAn.duration, transition: oAn.transition, wait: false});
				this.fx = new Fx.Morph(this.container, {duration: oAn.duration, transition: oAn.transition, wait: false});
				this.atScreen = this.aItems.length / 3;
				this.container.setStyle(oAn.direction, - this.atScreen * this.options.item.size);
				break;
		}

		if(this.options.animation.rotate.type == "auto") this.timer = this._autoRotate.periodical(this.options.animation.rotate.interval, this);
		
		$$(".imggallerylink").slimbox({/* Put custom options here */}, null, function(el) {
			return (this == el) || ((this.rel.length > 8) && (this.rel == el.rel));
		});
		
		
	},

	goTo: function(n) {
		switch(this.options.animation.type.toLowerCase()) {
			case "fade":
				var lastIndex = this.atScreen;
				this.atScreen = Math.abs(n % (this.aItems.length / 3));
				this._animate(this.atScreen, lastIndex);
				break;

			default:
				this.atScreen = Math.abs(n % (this.aItems.length / 3));
				this.atScreen += this.aItems.length / 3;
				this._animate(this.atScreen);
				break;
		}

		this.fireEvent("onGoTo", this, 20);
	},

	_previous: function() {
		switch(this.options.animation.type.toLowerCase()) {
			case "fade":
				var lastIndex = this.atScreen;
				this.atScreen -= this.options.animation.amount;
				if(this.atScreen < 0) this.atScreen = (this.aItems.length - 1);
				this._animate(this.atScreen, lastIndex);
				break;

			default:
				this.atScreen -= this.options.animation.amount;
				if(this.atScreen < this.aItems.length / 3) {
					this.container.setStyle(this.options.animation.direction, - this.options.item.size * this.aItems.length * 2 / 3);
					this.atScreen = this.aItems.length * 2 / 3 - this.options.animation.amount;
				}
				this._animate(this.atScreen);
				break;
		}

		this.fireEvent("onPrevious", this, 20);
	},

	_next: function() {
		switch(this.options.animation.type.toLowerCase()) {
			case "fade":
				var lastIndex = this.atScreen;
				this.atScreen += this.options.animation.amount;
				if(this.atScreen >= this.aItems.length) this.atScreen = 0;
				this._animate(this.atScreen, lastIndex);
				break;

			default:
				this.atScreen += this.options.animation.amount;
				if(this.atScreen > this.aItems.length * 2 / 3) {
					this.container.setStyle(this.options.animation.direction, - this.options.item.size * this.aItems.length / 3);
					this.atScreen = this.aItems.length / 3 + this.options.animation.amount;
				}
				this._animate(this.atScreen);
				break;
		}

		this.fireEvent("onNext", this, 20);
	},

	_toggle: function() {
		(this.container.getStyle("height").toInt() == 0) ?
			this.container.effect("height", { duration:1000, transition: Fx.Transitions.Sine.easeInOut }).start(this.height):
			this.container.effect("height", { duration:1000, transition: Fx.Transitions.Sine.easeInOut }).start(0);
	},

	_autoRotate: function() {
		if(this.options.animation.rotate.onMouseOver == "stop" && !this.isMouseOver) this._next();
	},

	_animate: function(a, b) {
		
		switch(this.options.animation.type) {
			case "fade":
				if($defined(b)) {
					this.aItems[b].fx.start(0).chain(function() {
					this.aItems[a].fx.start(1);}.bind(this));
				} else {
					this.aItems[a].fx.start(1);
				}
				break;

			case "scroll":
				var that = this;
				if(that.options.animation.direction == "top") {
					that.fx.start({"top" : - a * that.options.item.size});
				} else {
					that.fx.start({"left" : - a * that.options.item.size});
				}
				break;

			case "fadeNscroll":
				var that = this;
				if(that.options.animation.direction == "top") {
					that.fx.start({"opacity":0.75}).chain(function() {
						that.fx.start({"top" : - a * that.options.item.size}).chain(function() {
							that.fx.start({"opacity": 1});
						});
					});
				} else {
					that.fx.start({"opacity":0.75}).chain(function() {
						that.fx.start({"left" : - a * that.options.item.size}).chain(function() {
							that.fx.start({"opacity": 1});
						});
					});
				}
				break;
		}
	}
});
iCarousel.implement(new Events); // Implements addEvent(type, fn), fireEvent(type, [args], delay) and removeEvent(type, fn)
iCarousel.implement(new Options);// Implements setOptions(defaults, options)

var carousel = null;
document.addEvent("domready", function() {
	
	if($('smmhamburg')) {
		$('smmhamburg').store("tip:text", '<img src="frontend/pics/smm_4c_uz.jpg" alt="SMM Hamburg" style="border:1px solid #000;" />');
		new Tips($('smmhamburg'));
	}
	
	if($('achema')) {
		$('achema').store("tip:text", '<img src="frontend/pics/achema_2012_siegelmarke_rgb.jpg" alt="ACHEMA" style="border:1px solid #000;width:200px;" />');
		new Tips($('achema'));
	}
	
	$$("img").each(function(el) {
		//alert(el.get("id"));


		if(el.hasClass("untertext")) {

			var div = new Element("div");
			div.addClass("untertext");
			div.setStyle("float", el.getStyle("float"));
			div.setStyle("margin", el.getStyle("margin"));
			div.setStyle("width", el.getStyle("width"));
			el.setStyle("margin", "0");
			var p = new Element("p");
			p.set("html", el.get("title"));

			div.wraps(el);
			p.inject(div);
		}
				
			
		if(el.get("id") && el.get("id").test(/^lightbox/i)) {
			var link = new Element("a");
			link.set("href", el.get("src")).set("rel", "lightbox-inline").set("title", el.get("title"));
			link.wraps(el);
		}
		
	});
	if(window.Slimbox) Slimbox.scanPage();
	
	var lastToggled = null;
	$$('.sliders a').each(function(el) {
		if(el.get("href") == "#" && (el.getParent().getNext())) {
			// Dieser Link dient als "Klapp-Box"-Link
			var c = el.getParent().getNext();
			//if(!c) c = el.getParent().getNext("p");
			
			var div = new Element("div");
			div.addClass("PullDown").wraps(c);
			
			var p = el.getParent();
			el.inject(c, "before");
			p.destroy();
			
			c.slide('hide');
			el.addEvent("click", function() {
				if(lastToggled != null && lastToggled != c) {
					lastToggled.slide('out');
					$('content').getElements("a").removeClass("active");
				}
				lastToggled = c;
				if(this.hasClass("active")) this.removeClass("active");
				else this.addClass("active");
				c.slide('toggle');
				return false;
			});
		}
	});
	
	var container = $("scrollcontent");
	if(container) {
		var scrollItems = container.getChildren(".imagegallery");
		scrollItems.each(function(s) {
			var link = s.getElement("a")
			link.setStyles({
				top: "50%",
				'margin-top': '-'+(Math.round(link.getSize().y/2))+"px"
			});
		});
		if(scrollItems.length > 3) {
			//scrollItems.each(function(obj) {
				//obj.setStyle("display", "none");
			//});
			$('gallery_container').setStyle("overflow", "hidden");
			$('gallery_container').setStyle("height", "516px");
			
			carousel = new iCarousel("scrollcontent", {  
						//idPrevious: "logo",  
						//idNext: "logo",  
						idToggle: "undefined",  
						item: {  
							klass: "imagegallery",  
							size: 172
						},  
						animation: { 
							type: "scroll",
							direction: "top",  
							duration: 3000
						}  
			});
			
			window.setTimeout("scrollImages()",2000);
			
		}
	}
	
	
});

function scrollImages() {
	if(carousel) carousel._next();
	window.setTimeout("scrollImages()",5000);
} 
