var classSwitcher = Class.create();
classSwitcher.prototype = {

	initialize : function (elems)
	{
		this.options = elems;
		this.writeEvents();
	},
	
	writeEvents : function ()
	{
		for( i=0; i<this.options.length; i++ )
		{
			for( k=0; k<this.options[i].length; k++ )
			{
				this.options[i][k].phil = this;
				this.options[i][k].observe('mouseover', this.over );
				this.options[i][k].observe('mouseout', this.out );
			}
		}
	},

	over : function ()
	{
	
		this.addClassName('sfhover');
		clearInterval(this.hide);
	},
	
	out : function ()
	{
		this.hide = setInterval(this.phil.removeClass.bind(this),50);
	},
	
	removeClass : function()
	{
		if(this.className.indexOf("sfhover") >= 0)
		{
			this.removeClassName("sfhover");
		}
	}
	
}

var picSwitcher = Class.create();
picSwitcher.prototype = {
	
	initialize : function (elems)
	{
		this.options = elems;
		this.writeEvents();
	},
	
	writeEvents : function ()
	{
		for( i=0; i<this.options.length; i++ )
		{
			for( k=0; k<this.options[i].length; k++ )
			{
				this.options[i][k].observe('mouseover', this.over);
				this.options[i][k].observe('mouseout', this.out);
			}
		}
	},

	over : function ()
	{
		this.imgSrc = this.getElementsByTagName('img')[0].src;
		this.seperatedSrc = this.imgSrc.split('.');
		fileExtension =  this.seperatedSrc.length - 1;
		fileName = this.seperatedSrc.length - 2;
		this.seperatedSrc[fileName] = this.seperatedSrc[fileName] + '_active';
		this.joinedSrc = this.seperatedSrc.join(".");
		this.getElementsByTagName('img')[0].src = this.joinedSrc;
	},
	
	out : function ()
	{
		this.imgSrc = this.getElementsByTagName('img')[0].src;
		this.seperatedSrc = this.imgSrc.split('.');
		fileExtension =  this.seperatedSrc.length - 1;
		fileName = this.seperatedSrc.length - 2;
		this.seperatedSrc[fileName] = this.seperatedSrc[fileName].replace(new RegExp("_active\\b"), "")
		this.joinedSrc = this.seperatedSrc.join(".");
		this.getElementsByTagName('img')[0].src = this.joinedSrc;
	}
}

var pslider = Class.create();
pslider.prototype = {
	
	initialize : function (params)
	{
		this.options = params;
		this.options.posIndicator = $( this.options.posIndicator );
		this.options.posIndicator.items = this.options.posIndicator.getElementsByTagName('LI');
		this.options.sliderContainer = $( this.options.sliderContainer );
		this.options.itemWidth = this.options.maxSliderWidth / this.options.posIndicator.items.length;
		this.options.triggerNext = $( this.options.triggerNext );
		this.options.triggerPrev = $( this.options.triggerPrev );
		this.options.tmpPos = 0;
		this.options.slideType = this.options.slideType == 'v' ? 'top' : 'left';
				
		this.writeEvents();
	},
	
	writeEvents : function ()
	{
		//Loop through indicators and set events
		for ( i=0; i<this.options.posIndicator.items.length; i++ )
		{
			$(this.options.posIndicator.items[i]).observe( 'click', this.slideDirect );
			$(this.options.posIndicator.items[i]).myClass = this;
			$(this.options.posIndicator.items[i]).liID = i;
		}
		//Set the first indicator to active, after initialize
		$(this.options.posIndicator.items[0]).addClassName('active');
		
		//Set events for prev and next buttons
		this.options.triggerPrev.observe( 'click', this.slidePrev );
		this.options.triggerPrev.myClass = this;
		this.options.triggerNext.observe( 'click', this.slideNext );
		this.options.triggerNext.myClass = this;
	},
	
	slideDirect : function ()
	{
		//Make indicator highlighting
		for ( i=0; i<this.myClass.options.posIndicator.items.length; i++ )
		{
			this.myClass.options.posIndicator.items[i].removeClassName('active');
		}
		this.addClassName('active');
		
		//Calculate start- and endpositions to slide to
		this.currentPos = parseFloat( this.myClass.options.sliderContainer.getStyle( this.myClass.options.slideType ) );
		this.targetPos = ( this.myClass.options.itemWidth * this.liID ) *-1;
		this.myClass.options.tmpPos = this.liID;
		
		//Start sliding-animation
		if( this.currentPos != this.targetPos )
		{
			ex0 = new Animator({ transition: Animator.easeInOut, interval: 20, duration: 400 });
			ex0.addSubject( new NumericalStyleSubject( this.myClass.options.sliderContainer, this.myClass.options.slideType, String( this.currentPos ) + 'px', String( this.targetPos ) + 'px' ) );
			ex0.play();
		}
	},
	
	slideNext : function ()
	{
		//Calculate start- and endpositions to slide to
		this.currentPos = parseFloat( this.myClass.options.sliderContainer.getStyle( this.myClass.options.slideType ) );
		this.targetPos = ( this.myClass.options.itemWidth * ( this.myClass.options.tmpPos + 1 ) ) *-1;
		
		if( this.targetPos >= ( this.myClass.options.maxSliderWidth * -1 ) + this.myClass.options.itemWidth )
		{
			this.myClass.options.tmpPos += 1;
			
			//Set active indicator
			for( i=0; i<this.myClass.options.posIndicator.items.length; i++ )
			{
				$( this.myClass.options.posIndicator.items[i].removeClassName('active') );
			}
			$( this.myClass.options.posIndicator.items[this.myClass.options.tmpPos].addClassName('active') );
			
			//Start sliding-animation
			if( this.currentPos != this.targetPos )
			{
				ex0 = new Animator({ transition: Animator.easeInOut, interval: 20, duration: 400 });
				ex0.addSubject( new NumericalStyleSubject( this.myClass.options.sliderContainer, this.myClass.options.slideType, String( this.currentPos ) + 'px', String( this.targetPos ) + 'px' ) );
				ex0.play();
			}
		}
	},
	
	slidePrev : function ()
	{
		//Calculate start- and endpositions to slide to

		this.currentPos = parseFloat( this.myClass.options.sliderContainer.getStyle( this.myClass.options.slideType ) );
		this.targetPos = ( this.currentPos + this.myClass.options.itemWidth );
		
		if( this.targetPos < this.myClass.options.itemWidth )
		{
			this.myClass.options.tmpPos -= 1;
			
			//Set active indicator
			for( i=0; i<this.myClass.options.posIndicator.items.length; i++ )
			{
				$( this.myClass.options.posIndicator.items[i].removeClassName('active') );
			}
			$( this.myClass.options.posIndicator.items[this.myClass.options.tmpPos].addClassName('active') );
			
			//Start sliding-animation
			if( this.currentPos != this.targetPos )
			{
				ex0 = new Animator({ transition: Animator.easeInOut, interval: 20, duration: 400 });
				ex0.addSubject( new NumericalStyleSubject( this.myClass.options.sliderContainer, this.myClass.options.slideType, String( this.currentPos ) + 'px', String( this.targetPos ) + 'px' ) );
				ex0.play();
			}
		}
	}
	
}

