PianoSlideShow = function(options) {
  this.options = $.extend({
	      element: '.piano-slideshow-gallery',
	      speed: 500,
	      delay: 7000,
				active: '.piano-slideshow-gallery-current',
	      prev_element: '.piano-slideshow-gallery-prev',
	      next_element: '.piano-slideshow-gallery-next',
				title_element: '.piano-slideshow-gallery-title',
				prev_element_active: true,
				next_element_active: true
	    }, options || {});
	
  this.gallery_ul = $(this.options.element + ' ul');
	this.init();
};

PianoSlideShow.prototype = {
	init: function()
	{
		var self = this;
		self.timer();
		
		// Prev
		$(self.options.prev_element).live('click',function(){
			if ($('ul:animated').length <= 0) {
				if (typeof(window.timing) != 'undefined') {
					clearTimeout(window.timing);
				}
				self.prev();
			}
			
			if (this.getActualMargin() >= 0) {
				$(this.options.prev_element).hide();
			}
			
			if (parseInt(this.gallery_ul.find('li').length) == 1) {
				$(this.options.next_element).hide();
				$(this.options.prev_element).hide();
			}
			
			
		});
		
		// Next
		$(self.options.next_element).live('click',function(){
			if ($('ul:animated').length <= 0) {
				if (typeof(window.timing) != 'undefined') {
					clearTimeout(window.timing);
				}
				self.nextt();
			}
		});
	},
  timer: function() {
    var self = this;
    window.timing = setTimeout(function() {
      self.nextt();
    },this.options.delay)
  },
	hasPrev: function()
	{
		return parseInt($(this.gallery_ul).find(this.options.active).prev().length);
	},
  change: function(direction) {
		
		var active = $(this.options.active);
		var next = $(this.options.active).next();
		this.gallery_ul.find('li').removeClass(this.options.active.substring(1));
		
		if (!next.length) {
			next = this.gallery_ul.find('li:first');
			next.addClass(this.options.active.substring(1));
			$(this.options.next_element).hide();
			$(this.gallery_ul).animate({
				'margin-left': 0
			}, this.options.speed);
			
		} else {
			if (direction == 'left') {
				next.addClass(this.options.active.substring(1));
			} else {
				next = active.prev();
				next.addClass(this.options.active.substring(1));
			}
			
			if (this.getActualMargin() + (this.getImageWidth() * (this.gallery_ul.find('li').length-this.getImageWidth())) >= 0) {
				margin = this.getActualMargin();
			} else {
				if (direction == 'left') {
					margin = this.getActualMargin() - this.getImageWidth()
				} else {
					margin = this.getActualMargin() + this.getImageWidth()
				}
				
			}
			
			$(this.options.next_element).show();
			$(this.gallery_ul).animate({
				'margin-left': margin
			}, this.options.speed);
		}
		
		
		$(this.options.title_element).text(next.find('img').attr('alt'));
		this.timer();
  },
	nextt: function()
	{
		this.change('left');
	},
	prev: function()
	{
		this.change('right');
	},
	getGeneralWidth: function() {
    return parseInt(this.gallery_ul.width());
  },
  getActualMargin: function() {
    margin = this.gallery_ul.css('margin-left');
    return parseInt(margin.substring(0,margin.length-2));
  },
  getImageWidth: function() {
    return parseInt(this.gallery_ul.find('li').width());
  }
};

$(function(){
  var slide_home = new PianoSlideShow();
});
