var logger = { debug:function(msg){}, info:function(msg){}, warn:function(msg){}, error:function(msg){}, fatal:function(msg){} };
function ImageSlider(){}

ImageSlider.prototype = {
    initialize: function(slidingArea, options) {
        this.random = Math.random();
		
        this.log = this.getLog();
        this.log.info("initializing Slider " + this.random);

        this.options = jQuery.extend(this.DefaultOptions, options);
        this.slidingArea = jQuery(slidingArea);

		
        this.slidingContent = this.slidingArea.find(":first");
		this.log.debug("O sliding content " + this.slidingContent);
		this.slidingItems = this.slidingContent.children();
		
		this.log.debug("Total de itens " + this.slidingItems.length);
		
		width = 0;
		if ($.browser.webkit) {
    		width = 60;
		}
		var firstItem = null;
		for(var i=0; i<this.slidingItems.length; i++)
		{
			var item = this.slidingItems[i];
			if(firstItem == null) 
				firstItem = item;
			width += jQuery(item).width() + this.options.margin;
		}
		//if(firstItem != null) width += jQuery(firstItem).width();
		this.slidingContent.width(width);

        this.transition = this.Transition;
		
        this.areaWidth = this.slidingArea.width();
        this.totalWidth = this.slidingContent.width();
		

        this.initialPosition = 0;
        this.currentPosition = this.initialPosition;

        this.currentInterval = 0;

        this.currentX = 0;

        this.finalPosition = this.totalWidth - this.areaWidth;
        this.moveInterval = this.finalPosition / this.options.numSteps;

        this.log.debug("finalPosition:" + this.finalPosition);
        this.log.debug("moveInterval:" + this.moveInterval);

        var interval = 1000 / this.options.fps;
        this.log.debug("Setting interval of " + interval);

        this.intervalID = window.setInterval( jQuery.proxy( function() { this.enterFrame(); } , this) , interval);

        this.isInitialized = true;

		
		this.log.debug("Buttons:[" + this.options.btnLeft + "][" + this.options.btnRight + "]");

//        jQuery(this.options.btnLeft).bind("mouseover", jQuery.proxy( function(){ this.moveLeft(); } , this));
	//	jQuery(this.options.btnRight).bind("mouseover", jQuery.proxy( function(){ this.moveRight(); } , this));
        
		//jQuery(this.options.btnLeft).bind("mouseout", jQuery.proxy( function(){ this.stopMoving(); } , this));
		//jQuery(this.options.btnRight).bind("mouseout", jQuery.proxy( function(){ this.stopMoving(); } , this));


        jQuery(this.options.btnLeft).bind("click", jQuery.proxy( function(){ this.nextPage(); return false; } , this));
		jQuery(this.options.btnRight).bind("click", jQuery.proxy( function(){ this.previousPage(); return false; } , this));


        //Event.observe('window', "mouseup", (function(){this.stopMoving()}).bind(this) );

    },
	checkbuttons:function(){
		if(this.currentPosition == this.finalPosition) 
			$(this.options.btnLeft).hide();
		else 
			$(this.options.btnLeft).show();

		if(this.currentPosition == this.initialPosition) 
			$(this.options.btnRight).hide();
		else 
			$(this.options.btnRight).show();
	
	},
    enterFrame: function() {
	


			
	

        this.currentPosition += this.currentInterval;

        this.currentPosition = this.currentPosition > this.finalPosition ? this.finalPosition : this.currentPosition;
        this.currentPosition = this.currentPosition < this.initialPosition ? this.initialPosition : this.currentPosition;


        var dist = this.currentPosition - this.currentX;

        var step = 0;
        step = this.transition(dist);

        this.currentX += Math.floor(step);
		
	
		
		
        this.slidingContent.css({ left: -this.currentX + "px", position: "relative" });

    },
    moveLeft: function() {
        this.isInitialized = false;
        this.log.info("Start moving left");
        this.currentInterval = this.moveInterval;
        this.transition = this.Transition;
    },
    moveRight: function() {
        this.isInitialized = false;
        this.log.info("Start moving right");
        this.currentInterval = -this.moveInterval;
        this.transition = this.Transition;
    },
    stopMoving: function() {
        this.isInitialized = false;
        this.log.info("Stop moving");
        this.currentInterval = 0;

        var dist = this.currentPosition - this.currentX;
        if (dist > 400) this.currentPosition = this.currentX + 400;
        if (dist < -400) this.currentPosition = this.currentX - 400;
        this.transition = this.Transition;

    },
    nextPage: function() {
        this.log.info("NextPage: " + this.random);
        this.currentPosition += (this.areaWidth - (this.areaWidth / this.options.numSteps));
        this.transition = this.PageTransition;
    },
    previousPage: function() {
        this.log.info("PrevPage: " + this.random);
        this.currentPosition -= (this.areaWidth - (this.areaWidth / this.options.numSteps));
        this.transition = this.PageTransition;
    },
    dispose: function() {
        jQuery.die('mouseover', jQuery(this.options.btnLeft));
        jQuery.die('mouseover', jQuery(this.options.btnLeft));
        jQuery.die('mouseover', jQuery(this.options.btnLeft));
        jQuery.die('mouseover', jQuery(this.options.btnLeft));
		
        window.clearInterval(this.intervalID);
		
		
    },
	getLog:function()
	{
		return logger;
	},
	Transition:function(dist)
	{
		if(dist > 400)
		{
			dist = 400;
		} 
		if(dist < -400)
		{
			dist = -400;
		}

		return dist * 0.05;		
	},
	PageTransition:function(dist)
	{
		return dist * 0.23;
	},
	DefaultOptions:
	{
		numSteps:10,
		fps:16,
		direction:"H",
		margin:0,
		btnLeft:"#btnLeft",
		btnRight:"#btnRight",
		transition:this.Transition
	}
}