var boxSlider = Class.create();
boxSlider.prototype = {
	initialize : function (params, test)
	{
		this.options = params;
		
		for( i=0; i<this.options.sliderContainer.length; i++ )
		{
			this.options.sliderContainer[i].normalHeight = this.options.sliderContainer[i].getHeight();
			if( i != 0 )
			{
				this.options.sliderContainer[i].setStyle({ height: 0 + 'px' })
			}
		}
		this.writeEvents();
	},
	
	writeEvents : function ()
	{
		for ( i=0; i<this.options.triggers.length; i++ )
		{
			for( k=0; k<this.options.triggers[i].length; k++ )
			{
				this.options.triggers[i][k].observe('click', this.toggle);
				this.options.triggers[i][k].triggerForElem = k;
				this.options.triggers[i][0].opened = true;
				this.options.triggers[i][k].myClass = this;
			}
		}
	},
	
	toggle : function ()
	{	
		if(this.opened == true)
		{
			this.myClass.closeBox(this);
		}
		else
		{
			this.myClass.openBox(this);
		}
	},
	
	openBox : function (myElem)
	{
		ex0 = new Animator({ transition: Animator.easeInOut, interval: 20, duration: 400 });
		ex0.addSubject( new NumericalStyleSubject( this.options.sliderContainer[myElem.triggerForElem], 'height', this.options.minHeight + 'px', this.options.sliderContainer[myElem.triggerForElem].normalHeight + 'px' ) );
		ex0.play();
		myElem.opened = true;
	},
	
	closeBox : function (myElem)
	{
		ex0 = new Animator({ transition: Animator.easeInOut, interval: 20, duration: 400 });
		ex0.addSubject( new NumericalStyleSubject( this.options.sliderContainer[myElem.triggerForElem], 'height', this.options.sliderContainer[myElem.triggerForElem].normalHeight + 'px', this.options.minHeight + 'px' ) );
		ex0.play();
		myElem.opened = false;		
	}
	
}

var dropDownSelector = Class.create();
dropDownSelector.prototype = {

	initialize : function ( params )
	{
		this.options = params;
		for( i=0; i<this.options.collections.length; i++ )
		{
			this.options.elemSelector = this.options.collections[i].getElementsByClassName(this.options.selectboxClass)[0];
			this.options.iconCollection = this.options.collections[i].getElementsByClassName(this.options.iconClass)
			for( k=0; k<this.options.iconCollection.length; k++ )
			{
				this.options.iconCollection[k].observe('click', this.setDropdown);
				this.options.iconCollection[k].selector = this.options.elemSelector;
				this.options.iconCollection[k].meID = k;
			}
		}
	},
	
	setDropdown : function ()
	{
		this.selector.options[this.meID].selected = true;
		this.blur();
	}
	
}

var tipbox = Class.create();
tipbox.prototype = {
	
	initialize : function (params)
	{
		this.options = params;
		this.writeEvents();
	},
	
	writeEvents : function ()
	{
		for( i=0; i<this.options.triggers.length; i++ )
		{
			this.options.triggers[i].observe('mouseover',this.showTip);
			this.options.triggers[i].observe('mouseout',this.hideTip);
			this.options.triggers[i].meBox = this.options.tbs[i];
			this.options.tbs[i].setStyle({ display: 'none' });
		}
	},
	
	showTip : function ()
	{
		this.meBox.setStyle({ display: 'block' });
	},
	
	hideTip : function ()
	{
		this.meBox.setStyle({ display: 'none' });
	}
	
}
