/**
 * $URL:$
 * $Date:$
 * $Revision:$
 * $Author:$
 *
 * Copyright (C) 2009 seso media group <www.seso.at>
 * 
 * This program is free software; you can redistribute it and/or modify it 
 * under the terms of the GNU General Public License as published by the 
 * Free Software Foundation; either version 2 of the License, or 
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but 
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 
 * for more details: <http://www.gnu.org/licenses/>
 * 
 * powered by Prototype JavaScript framework <http://www.prototypejs.org/>
 * powered by scriptaculous <http://script.aculo.us/>
 * 
 * @author: gg@seso.at
 */

/*******************************************************************************
	SLIDER
*******************************************************************************/

var sliderObserver = Class.create(Observer, {

	
	startUp : function() {

		for (var i=0; i<this.cache.length; i++) {
			
			this.prepeareItem(this.cache[i]);
		}
	},
	
	
	getDefaultConfig : function() {

		var item = {};
			item.cid = null;

			item.itemContainer = 'sliderItemContainer';
			item.pageCounter = null;
			item.pageCounterInfo = null;
			item.slideLeft = 'slideLeft';
			item.slideRight = 'slideRight';
			
			item.viewportSize = 3;
			item.itemWidth = 200;
			item.jumpSize = 1;
			
			item.counter = 0;
			item.actPosition = 1;
			item.pageIndex = 0;
			item.active = false;

		return item;
	},
	

	prepeareItem : function(item) {
		
		item.itemContainer = $(item.itemContainer);
		
		if (!item.itemContainer) {
			
			log("["+item.cid+"] itemContainer not found", 3, this.name);
			return;
		}
		
		item.counter = item.itemContainer.childElements().size(); //- 1; // clear-div
		item.itemContainer.style.width = item.counter * item.itemWidth + 'px';
		
				
		item.slideLeft = $(item.slideLeft);
		
		if (item.slideLeft) {

			Event.observe(item.slideLeft, 'click', util.slider.slideLeft);
			item.slideLeft.setAttribute('cid', item.cid); 

		} else {
			
			log("slideLeft not found", 2, this.name);
		}
		
		
		item.slideRight = $(item.slideRight);
		
		if (item.slideRight) {

			Event.observe(item.slideRight, 'click', util.slider.slideRight);
			item.slideRight.setAttribute('cid', item.cid);
			
		} else {
			
			log("slideRight not found", 2, this.name);
		}
		
		
		if (item.pageCounter !== null && item.pageIndex >= 0) {

			item.pageCounter = $(item.pageCounter);
			
			if (item.pageCounter) {
				
				var list = [];
				var pages = Math.ceil((item.counter-item.viewportSize) / item.jumpSize)+1;

				for (var i=0; i<pages; i++) {
					
					list[i] = new Element('DIV');
					item.pageCounter.appendChild(list[i]);
				}
				
				if (item.pageCounter.firstChild) {
					
					var m = pages * item.pageCounter.firstChild.getWidth() * (-1);
					item.pageCounter.style.marginLeft = Math.round(m) + 'px';
					item.pageCounter = list;
					
				} else {
					
					item.pageCounter = null;
				}
				
			} else {
				
				log("["+item.cid+"] pageCounter not found", 2, this.name);
				item.pageCounter = null;
			}
		}
		
		if (item.pageCounterInfo !== null) {
			
			if (item.pageIndex >= 0) {
			
				var pId = item.pageCounterInfo+'_page';
				var oId = item.pageCounterInfo+'_of';

				if ($(item.pageCounterInfo)) {

					$(oId).innerHTML = item.counter;
					$(pId).innerHTML = item.pageIndex+1;
					
					item.pageCounterInfo = $(pId); 
				}
				
			} else {
				
				$(item.pageCounterInfo).style.display = 'none';
			}
		}
		
		//util.slider.checkArrows(item);
	},

	
	update : function() {},

	remove : function() {},
	
	
	
	checkArrows : function(item) {

		var max = item.counter - item.viewportSize + 1;

		/*if (item.actPosition >= max) {
			
			item.slideRight.style.display = 'none';
								
		} else {
			
			item.slideRight.style.display = 'block';
		}
		
		
		if (item.actPosition <= 1) {
			
			item.slideLeft.style.display = 'none';	
			
		} else {
			
			item.slideLeft.style.display = 'block';
		} */
		
		// check page-count

		if (item.pageCounterInfo) {
		
			item.pageCounterInfo.innerHTML = item.pageIndex+1;
		}
		
		if (item.pageCounter === null) {
			return;
		}
		
		for (var i=0; i<item.pageCounter.length; i++) {
			
			item.pageCounter[i].className = '';
		}

		item.pageCounter[item.pageIndex].className = 'act'; 
	},
		
	slideRight : function(event) {
		
		var cid = null;
		
		if(event != null){
			
			event.stop();
		
			var e = event.element();
	
			if (e.nodeName == 'IMG') {
				e = e.parentNode;
			}
			
			e.blur();
			
			cid = parseInt(e.getAttribute('cid'));
		} else {
			cid = 0;
		}
		
		
		var item = util.slider.cache[cid];

		//alert(item);

		if (!item) {
			
			log("slideRight - invalid cid: "+cid, 2, this.name);
			return false;
		}
		
		
		if (item.active == false) {
			
			item.active = true;

			var j = item.jumpSize;
			var t = item.actPosition + j;
	
			var max = item.counter - item.viewportSize + 1;
	
			if (t > max) {
				
			/*	t = max;
				j = max - item.actPosition; */ 
				t = 1;
				j = (max * -1)+1;
				item.pageIndex = -1;
			}
				
			item.actPosition = t;
			
			item.pageIndex++;
			util.slider.checkArrows(item);
			
			var x = item.itemWidth * j * (-1);
			util.slider.slide(item, x, 0);
		}
	},
	
	
	
	slideLeft : function(event) {
		
		event.stop();
		
		var e = event.element();			
		
		if (e.nodeName == 'IMG') {
			e = e.parentNode;
		}
		
		e.blur();

		var cid = parseInt(e.getAttribute('cid'));
		var item = util.slider.cache[cid];

		if (!item) {
			
			log("slideLeft - invalid cid: "+cid, 2, this.name);
			return false;
		}
		
		
		if (item.active == false) {
			
			item.active = true;
		
			var j = item.jumpSize;
			var t = item.actPosition - j;
	
			var max = item.counter - item.viewPortSize + 1;
	
			if (t < 1) {
				
				/*t = 1;
				j = item.actPosition - 1; */
				t = item.counter; 
				j= (t * -1) + 1;;
				item.pageIndex = t;
			}
	
			item.actPosition = t;
			
			item.pageIndex--;
			util.slider.checkArrows(item);
			
			var x = item.itemWidth * j;
			util.slider.slide(item, x, 0);
		}
	},
	
	
	slide : function(item, x, y) {

		new Effect.Move(item.itemContainer, {
			x: x,
			y: y,
			mode: 'relative',
			transition: Effect.Transitions[util.slider.p.transition],
			duration: util.slider.p.duration,
			afterFinish : function() {
			
				item.active = false;
			}
		});
	}
	
/*
slide : function(item, x, y) {
		alert ('x: '+x+' y: '+y);
	  new Effect.SmoothScroll(item.itemContainer, {
	   x: x,
	   y: y,
	   transition: Effect.Transitions.sinoidal,
	   duration: util.slider.p.duration,
	   afterFinish : function() {
	    item.active = false;
	   }
  });
 } */

});

/*
Effect.SmoothScroll = Class.create();
Object.extend(Object.extend(Effect.SmoothScroll.prototype, Effect.Base.prototype), {
  initialize: function(element) {
    this.element = $(element);
    var options = Object.extend({
      x:    0,
      y:    0,
      mode: 'absolute'
    } , arguments[1] || {}  );
    this.start(options);
  },
  setup: function() {
    if (this.options.continuous && !this.element._ext ) {
      this.element.cleanWhitespace();
      this.element._ext=true;
      this.element.appendChild(this.element.firstChild);
    }
   
    this.originalLeft=this.element.scrollLeft;
    this.originalTop=this.element.scrollTop;
   
    if(this.options.mode == 'absolute') {
      this.options.x -= this.originalLeft;
      this.options.y -= this.originalTop;
    } 
  },
  update: function(position) {   
    this.element.scrollLeft = this.options.x * position + this.originalLeft;
    this.element.scrollTop  = this.options.y * position + this.originalTop;
  }
}); */

util.prototype.slider = new sliderObserver('slider');





