$.extend({
	delegate: function(fn, thisObject) {
      var parameters = Array.prototype.slice.call(arguments).slice(2);
 
      return function() {
         return fn.apply(thisObject || this, parameters.concat(Array.prototype.slice.call(arguments)));
      };
	}

});


$.widget('ui.button', {
			
	_init: function(options) {
		this.element.mouseover($.delegate(this.over, this));
		this.element.mouseout($.delegate(this.out, this));
	},
	
	
	fade: function(create) {
		if (create) {
			if (this.clone == null) {
				this.element.append(this.element.html());
				this.clone = this.element.find('> *:last');
				this.clone.addClass('over');
				this.clone.fadeTo(0, 0);
			}
			
			this.clone.stop(true);
			this.clone.fadeTo(500, 1);
		}
		else {
			if (this.clone != null) {
				this.clone.fadeTo(500, 0, $.delegate(this.remove, this));
			}
		}
	},
	
	
	remove: function() {
		if (this.clone != null) {
			this.clone.remove();
		}
		
		this.clone = null;
	},
	
	
	over: function() {
		if (this._status) return;
		this.fade(true);
	},
	
	
	out: function() {
		if (this._status) return;
		this.fade(false);	
	},
	
	
	status: function(status) {
		this._status = status
		this.element.css('cursor', (status ? 'default' : 'pointer'));
		this.fade(status);
	}

});


$.scale = function(width, height, originalWidth, originalHeight) {
	var size;

	size = new Object();
	size.width = originalWidth;
	size.height = originalHeight;

	var xScale = width / originalWidth;
	var yScale = height / originalHeight;
	var scale = (xScale > yScale) ? yScale : xScale;

	if (scale < 1) {
		size.width *= scale;
		size.height *= scale;
	}
	
	size.width = Math.round(size.width);
	size.height = Math.round(size.height);

	return size;
}


