/*
Copyright (c) 2007, Jorge Luis Dorta Palmero. All rights reserved.
E-mail: ivnmarin@gmail.com
Portions Copyright (c) 2007, Yahoo!, Inc. All rights reserved.
Code licensed under the BSD License:
http://developer.yahoo.net/yui/license.txt
*/

YAHOO.namespace("ImageScroll");

(function() {
	$D = YAHOO.util.Dom;
	YAHOO.ImageScroll = function(elem, sep) {
		this.elem = elem;
		this.sep = sep;
		this.init();
	};
	
	YAHOO.ImageScroll.prototype = {
		/**
	     * Constructor.
	     * @method init
	     */ 
		init: function() {
			YAHOO.util.Event.onContentReady('carrucel', this.animateImages, this, true);
		},
		
		animateImages : function() {
			var listImg = $D.get('images');
			
			elem = this.elem;
			var images = $D.getElementsByClassName('obra','img',listImg);
			
			this.width = -this.sep;
			
			for (var i = 0; i < images.length; i++) {
				this.width += parseInt(images[i].width)+this.sep;
			}
			
			$D.setStyle('images','width',this.width + 'px');
			
			var lastchild = $D.getLastChild('images');
			$D.setStyle(lastchild,'margin-right','0px');
			
			var attributes = {
				scroll: { to: [0, 0] }
			};
			this.anim = new YAHOO.util.Scroll('obras', attributes);
			this.anim.currentFrame = 0;
			
			this.anim.duration = 0;
			this.anim.animate();
					
			YAHOO.util.Event.on('run-left', 'mouseover', this.animateLeft, this, true);
			YAHOO.util.Event.on('run-left', 'mouseout', this.mouseout, this, true);

			YAHOO.util.Event.on('run-right', 'mouseover', this.animateRight, this, true);
			YAHOO.util.Event.on('run-right', 'mouseout', this.mouseout, this, true);
		},
				
		mouseout : function() {
			var arrow = $D.get('run-right');
			arrow.src = "images/right_arrow.gif";
			
			var arrow = $D.get('run-left');
			arrow.src = "images/left_arrow.gif";
			this.anim.stop();
		},
		
		animateRight : function() {
			var arrow = $D.get('run-right');
			arrow.src = "images/right_arrow_hover.gif";
			
			this.moveLeft = 0;
			this._animateTime(125);			
			
			this.anim.attributes.scroll.to = [this.width,0];
			
			this.anim.animate();
		},
		
		animateLeft : function() {
			var arrow = $D.get('run-left');
			arrow.src = "images/left_arrow_hover.gif";
			
			this.moveLeft = 1;
			this._animateTime(125);
			
			this.anim.attributes.scroll.to = [0,0];
			
			this.anim.animate();
		},
		
		splitContainer : function() {
			width = $D.getStyle('images','width');
			width = width.substr(0, width.length-2);
			$D.setStyle('images','width',(width/2) + 'px');
		},
		
		setWidth : function(width) {
			$D.setStyle('images','width',width + 'px');
			this.width = width;
		},
		
		_animateTime : function(fps) {
			var scroll = this.anim.getAttribute('scroll'); // posicion donde esta el scroll
			if (this.moveLeft)
				moveWidth = scroll[0];
			else
				moveWidth = this.width - scroll[0];
				
			if (moveWidth < fps)
			  moveWidth = fps;
			   
			this.anim.totalFrames = moveWidth;
			this.anim.duration =  Math.round(moveWidth / fps); // Mover a 125 fps
		}
			
	};
})();

YAHOO.register("ImageScroll", YAHOO.ImageScroll, {version: "2.3.1", build: "1"});