// NOTE: Absolutely/Relatively Positioned Elements will not fade if you fade their parent element.
// the element.getChildren() is used to fade them manually.

var thisTimer;

var delayTimer = 5000;

function simpleSlideInit(){
	if($('rotate_holder')){
		var allofthem = $$('.mid_rotate');
		if(allofthem.length > 1){
			var mycounter = 1;
			allofthem.each(function(e){
				e.setProperty('id','info_homey'+mycounter);
				if(mycounter != 1){
					e.setStyle('display','none');
				}
				mycounter++;
			});
			var selectors = $$('.switch_holder a');
			mycounter = 1;
			selectors.each(function(e){
				e.setProperty('id','homey'+mycounter);
				mycounter++;
			});
			$$('.switch_holder a').each(function(e){
				e.addEvent('click',function(s){
					$$('.switch_holder a').removeClass('current');
					e.addClass('current');
					var hideme;
					$$('.mid_rotate').each(function(el){
						if(el.getStyle('display') != 'none'){
							hideme = el;
						}
					});
					hideme.fade('out');
					hideme.getChildren().fade('out');
					(function(){ hideme.setStyle('display','none');	}).delay(400);
					(function(){
						$('info_'+e.id).setStyles({ opacity:0,display:'block'});
						$('info_'+e.id).fade('in');
						$('info_'+e.id).getChildren().fade('in');
					}).delay(400);
					$clear(thisTimer);
					return false;
				});
			});
			thisTimer = function(s){
				for(var i =0;i < allofthem.length;i++){
					var e = allofthem[i];
					if(e.getStyle('display') == 'block'){
						var nexty = i + 2;
						if(nexty > allofthem.length){
							nexty = Number(1);
						}
						var showthis = 'info_homey' + String(nexty);
						e.fade('out');
						e.getChildren().fade('out');

						(function(){ e.setStyle('display','none'); }).delay(400);
						(function(){
							$(showthis).setStyles({ opacity:0,display:'block'});
							$(showthis).fade('in');
							$(showthis).getChildren().fade('in');
						}).delay(400);

						$$('.switch_holder a').removeClass('current');
						var selector = 'homey'+String(nexty);
						$(selector).addClass('current');

						break;
					}
				}
			}.periodical(delayTimer);
		}
		else{
			$$('.switch_holder a').setStyle('display','none');
		}
	}
}

function infiniteScrollInit(backdiv,forwarddiv,outside,scrollers,amount,leftortop){
	if(backdiv){
		var count = $$(scrollers).length;
		var ismoving = false;
		if(count > 1){
			backdiv.setStyle('display','block');
			forwarddiv.setStyle('display','block');
			var counter = 0;
			$$(scrollers).each(function(el){
				el.setStyle(leftortop,(amount*counter));
				el.setStyle('display','');
				counter++;
				if(counter == count){
					el.clone().inject(outside.getFirst(scrollers),'before').setStyles({top:(amount*-1)});
				}
			});
			forwarddiv.addEvent('click',function(ev){
				if(ismoving == false){
					ismoving = true;
					counter = 0;
					var last = 0;
					$$(scrollers).each(function(el){
						if(counter == 0){
							el.dispose();
						}
						else{
							last = Math.max(last,el.getStyle(leftortop).toInt());
							var newFX = new Fx.Tween(el).start(leftortop,(el.getStyle(leftortop).toInt()-amount));
							newFX.addEvent('complete',function(){
								ismoving = false;
							});
						}
						counter++;
					});
					outside.getFirst(scrollers).clone().inject(outside.getLast(scrollers),'after').setStyle(leftortop,(last));
				}
				else{
					//
				}
				return false;
			});
			backdiv.addEvent('click',function(ev){
				if(ismoving == false){
					ismoving = true;
					counter = 0;
					var first = amount;
					$$(scrollers).each(function(el){
						first = Math.min(first,el.getStyle(leftortop).toInt());
						var newFX = new Fx.Tween(el).start(leftortop,(el.getStyle(leftortop).toInt()+amount));
						newFX.addEvent('complete',function(){
							ismoving = false;
						});
						if(counter == count){
							el.dispose();
						}
						counter++;
					});
					outside.getLast(scrollers).clone().inject(outside.getFirst(scrollers),'before').setStyle(leftortop,(first));
				}
				else{
					//
				}
				return false;
			});
		}
	}
}

