window.addEvent('domready', function(){ 
	SqueezeBox.initialize({size:{x:490,y:376}});
    SqueezeBox.assign($$('a.squeezebox'));
	if($chk($('slider-stage'))){
		var test = new ClickRotate();
	}
});



var ClickRotate = new Class({
	Implements: [Options],
	options: {
		stage: 'slider-stage',
		sliders: 'slider-list',
		gotoclass: 'gotolink',
		gotoevent:'click',
		next:'next',
		nextevent:'click',
		transition:'fade',
		transitionduration:500,
		clickduration:250,
		previous:'previous',
		previousevent:'click',
		autorotate:true,
		waittime:3000,
		returnrotate:true,
		returnafter:5000,
		returnid:null,
		intervalID:null,
		useactivebuttons:false,
		onbutton:'/gfx/splash_on.gif',
		offbutton:'/gfx/splash_off.gif',
		buttonsinject:'splashbuttons'
	},
	initialize: function(options) {
		this.setOptions(options);
		var op = this.options;
		var f = this;
		op.children = $(op.sliders).getChildren('li');
		if (op.transition == "fade"){
			var startloop = 1000+op.children.length;
			for(i=0;i<op.children.length;i++){
				op.children[i].setStyle("z-index",startloop);
				startloop--;
			}
		}
		if(op.useactivebuttons){
			op.clickbuttons = Array();
			op.currentbuttonid=0;
			for(i=0;i<op.children.length;i++){
				if(i==0){
					op.clickbuttons[i] = new Element('img', {'src': op.onbutton});
				}else{
					op.clickbuttons[i] = new Element('img', {'src': op.offbutton});
				}
				op.clickbuttons[i].inject($(op.buttonsinject));
			}
		}
		
		if(op.autorotate) op.intervalID = this.autorotate.periodical(op.waittime,this);
		if($type($(op.next))=='element'){
			$(op.next).addEvent(op.nextevent,function(e){
				e.stop();
				if($type(op.returnid)=='number') $clear(op.returnid);
				if($type(op.intervalID)=='number'){
					$clear(op.intervalID);
					if(op.returnrotate) op.returnid = f.autorotate.delay(op.returnafter,f);
				}
				var children = $(op.sliders).getChildren('li');
				if (op.transition == "fade"){
					children[0].set('tween',{duration:op.clickduration,onComplete:function(){
						children[0].setStyle("z-index",0).inject($(op.sliders)).setStyle('opacity',1);
						var children2 = $(op.sliders).getChildren('li');
						var startloop = 1000+op.children.length;
						for(i=0;i<children2.length;i++){
							children2[i].setStyle("z-index",startloop);
							startloop--;
						}
					}});
					children[0].tween('opacity', 0);
				}else{
					children[0].set('tween',{duration:op.clickduration,onComplete:function(){children[0].inject($(op.sliders)).setStyle('margin-left',0);}});
					children[0].tween('margin-left', -children[0].getSize().x);
				}
			});
		}
		if($type($(op.previous))=='element'){
			$(op.previous).addEvent(op.previousevent,function(e){
				e.stop();
				if($type(op.returnid)=='number') $clear(op.returnid);
				if($type(op.intervalID)=='number'){
					$clear(op.intervalID);
					if(op.returnrotate) op.returnid = f.autorotate.delay(op.returnafter,f);
				}
				var children = $(op.sliders).getChildren('li');
				var last = children.length-1;
				if (op.transition == "fade"){
					children[last].set('tween',{duration:op.clickduration,onComplete:function(){children[last].setStyle('opacity',1);op.infade=false;}});
					children[last].setStyle('opacity',0).inject($(op.sliders),'top');
					var children2 = $(op.sliders).getChildren('li');
					var startloop = 1000+op.children.length;
					for(i=0;i<children2.length;i++){
						children2[i].setStyle("z-index",startloop);
						startloop--;
					}
					children[last].tween('opacity', 1);
				}else{
					children[last].set('tween',{duration:op.clickduration,onComplete:function(){children[last].set('style','');}});
					children[last].setStyle('margin-left',-children[last].getSize().x).inject($(op.sliders),'top');
					children[last].tween('margin-left', 0);
				}
			});
		}
		$$("a."+op.gotoclass).each(function(elm){
			var slideid = elm.getProperty('rel');
			if ($type(slideid)=="string"){
				slideid=slideid.toInt();
				elm.addEvent(op.gotoevent,function(e){
					e.stop();
					if($type(op.returnid)=='number') $clear(op.returnid);
					if($type(op.intervalID)=='number'){
						$clear(op.intervalID);
						if(op.returnrotate) op.returnid = f.autorotate.delay(op.returnafter,f);
					}
					var children = $(op.sliders).getChildren('li');
					if (op.transition == "fade"){
						op.children[slideid].set('tween',{duration:op.clickduration,onComplete:function(){
								op.children[slideid].setStyle('opacity',1);
								for(i=slideid+1;i<op.children.length;i++) op.children[i].inject($(op.sliders),'bottom');
								for(i=0;i<slideid;i++) op.children[i].inject($(op.sliders),'bottom');
								var children2 = $(op.sliders).getChildren('li');
								var startloop = 1000+op.children.length;
								for(i=0;i<children2.length;i++){
									children2[i].setStyle("z-index",startloop);
									startloop--;
								}
								op.infade=false;
						},onStart:function(){op.infade=true;}});
						op.children[slideid].setStyle('opacity',0).inject($(op.sliders),'top');
						var children2 = $(op.sliders).getChildren('li');
						var startloop = 1000+op.children.length;
						for(i=0;i<children2.length;i++){
							children2[i].setStyle("z-index",startloop);
							startloop--;
						}
						op.children[slideid].tween('opacity', 1);
					}else{
						op.children[slideid].set('tween',{duration:op.clickduration,onComplete:function(){
							op.children[slideid].set('style','');
							for(i=slideid+1;i<op.children.length;i++) op.children[i].inject($(op.sliders),'bottom');
							for(i=0;i<slideid;i++) op.children[i].inject($(op.sliders),'bottom');
						}});
						op.children[slideid].setStyle('margin-left',-op.children[slideid].getSize().x).inject($(op.sliders),'top');
						op.children[slideid].tween('margin-left', 0);
					}
				});
			}

		
		});
		
	},
			
	autorotate:function(){
		var op = this.options;
		if($type(op.returnid)=='number'){
			$clear(op.returnid);
			op.returnid=null;
			op.intervalID = this.autorotate.periodical(op.waittime,this);
		}
		var children = $(op.sliders).getChildren('li');
		
		var toupdate = op.currentbuttonid+1;
			var lasupdate = op.currentbuttonid;
			if(toupdate>children.length-1){
					lasupdate = children.length-1;
					toupdate=0;
					op.currentbuttonid=0;
			}else{
				op.currentbuttonid++;
			}
			if(op.useactivebuttons){
				op.clickbuttons[toupdate].src = op.onbutton;
				op.clickbuttons[lasupdate].src = op.offbutton;
			}
		
		if (op.transition == "fade"){
			children[0].set('tween',{duration:op.transitionduration,onComplete:function(){
				children[0].setStyle("z-index",0).inject($(op.sliders)).setStyle('opacity',1);
				var children2 = $(op.sliders).getChildren('li');
				var startloop = 1000+op.children.length;
				for(i=0;i<children2.length;i++){
					children2[i].setStyle("z-index",startloop);
					startloop--;
				}
			}});
			children[0].tween('opacity', 0);
		}else{
			children[0].set('tween',{duration:op.transitionduration,onComplete:function(){
				children[0].inject($(op.sliders)).setStyle('margin-left',0);
			}});
			children[0].tween('margin-left', -children[0].getSize().x);
		}
	}
		
	
});