// JavaScript Document
(function($) {
 $.fn.styleGallery = function(options) {

	return this.each(function() {
		$.data(this, 'styleGallery', new $.styleGallery.init(this, options));
		//$.styleGallery.init(this, options);
	});
 };
 
 
$.styleGallery = {
	defaults: {
		items: 7,		//total items in the list
		width: 860,
		height: 533
	},

	init: function (element, settings) {
		var self = this;
		this.b_left = $("#left");
		this.b_right = $("#right");

		this.currentItem = 0;
		this.arrItems = $("#sgallery, carousel, li");
		
		this.settings = $.extend({}, $.styleGallery.defaults, settings);
		this.settings.items = this.settings.items - 1; //Starting count at 0
		this.highlight(0);

		//Flash "Right" button
		setTimeout(function() { $("#right").fadeTo(1700, .7); $("#right").fadeTo(1400, 0); }, 1200 );
		
		
		//setup the stage
		self.setup();
		
		this.b_left.addClass("block-disabled");
		this.b_left.click(function() { self.left(self.settings.width) });
		this.b_right.click(function() { self.right(self.settings.width) });
		
		/* Show hover buttons */
		this.b_left.mouseover (function(){$(this).fadeTo("fast", .3);});
		this.b_left.mouseout  (function(){$(this).fadeTo("fast", 0);});

		this.b_right.mouseover (function(){$(this).fadeTo("fast", .3);});
		this.b_right.mouseout  (function(){$(this).fadeTo("fast", 0);});

	}
};

$.extend($.styleGallery.init.prototype, {

	mytest: function () {
		alert("Blah _ " + this.settings.panelsNo);
	},

	left: function (width) {
		if (this.currentItem > 0 && !(this.b_left.hasClass("block-disabled"))) {
			$("#sgallery, carousel, li").animate({"left": "+=" + width + "px"}, "slow");
			this.currentItem -= 1;

			if (this.currentItem <= 0)
				this.b_left.addClass("block-disabled");
			
			if (this.currentItem < this.settings.items)
				this.b_right.removeClass("block-disabled");

			//dim the current image & highlight the new
			this.dim(this.currentItem + 1);
			this.highlight(this.currentItem);
		}
	},

	right: function (width) {
		if (this.currentItem < this.settings.items && !(this.b_right.hasClass("block-disabled"))) {
			$("#sgallery, carousel, li").animate({"left": "-=" + width + "px"}, "slow");
			this.currentItem += 1;

			if (this.currentItem >= this.settings.items)
				this.b_right.addClass("block-disabled");
			
			if (this.currentItem > 0)
				this.b_left.removeClass("block-disabled");

			//dim the current image & highlight the new
			this.dim(this.currentItem - 1);
			this.highlight(this.currentItem);
		}
	},
	
	setup: function () {
		//$("#sgallery").width(this.settings.width);
		this.b_left.toggleClass("block-disabled");  //disable the Left Button
	},
	
	highlight: function (itemNumber) {
		//alert("to highlight: [" + itemNumber + "] " + $(".carousel li:eq(" + itemNumber + ")").html());  //debug
		$(".carousel li:eq(" + itemNumber + ")").fadeTo(200, 1);
	},
	dim: function (itemNumber) {
		//alert("to dim: [" + itemNumber + "] " + $("#carousel li:eq(" + itemNumber + ")").html());   //debug
		$(".carousel li:eq(" + itemNumber + ")").fadeTo(200, 0.2);
	}
	
});
})(jQuery);





