/*****
image slider extension - animation scripts
requires jquery 1.4+
frappant.ch/mhm
v1.1/24.5.2011

folgendes code bei projekt-js zu integrieren:
jQuery(document).ready(function(){
	var imageslider=new frp_imageslider({
		'interval'	: 5	// pause (sekunden)
	});
});

******/

frp_imageslider=function(args){
	/**
	core object. init() will be called automatically when the object is instantialized
	optional parameters (add parameters together as array):
		interval: pause between changeover in seconds
		rootElement: jQuery object of overall root element
	**/
	if(!args['rootElement']){args['rootElement']=jQuery('.frp_imageslider');}
	this.init(args['rootElement'],args['interval']);
}

frp_imageslider.prototype.init=function(rootElement,interval){	//	pass in nr. of seconds between changes
	////////// initialize image slider //////////
	if(interval==0){
		if(window.console){
			window.console.log('frp_imageslider.prototype.init: interval is zero!');
		}
	}else{
		this.interval=interval*1000;

		this.sliderObject 		= this;

		this.domObject 			= rootElement;
		this.domObject.data('sliderObject',this.sliderObject)

		this.slidesContainer	= this.domObject.find('.slides');
		this.slides 			= this.domObject.find('.slide');
		this.naviObjects		= this.domObject.find('.navi');

		this.leftControl		= this.naviObjects.find('.leftControl');
		this.rightControl		= this.naviObjects.find('.rightControl');

		this.currentPosition 	= 0;
		this.autoPlayPosition 	= 0;
		this.slideWidth 		= this.domObject.width();
		this.numberOfSlides 	= this.slides.length;

		this.slidesContainer.css('width', this.slideWidth * this.numberOfSlides);

		this.initControls();
		this.initNavigation();

		this.slideTo(0);
		objToPass=this;

		//	start repeater for animation
		this.intervalId=setInterval(function(){
			objToPass.autoPlay();
		},objToPass.interval);
	}
}; // init

////////// functions //////////

frp_imageslider.prototype.initControls=function(){
	// ZURÜCK, WEITER BUTTONS VERLINKEN
	this.leftControl.click(function(){
		this.stopAutoPlay();
		this.slideTo(this.currentPosition-1);
	});
	this.rightControl.click(function(){
		this.stopAutoPlay();
		this.slideTo(this.currentPosition+1);
	});
};	//initControls

frp_imageslider.prototype.initNavigation=function(){

	sliderObject=this.domObject.data('sliderObject');
	this.naviObjects.each(function(){
		jQuery(this).data('sliderObject',sliderObject);
		jQuery(this).attr('title',jQuery(this).text());
	});

	// NAVIGATION VERLINKEN UND RICHTIG DARSTELLEN
	this.naviObjects.each(function(){
		jQuery(this).click(function(){
			$this=jQuery(this);
			sliderObject=$this.data('sliderObject');
			sliderObject.stopAutoPlay();

			// Elementnummer herausfinden
			var klassen = $this.attr('class').replace('first','').replace('active','').replace('navi','').replace(' ','');
			if (klassen.substr(0,4) == 'navi') {
				var elementNummer = parseInt(klassen.substr(4));
				sliderObject.slideTo(elementNummer);
			} else {
				sliderObject.slideTo(0);
			}
		});
	});
};	//initNavigation

frp_imageslider.prototype.manageControls=function(){
	// ÜBERPRÜFT OB DIE PFEILE ANGEZEIGT WERDEN
	this.leftControl.hide();
	this.rightControl.hide();

	if (this.currentPosition > 0 && this.numberOfSlides > 0) {
		this.leftControl.show();
	}
	if (this.currentPosition+1 < this.numberOfSlides) {
		this.rightControl.show();
	}
}; //manageControls

frp_imageslider.prototype.autoPlay=function() {
	// AUTOMATISCHES STARTEN DES SLIDES
	if (this.autoPlayPosition > this.currentPosition) {
		this.autoPlayPosition = this.currentPosition;
	}
	this.autoPlayPosition++;
	this.slideTo(this.autoPlayPosition);
}; // autoPlay

frp_imageslider.prototype.stopAutoPlay=function() {
	clearInterval(this.intervalId);
}; // stopAutoPlay()

frp_imageslider.prototype.slideTo=function(elementNumber) {

	elementNumber = parseInt(elementNumber);

	if (elementNumber+1 > this.numberOfSlides || elementNumber<0) {
		elementNumber = 0;
	}
	this.currentPosition = elementNumber;

	// Aktiv setzen
	this.naviObjects.removeClass("active");
	this.domObject.find('.navi' + elementNumber).addClass("active");
	// Anzeigen/Verbergen Schaltflächen
	this.manageControls();

	// Move slidesContainer using margin-left
	if(elementNumber == this.numberOfSlides) {
		this.slidesContainer.css('margin-left', 0);
	} else {
		this.slidesContainer.animate({
		  'marginLeft'	: this.slideWidth*(-elementNumber)
		});
	}

	this.manageControls();
}; // slideTo()