$.widget('ui.gallery', {

	_init: function() {
		var $this = this;

		if ($.browser.msie && $.browser.version < 7) {
			$(window).scroll(function() {
				$this.element.css('top', $(window).scrollTop() + 'px');		  
			});
		}
		else {
			this.element.css('position', 'fixed');	
		}
		
		this.element.find('.close').unbind().click($.delegate(this.hide, this));
	},


	update: function(id, index) {
		var $this = this;
		var data = (String(id).indexOf('<') === -1 ? $('#' + id).val() : id);
		var images = $("<div>" + data + "</div>").find('img');
		var image;
		var $element;
		var html = "";
		
		if (isNaN(index)) index = 1;
		
		index = Math.max(1, Math.min(images.length, index));

		for (var i = 0; i < images.length; i++) {
			image = images.eq(i);
			html += "<li" + (i + 1== images.length ? " class=\"last\"" : "") + "><a href=\"javascript:void(0)\" rel=\"" + (i + 1) + "\"><span><em></em><img src=\"" + image.attr('alt') + "\" /></span></a></li>";
		}

		$element = this.element.find('.thumbnails .items ul');
		$element.html(html);
		$element.css('width', Math.max(0, ((images.length * 97) - 13)) + 'px');
		$element.find('li a img').hide().load(function() {
			$(this).css('margin-left', Math.round((84 - $(this).width()) / 2) + 'px');
			$(this).show();
		});

		$element = this.element.find('.image .info');

		if (!$.browser.msie) {
			$element.show();
			$element.fadeTo(0, 0);
		}

		this.element.find('.image').unbind().hover(
			function() {
				var $element = $(this).find('.info');
				($.browser.msie ? $element.show() : $element.animate({ opacity: 1 }, 500)); 
			},
			function() {
				var $element = $(this).find('.info');
				($.browser.msie ? $element.hide() : $element.animate({ opacity: 0 }, 500));
			}
		);
		this.element.find('.image .info .button1, .image .info .button2').unbind().click(function() {
         $this.change(($(this).hasClass('button1') ? '-1' : '1'));
      });
		this.element.find('.thumbnails .items ul li a').unbind().click(function() {
			$this.change(parseInt($(this).attr('rel'), 10));																	
		}).button();

		var limit = 7;
		var pageCount = Math.ceil(images.length / limit);

		this._setData('index', null);
		this._setData('limit', limit);
		this._setData('data', images);
		this._setData('pageCount', pageCount);
		
		html = "";
		
		for (var i = 0; i < pageCount; i++)
		{
			html += "<li><a href=\"javascript:void(0)\" rel=\"" + (i + 1) + "\"" + (i == 0 ? " class=\"active\"" : "") + "></a></li>";
		}

		this.element.find('.pagination ul').html(html);

		this.element.find('.pagination ul li a').unbind().click(function() {
         $this.slide(parseInt($(this).attr('rel'), 10));
      });
		this.element.find('.pagination .button1, .pagination .button2').unbind().click(function() {
         $this.slide(($(this).hasClass('button1') ? '-1' : '1'));
      });
		
		this.element.find('.overlay').unbind().click(function() { $this.hide(); });
		this.element.find('.image img').remove();
		
		this._setData('enabled', true);
		this.element.show();

		if ($.browser.msie && $.browser.version < 7) {
			$(window).resize(function() {
				$this.element.find('.overlay').height($(this).height()); 
			});
			$(window).trigger('resize');
			
			this.element.find('.content').show();
			this.element.find('.overlay').show();
			this.change(index);
		}
		else
		{
			$element = this.element.find('.overlay');
			$element.fadeTo(0, 0);
			$element.fadeTo(500, 0.75);
			$element = this.element.find('.content');
			$element.fadeTo(0, 0);
			$element.fadeTo(500, 1, function() {
				$this.change(index);															 
			});
		}
	},
	
	
	hide: function() {
		var $this = this;

		if ($.browser.msie && $.browser.version < 7) {
			this.element.find('.content').hide();
			this.element.find('.overlay').hide();
			this.element.hide();
		}
		else {
			this.element.find('.overlay').fadeTo(500, 0);
			this.element.find('.content').fadeTo(500, 0, function() {
				$this.element.hide();																		
			});
		}
	},

	
	change: function(offset) {
		var $this = this;
		var data = this._getData('data');
		var $parent;
		var $images;
		var $image;
		var $button;

		 switch (offset) {
      	case '-1': index = (index == 1 ? data.length : index - 1); break;
         case '1': index = (index == data.length ? 1 : index + 1); break;
			default: index = offset;
      }

      if (index == this._getData('index')) return;
		
		data = data.eq(index - 1);

		this._setData('index', index);

		$parent = this.element.find('.image .info .description');
		$parent.find('p').html(data.attr('title'));

		if (data.attr('title').length > 0) {
			$parent.show();
			$parent.find('div').fadeTo(1, 0.75);
		}
		else {
			$parent.hide();
			$parent.find('div').fadeTo(0, 0);
		}

		this.element.find('.thumbnails .items ul li a').removeClass('active').button('status', false);
		
		$button = this.element.find('.thumbnails .items ul li a[rel=' + index + ']');
		$button.addClass('active').button('status', true);
		$button.find('img').each(function() {
			var $border;
			var width = $(this).width();
			var height = $(this).height();
			
			$border = $(this).parent().find('em');
			$border.css('width', (width - 6) + 'px');
			$border.css('height', (height - 6) + 'px');
			$border.css('margin-left', Math.floor((84 - width) / 2) + 'px');
			$border.css('margin-top', Math.floor((57 - height) / 2) + 'px');
		});
		
		if (!$.browser.msie) $button.find('em').fadeTo(0, 0).fadeTo(500, 1);

		$parent = this.element.find('.image');
		$parent.find('img').remove();
		$parent.append("<div class=\"file\"><img src=\"" + data.attr('src') + "\" /></div>");
		$image = $parent.find('img:last');
		$image.click($.delegate(this.change, this, '1'));
		$image.fadeTo(0, 0);
		$image.parent().fadeTo(0, 0);
		$image.load(function() {
			var size = $.scale(666, 443, $(this).width(), $(this).height());

			$image.fadeTo(0, 1);
			$image.css('width', size.width + 'px');
			$image.css('height', size.height + 'px');
			$image.css('margin-top', Math.round((443 - size.height) / 2) + 'px');
			$image.css('margin-left', Math.round((666 - size.width) / 2) + 'px');
			$image.parent().fadeTo(1000, 1);
		});

		this.slide(Math.floor((index - 1) / this._getData('limit')) + 1);
		this.element.find('.pagination .status').html("Image " + index + " of " + this._getData('data').length);
	},


	slide: function(offset)
   {
   	var $this = this;
   	var index = this._getData('page');
      var items = this.element.find('.thumbnails .items ul li');
      var item = items.eq(0);
      var length = item.width();
      var spacing = parseInt(item.css('marginRight'), 10);
		var count = Math.floor((this.element.find('.items').width() + spacing) / (length + spacing));
      var total = Math.ceil(items.length / count);
      var data = {};

      switch (offset) {
      	case '-1': index = (index == 1 ? total : index - 1); break;
         case '1': index = (index == total ? 1 : index + 1); break;
			default: index = offset;
      }
 
      data.marginLeft = -((length + spacing) * count) * (index - 1);

      this._setData('page', index);
      this.element.find('.thumbnails .items ul').stop(true).animate(data, 1000);
		this.element.find('.pagination ul li a').removeClass('active');
		this.element.find('.pagination ul li a[rel=' + index + ']').addClass('active');
   }

});


$().ready(function() {
	$('#gallery').gallery();
	
	/*
	$('.gallery-button').click(function() {											
		$('#gallery').gallery('update', $(this).attr('rel'));									
	});
	*/
});