function limitedScrollInit(backdiv,forwarddiv,outside,scrollers,amount,leftortop){
	if(backdiv){
		var count = $$(scrollers).length;
		var ismoving = false;
		if(count > 1){
			forwarddiv.setStyle('display','block');
			var counter = 0;
			$$(scrollers).each(function(el){
				el.setStyle(leftortop,(amount*counter));
				el.setStyle('display','');
				counter++;
			});
			forwarddiv.addEvent('click',function(ev){
				if(ismoving == false){
					backdiv.setStyle('opacity','1'); //new
					backdiv.setStyle('display','block'); //new
					ismoving = true;
					counter = 0;
					var last = 0;
					var countzeros = 0; //new
					$$(scrollers).each(function(el){
						if(el.getStyle(leftortop).toInt() > 0){ //new
							countzeros++; //new
						} //new
						last = Math.max(last,el.getStyle(leftortop).toInt());
						var newFX = new Fx.Tween(el).start(leftortop,(el.getStyle(leftortop).toInt()-amount));
						newFX.addEvent('complete',function(){
							ismoving = false;
						});
						counter++;
					});
					if(countzeros == 1){ //new
						forwarddiv.fade('out'); //new
						(function(){	forwarddiv.setStyle('display','none');	}).delay(400); //new
					} //new
				}
				else{
					//
				}
				return false;
			});
			backdiv.addEvent('click',function(ev){
				if(ismoving == false){
					forwarddiv.setStyle('opacity','1'); //new
					forwarddiv.setStyle('display','block'); //new
					ismoving = true;
					counter = 0;
					var first = amount;
					var countzeros = 0; //new
					$$(scrollers).each(function(el){
						if(el.getStyle(leftortop).toInt() < 0){ //new
							countzeros++; //new
						} //new
						first = Math.min(first,el.getStyle(leftortop).toInt());
						var newFX = new Fx.Tween(el).start(leftortop,(el.getStyle(leftortop).toInt()+amount));
						newFX.addEvent('complete',function(){
							ismoving = false;
						});
						counter++;
					});
					if(countzeros == 1){ //new
						backdiv.fade('out'); //new
						(function(){	backdiv.setStyle('display','none');	}).delay(400); //new
					} //new
				}
				else{
					//
				}
				return false;
			});
		}
	}
}

/*
function scrollInit(){
	if($('specialup')){
		var count = $$('.specialholder').length;
		var ismoving = false;
		if(count > 1){
			$('specialup').setStyle('display','block');
			$('specialdown').setStyle('display','block');
			var counter = 0;
			$$('.specialholder').each(function(el){
				el.setStyles({top:(146*counter),left:0,display:''});
				counter++;
				if(counter == count){
					el.clone().inject('specialbox','top').setStyles({top:(146*-1)});
				}
			});
			$('specialdown').addEvent('click',function(ev){
				if(ismoving == false){
					ismoving = true;
					counter = 0;
					var last = 0;
					$$('.specialholder').each(function(el){
						if(counter == 0){
							el.dispose();
						}
						else{
							last = Math.max(last,el.getStyle('top').toInt());
							var newFX = new Fx.Tween(el).start('top',(el.getStyle('top').toInt()-146));
							newFX.addEvent('complete',function(){
								ismoving = false;
							});
						}
						counter++;
					});
					$('specialbox').getFirst('.specialholder').clone().inject($('specialbox').getLast('.specialholder'),'after').setStyle('top',(last));
				}
				else{
					//
				}
				return false;
			});
			$('specialup').addEvent('click',function(ev){
				if(ismoving == false){
					ismoving = true;
					counter = 0;
					var first = 146;
					$$('.specialholder').each(function(el){
						first = Math.min(first,el.getStyle('top').toInt());
						var newFX = new Fx.Tween(el).start('top',(el.getStyle('top').toInt()+146));
						newFX.addEvent('complete',function(){
							ismoving = false;
						});
						if(counter == count){
							el.dispose();
						}
						counter++;
					});
					$('specialbox').getLast('.specialholder').clone().inject('specialbox','top').setStyle('top',(first));
				}
				else{
					//
				}
				return false;
			});
		}
	}
}
*/
