/**
 * @fileoverview  toco3_referencegallery_pi2
 *
 * @package  toco3_teaserrotation
 * @author   Murat Purc <murat@purc.de>
 * @author   Christian Ehret <ce@toco3.com>
 * @licence  GNU General Public License v2, 
 *           http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 * @requires Prototype JavaScript framework and script.aculo.us
 */


/**
 * Class toco3_referencegallery_pi2
 *
 * @class
 */
var toco3_referencegallery_pi2 = Class.create({

	/**
	 * @fileoverview  toco3_teaserrotation_pi2
	 *
	 * @package  toco3_teaserrotation_pi2
	 * @author   Murat Purc <murat@purc.de>
	 * @author   Christian Ehret <ce@toco3.com>
	 * @licence  GNU General Public License v2, 
	 *           http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
	 * @requires Prototype JavaScript framework and script.aculo.us
	 */


		/**
		 * Slidebox element
		 *
		 * @type  {String}
		 */
		_slideBox: null,

		/**
		 * Width of slidebox
		 *
		 * @type  {Integer}
		 */
		_width: 0,

		/**
		 * Height of slidebox
		 *
		 * @type  {Integer}
		 */
		_height: null,

		/**
		 * List of slidebox items
		 *
		 * @type  {Array}
		 */
	    _items: null,

		/**
		 * Duration of effect
		 *
		 * @type  {Float}
		 */
	    _duration: 0.5,

		/**
		 * Position of current slide item
		 *
		 * @type  {Integer}
		 */
	    _pos: 0,

		/**
		 * Flag about current running animation
		 *
		 * @type  {Boolean}
		 */
	    _animating: false,


		/**
		 * Constructor
		 *
		 * @param  {String}  element  ID of slide box element
		 * @param  {Object}  options  Options as json object, as follows:
	     *                            - options.itemsClass = Classname of slidebox items
		 */
		initialize: function(element, options) {
			if (typeof(options) == "undefined") {
			    throw("toco3_teaserrotation.initialize: Missing options parameter");
	            return;
	        } else if (!$(element)) {
			    throw("toco3_teaserrotation.initialize: Couldn't find slide box element having id '" + element + "'");
	            return;
	        } else if (typeof(options.itemsClass) == "undefined") {
			    throw("toco3_teaserrotation.initialize: Missing option for class name of slide box items");
	            return;
	        }

	        // slide box
	        this._slideBox = element;

	        // set slide box items
	        this._items = $(this._slideBox).select("." + options.itemsClass);
	        if (!this._items) {
	            return;
	        }

	        if ($(this._slideBox+'-'+this._pos)) {
	        	$(this._slideBox+'-'+this._pos).addClassName('active');
	        }

	        $(this._slideBox).setStyle({height : $(this._items[this._pos]).getHeight() +"px", minHeight: $(this._items[this._pos]).getHeight() +"px"});

	        // initialize slide box items
	        this._items.each(function(item, i){
	            item.setStyle({position: "absolute", top: "0px", left: "0px"});
	            if (i !== this._pos) {
	                item.setStyle({left: "-" + $(this._items[this._pos]).getWidth() + "px"});
	            }
	        }.bind(this));

		},


	    /**
	     * Displays the slide box item by passed position
	     *
	     * @param  {Interger}  position  Position of item to show
	     */
	    goToPos: function(position){
			if (this._pos == position) {
				return;
			}
	        if (this._animating) {
	            return;
	        }
	        if (position < 0 || position >= this._items.length) {
	            return;
	        }
	        var currentPos = this._pos;
			var setreverse = false;
			if (position < currentPos) {
				setreverse = true;
			}
			this._pos = position--;
	        this._move(this._newSrollOptions(currentPos, this._pos, setreverse));
	    },


	    /**
	     * Returns the scroll options struct for the next slide
	     *
	     * @param  {Integer}  curPos   Current slide item position
	     * @param  {Integer}  nextPos  Next slide item position
	     * @param  {Boolean}  reverse  Flag to reverse 
	     *
	     */
	    _newSrollOptions: function(curPos, nextPos, reverse){
	        var height = this._height + this._spacer;
	        var width  = this._width + this._spacer;
	        if (reverse == true) {
	            height = - height;
	            width  = - width;
	        }

	        // base scroll options
	        var opt = {
	            cur: {
	                pos: curPos, x: 0, y: 0
	            },
	            next: {
	                pos: nextPos, x1: 0, y1: 0, x2: 0, y2: 0
	            }
	        };

	        return opt;
	    },


	    _move: function(opt) {

        	if ($(this._slideBox+'-'+opt.cur.pos)) {
	    	  $(this._slideBox+'-'+opt.cur.pos).removeClassName('active');
        	}
	    	this._animating = true;
	    	
	    	new Effect.Fade($(this._items[opt.cur.pos]), {
	            duration:   this._duration,
	            from: 1,
	            to: 0,
	            beforeStart: function(){
        		
	        }	            
	        });

	    	$(this._items[opt.next.pos]).setStyle({display: "none", top: opt.next.x1 + "px", left: opt.next.y1 + "px"});

        
	        $(this._slideBox).setStyle({height : $(this._items[opt.next.pos]).getHeight() +"px", minHeight: $(this._items[opt.next.pos]).getHeight() +"px"});
	        if ($(this._slideBox+'-'+opt.next.pos)) {
	        	$(this._slideBox+'-'+opt.next.pos).addClassName('active');
	        }
	        new Effect.Appear($(this._items[opt.next.pos]), {
	            duration:   this._duration,
	            from: 0,
	            to: 1,
	            afterFinish: function(){
                this._animating = false;
                
            }.bind(this)	            
	        });

	    }
	});


	if (!("console" in window) || !("firebug" in console)) {
	(function(){
		window.console = {
			log:   function(){},
			debug: function(){},
			info:  function(){},
			warn:  function(){},
			error: function(){}
		}
	})();
	}

