var Slideshow = {
	options : {                    // object Options for slideshow
		transitionDelay    : 4000,  // int delay between switchings when slideshow is started (ms)
		transitionDuration : 1000,  // int duration the switching between two pictures takes (ms)
		width : 0                   // int width of parent-object (px)
	},

	pictures : null,               // nodeList List of all pictures
	currentPicture : null,         // node currently showed Picture
	upcomingPicture : null,        // node picture that is to be showed next
	isLocked : false,              // bool slideshow is locked
	timer : null,                  // timerobject timer for automatic slideshow

	


	/**
	* Inits Slideshow (precaches pictures, starts slideshow)
	*
	* @param object options for slideshow
	*/
	init : function(options) {
		this.mergeOptions(options);
		this.loadPictures();
		this.startSlideshow();
	},




	/**
	* Merges Options into Slideshows options
	*
	* @param object options to be merged
	*/
	mergeOptions : function(options) {
		for( var i in options ) {
			this.options[i] = options[i];
		}
	},




	/**
	* Loads all Pictures to be in Browser-Cache,
	* so there's no delay on switching images
	*/
	loadPictures : function() {

	},




	/**
	* Starts automatic image-switching
	*/
	startSlideshow : function() {
		this.timer = window.setInterval(function(){Slideshow.nextPicture();}, this.options.transitionDelay);
	},




	/**
	* Stops automatic image-switching
	*/
	stopSlideshow : function() {
		if(this.timer == null)
			return;

		window.clearInterval(this.timer);
	},




	/**
	* Fetches Pictures
	*
	* @return nodeList pictures
	*/
	getPictures : function() {
		if(this.pictures == null)
			this.pictures = $('#managerscreenshots').find('DIV');

		return this.pictures;
	},




	/**
	* Returns currentPicture node
	*
	* @return node currentPicture node
	*/
	getCurrentPicture : function() {
   	if(this.currentPicture == null)
			this.currentPicture = $('div.activescreenshot')[0];

		return this.currentPicture;
	},




	/**
	* Switches Image
	*
	* @param int direction to slide (+1 or -1)
	*/
	shiftPicture : function( dir ) {
		if(this.isLocked)
			return false;
		this.isLocked = true;

		var num = this.getCurrentPicture().ID().split('-')[1];
		var pics = this.getPictures();

		num = parseInt(num) + dir;

		if(num < 1)
			num = pics.length;
		else if(num > pics.length)
			num = 1;


		this.upcomingPicture = $('#managerscreenshot-' + num);
		
		if( dir > 0) // Slide next-pic from right to left
			var left = this.options.width;
		else // Slide prev-pic from left to right 
			var left = this.options.width * -1;

		this.upcomingPicture.css('display', 'block').css('left', left);
		this.getCurrentPicture().transform('left', 0, (-1 * left), this.options.transitionDuration);
		this.upcomingPicture.transform('left', left, 0, this.options.transitionDuration, this.completeShifting);
	},




	/**
	* Ends shiftig-process, unlocks slideshow, sets display to none on last picture
	*
	* @param bool is function calles as a method?
	*/
	completeShifting : function( v ) {
		if(typeof(v) == 'undefined')
		{
			Slideshow.completeShifting(true)
		}
		else
		{
			this.currentPicture.css('display', 'none');
			this.currentPicture = this.upcomingPicture;
			this.upcomingPicture = null;
			this.isLocked = false;
		}
	},




	/**
	* Go to next picture in slideshow
	*/
	nextPicture : function(ev) {
		this.shiftPicture(+1);
	},




	/**
	* Go to previous picture in slideshow
	*/
	previousPicture : function(ev) {
		this.shiftPicture(-1);
	},
	



	/**
	 * Assigns next/previus-picture methods to html-Objects
	 *
	 * @param fnode Node to assign previus-Method
	 * @param fnode Node to assign next-Method
	 */
	assignControls : function(previous, next) {
		previous.click(
				function(ev) {
					ev.preventDefault();
					Slideshow.stopSlideshow();
					Slideshow.previousPicture();
				}
		);
		
		next.click(
				function(ev) {
					ev.preventDefault();
					Slideshow.stopSlideshow();
					Slideshow.nextPicture();
				}
		);
	}
};

