//--------------------------------------------------
/**
* JS versions 1.5 
* 
* @version    1.0.1
* @project    smartWeb 6
* @codename   swoop
* @package    js-css
* @subpackage admin
* @author     Daniel Schneider <ds@swoop-dev.de>
* @copyright  24YOU e.SOLUTIONS GmbH 2010 (c)
* @license    all rights reserved
* @build      nightly/1047
* @link       http://www.swoop-dev.de/
* @since      File available since Release 6.9.01
*/
//--------------------------------------------------

var stageSlider = new Class({

//--------------------------------------------------
// Public:Propertys

	version 	: "1.0.1", 
	config 		: {}, 
	local 		: {}, 
	debug 		: {}, 
	options 	: {"speed": 8000, "bullet": true, "play": true, "txtbar": true, "controler": true, "preview": false, "buttons": {"height": 80, "width": 27, "preview": 25}}, 
	positon 	: 0, 
	cords 		: null, 
	canvas 		: null, 
	interval 	: null, 
	points 		: [], 
	elements 	: [], 
	preview 	: null, 
	previews 	: [], 
	prvctrls 	: {}, 
	prvpositon 	: 0, 
	controler 	: {}, 
	data 		: [], 
	
//--------------------------------------------------

	initialize : function(ele, init){
		this.log("stageSlider.initialize");
		
		this.canvas = ele;
		this.controler = {"ctrlp": null, "ctrln": null, "scrls": null};
		this.prvctrls = {"ctrlp": null, "ctrln": null, "range": null, "scrls": null};
		
		if(typeof(swoop_cnf) == "object"){
			$extend(this.config, swoop_cnf);
			}
			
		if(typeof(swoop_local) == "object"){
			$extend(this.local, swoop_local);
			}
			
		$extend(this.options, init);

		window.addEvent("domready", (function(e){this.domready()}).bind(this));
		window.addEvent("load", (function(e){this.load()}).bind(this));
		}, 

//--------------------------------------------------

	domready : function(){
		this.log("stageSlider.domready");

		try{
			this.canvas = $(this.canvas);
			this.data = $A((this.options.data ? this.options.data : []));
			this.draw();
			
			if(this.options.play){
				this.interval = window.setInterval((function(){this.move(1)}).bind(this), this.options.speed);
				}
			}
		catch(e){
			this.error(e);
			}
		}, 

//--------------------------------------------------

	load : function(){
		this.log("stageSlider.load");
		
		try{
			}
		catch(e){
			this.error(e);
			}
		}, 

//--------------------------------------------------

	error : function(e){
		try{
			if((typeof(console) != "undefined") && ($type(console) == "object")){
				if((typeof(console.error) != "undefined") && ($type(console.error) == "function")){
					console.group("fdbCleanerClass", this);
					console.error(e);
					console.groupEnd();
					}
				}
			else{
				//alert(e);
				}
			}
		catch(e){
			}
		},

//--------------------------------------------------

	log : function(o){
		try{
			if((typeof(console) != "undefined") && ($type(console) == "object")){
				if((typeof(console.log) != "undefined") && ($type(console.log) == "function")){
					console.log(o);
					}
				}
			else{
				//alert(o);
				}
			}
		catch(e){
			}
		}, 

//--------------------------------------------------

	draw : function(){
		this.log("stageSlider.draw");
		
		try{
			if(($chk(this.canvas)) && ($type(this.canvas) == "element") && (this.canvas.tagName == "DIV")){
				this.cords = this.canvas.getCoordinates();
				
				var butts = this.options.buttons;
				var style = {"display": "block", "overflow": "hidden", "position": "relative", "top": 0, "left": 0, "height": this.cords.height, "width": this.cords.width};
					
				this.canvas.setStyles($merge(style, {"cursor": "pointer"}));
				
				for(var i=0;i<this.data.length;i++){
					this.elements.push(new Element("div", {"class": "stageSliderItem", "styles": $merge(style, {"position": "static", "float": "left", "background": "no-repeat center center", "background-image": "url('"+this.data[i].view+"')"})}));
					
					if(this.options.bullet){
						this.points.push(new Element("a", {"class": "stageSliderClick", "rel": i, "events": {"click": (function(e){this.click(new Event(e).target, e)}).bind(this)}}));
						}
					
					if(this.options.preview){
						var tmp = new Element("a", {"class": "stageSliderPreviewItem", "rel": i, "events": {"click": (function(e){this.previewClick(new Event(e).target, e)}).bind(this)}});

						tmp.grab(new Element("span", {"class": "stageSliderPreviewItem"}).adopt([
							new Element("img", {"class": "stageSliderPreviewItemLft", "src": this.data[i].thmb}), 
							new Element("span", {"class": "stageSliderPreviewItemRgt"}).adopt([new Element("strong", {"html": this.data[i].head}), new Element("br"), new Element("i", {"html": this.data[i].text})]) 
							]));
						
						this.previews.push(tmp);
						}
					}
				
				if(this.options.bullet){
					this.points.push(new Element("br", {"style": {"clear": "both", "display": "bock"}}));
					}
				
				if(this.options.controler){
					this.controler.ctrlp = new Element("a", {"class": "stageSliderCtrl stageSliderCtrl-prv", "events": {"click": (function(e){this.prev(e);}).bind(this)}, "styles": $merge(style, {"position": "absolute", "top": ((this.cords.height + butts.height) / 2.2) - butts.height, "height": butts.height, "width": butts.width, "left": (butts.width)})});
					this.controler.ctrln = new Element("a", {"class": "stageSliderCtrl stageSliderCtrl-nxt", "events": {"click": (function(e){this.next(e);}).bind(this)}, "styles": $merge(style, {"position": "absolute", "top": ((this.cords.height + butts.height) / 2.2) - butts.height, "height": butts.height, "width": butts.width, "left": (this.cords.width - (butts.width * 2))})});
					}
				else{
					this.controler.ctrlp = new Element("span", {"class": "stageSliderCtrlNone"});
					this.controler.ctrln = new Element("span", {"class": "stageSliderCtrlNone"});
					}
				
				if(this.options.preview){
					this.preview = new Element("div", {"class": "stageSliderPreview", "styles": {"height": this.cords.height}});
					
					this.prvctrls.ctrlp = new Element("a", {"class": "stageSliderPreviewCtrl stageSliderPreviewCtrl-prv", "styles": {"height": butts.preview}, "events": {"click": (function(e){this.prev(e);}).bind(this)}});
					this.prvctrls.ctrln = new Element("a", {"class": "stageSliderPreviewCtrl stageSliderPreviewCtrl-nxt", "styles": {"height": butts.preview}, "events": {"click": (function(e){this.next(e);}).bind(this)}});
					this.prvctrls.range = new Element("div", {"class": "stageSliderPreviewRange", "styles": {"height": (this.cords.height - (butts.preview * 2))}});
					this.prvctrls.scrls = new Element("div", {"class": "stageSliderPreviewScrl"});
					
					this.prvctrls.scrls.adopt(this.previews);
					this.prvctrls.range.grab(this.prvctrls.scrls);
					this.preview.adopt([this.prvctrls.ctrlp, this.prvctrls.range, this.prvctrls.ctrln]);
					}
				
				this.controler.point = new Element("div", {"class": "stageSliderPoint", "styles": {"positon": "absolute"}}).adopt(this.points);
				this.controler.ovrly = new Element("div", {"class": "stageSliderOvrly", "styles": $merge(style, {"position": "absolute", "height": "auto", "max-height": 200, "top": null, "bottom": 0})}).grab(new Element("div", {"class": "stageSliderOvrlyPad"}).adopt((this.data[0] && this.data[0].head ? [new Element("h1", {"text": this.data[0].head}), new Element("span", {"text": this.data[0].text})] : [])));
				this.controler.scrls = new Element("div", {"styles": $merge(style, {"position": "absolute", "left": 0, "height": this.cords.height, "width": (this.cords.width * this.data.length)}), "class": "stageSliderScroll"});
				
				if(!this.options.txtbar){
					this.controler.ovrly.setStyles({"display": "none"});
					}
				
				this.controler.scrls.adopt(this.elements);
				this.canvas.addClass("stageSlider");
				this.canvas.empty();
				this.canvas.addEvent("click", (function(e){this.navigate(new Event(e).target, e);}).bind(this));
				
				if(this.options.preview){
					this.canvas.adopt([this.preview]);
					}
				
				this.canvas.adopt([this.controler.scrls, this.controler.ctrlp, this.controler.ctrln, this.controler.ovrly, this.controler.point]);
				this.activate();
				}
			}
		catch(e){
			this.error(e);
			}
		}, 

//--------------------------------------------------

	move : function(dir){
		this.log("stageSlider.move");

		try{
			this.positon = this.positon + dir;
			this.positon = this.seek(this.positon);

			if(this.data[this.positon]){
				this.text(this.data[this.positon]);
				new Fx.Tween(this.controler.scrls, {transition: Fx.Transitions.linear.easeOut, duration: 700}).start("left", ((this.positon * this.cords.width) * -1));
				}
			
			this.activate();
			}
		catch(e){
			this.error(e);
			}
		}, 

//--------------------------------------------------

	text : function(data){
		this.log("stageSlider.text");

		try{
			if(!this.options.txtbar){
				return true;
				}
				
			new Fx.Tween(this.controler.ovrly, {transition: Fx.Transitions.linear.easeOut, duration: 200, onComplete: (function(){
				var head = this.controler.ovrly.getElement("h1");
				var text = this.controler.ovrly.getElement("span");

				if(($chk(head)) && ($type(head) == "element") && (head.tagName == "H1")){
					head.set("text", data.head);
					}
				
				if(($chk(text)) && ($type(text) == "element") && (text.tagName == "SPAN")){
					text.set("text", data.text);
					}
					
				new Fx.Tween(this.controler.ovrly, {transition: Fx.Transitions.linear.easeOut, duration: 600}).start("bottom", 0);
				}).bind(this)}).start("bottom", -200);
			
			}
		catch(e){
			this.error(e);
			}
		}, 

//--------------------------------------------------

	navigate : function(ele, e){
		this.log("stageSlider.navigate");
		
		try{
			if(this.data[this.positon]){
				if((this.data[this.positon].href) && (this.data[this.positon].href[0])){
					location.href = this.data[this.positon].href[0];
					}
				}
			}
		catch(e){
			this.error(e);
			}
		}, 

//--------------------------------------------------

	prev : function(e){
		this.log("stageSlider.prev");
		
		try{
			try{$com.ivwfire();}catch(e){}
	
			var e = new Event(e).stop();
			window.clearInterval(this.interval);
			this.move(-1);
			}
		catch(e){
			}
		}, 

//--------------------------------------------------

	next : function(e){
		this.log("stageSlider.next");

		try{			
			try{$com.ivwfire();}catch(e){}
			
			var e = new Event(e).stop();
			window.clearInterval(this.interval);
			this.move(+1);
			}
		catch(e){
			}
		}, 

//--------------------------------------------------

	activate : function(){
		this.log("stageSlider.activate");

		try{			
			$A(this.points).each(function(itm, idx){itm.removeClass("act")});
			
			if(this.points[this.positon]){
				this.points[this.positon].addClass("act");
				}
			
			if(this.options.preview){
				if(this.previews[this.positon]){
					this.previewActivate(this.previews[this.positon]);
					}
				}
			}
		catch(e){
			}
		}, 

//--------------------------------------------------

	previewActivate : function(ele){
		this.log("stageSlider.previewActivate");

		try{
			var cords = ele.getCoordinates();
			var bxcrd = ele.getParent("div").getCoordinates();

			$A(this.previews).each(function(itm){
				itm.removeClass("act");
				});
				
			ele.addClass("act");
			
			this.previewMove();
			}
		catch(e){
			}
		}, 

//--------------------------------------------------

	previewMove : function(){
		this.log("stageSlider.previewMove");

		try{
			var move = {"pos": 0, "ele": null};
			
			$A(this.previews).each(function(itm, idx){
				if(itm.hasClass("act")){
					this.ele = itm;
					this.pos = idx;
					}
				}, move);
			
			if(($chk(move.ele)) && ($type(move.ele) == "element") && (move.ele.tagName == "A")){
				var elements = {
					"current": move.ele, 
					"movable": move.ele.getParent("div"), 
					"clipbox": move.ele.getParent("div").getParent("div") 
					};
				var coordinates = {
					"current": elements.current.getCoordinates(), 
					"clipbox": elements.clipbox.getCoordinates() 
					};
					
				var maxin = Math.floor(coordinates.clipbox.height / coordinates.current.height);
				var offst = (elements.movable.getStyle("top").toInt() / coordinates.current.height);
				
				if(move.pos > this.prvpositon){
					ospos = (move.pos - (maxin - 2));
					}
				else if(move.pos < this.prvpositon){
					ospos = (move.pos - 1);
					}
					
				ospos = (ospos < 0 ? 0 : ospos);
				ospos = (ospos >= (this.previews.length - maxin) ? (this.previews.length - maxin) : ospos);
				ospos = ((ospos * coordinates.current.height) * -1);
				
				this.prvpositon = move.pos;

				new Fx.Tween(elements.movable, {transition: Fx.Transitions.linear.easeOut, duration: 700}).start("top", ospos);
				}
			}
		catch(e){
			this.error(e);
			}
		}, 

//--------------------------------------------------

	previewClick : function(ele, e){
		this.log("stageSlider.previewClick");

		try{			
			var e = new Event(e).stop();
			
			if(($chk(ele)) && ($type(ele) == "element")){
				if(ele.tagName != "A"){
					ele = ele.getParent("a");
					}
					
				this.click(ele, e);
				}
			}
		catch(e){
			this.error(e);
			}
		}, 

//--------------------------------------------------

	click : function(ele, e){
		this.log("stageSlider.click");

		try{			
			var e = new Event(e).stop();
			window.clearInterval(this.interval);
			
			if(($chk(ele)) && ($type(ele) == "element") && (ele.tagName == "A")){
				var pos = parseInt(ele.getProperty("rel"));
				var dif = pos - this.positon;
				
				if(dif != 0){
					this.move(dif);
					}
				}
			}
		catch(e){
			}
		}, 

//--------------------------------------------------

	seek : function(pos){
		try{
			if(pos > (this.data.length - 1)){
				pos = 0;
				}
			
			if(pos < 0){
				pos = (this.data.length - 1);
				}
			}
		catch(e){
			}
		
		return pos;
		}, 

//--------------------------------------------------

	end : function(){
		} 

//--------------------------------------------------

	});

//--------------------------------------------------
