/*
 * jQuery UI Accordion 1.6
 *
 * Copyright (c) 2008 AUTHORS.txt (http://ui.jquery.com/about)
 * Dual licensed under the MIT (MIT-LICENSE.txt)
 * and GPL (GPL-LICENSE.txt) licenses.
 *
 * http://docs.jquery.com/UI/Accordion
 *
 * Depends:
 *	ui.core.js
 */
(function($) {

$.widget("ui.viewfinder", {
		 
		 _init: function() {
			this._slide = this.element;
			
			// Check structure and count frames
			this._collectFrames();
			this._currentFrame = this.options.framesPerSlide;
			/// Start with prev button hidden
			$("#"+this.options.prevTrigger).hide();
			this.noPrev = true;
			this._setupSkin();
			var that = this;
			$("#"+this.options.nextTrigger).click(function() {that._slideNext();});
			$("#"+this.options.prevTrigger).click(function() {that._slidePrev();});
		 },
		 
		 _collectFrames: function() {
			this._frames = (this.options.slideClass=='') ? this._slide.children() : this._slide.children('.'+this.options.slideClass);
			this._frameCount = this._frames.length;
			this._frameWidth = $(this._frames.get(0)).outerWidth();
		 },
		 
		 _setupSkin: function() {
			 this._slide.wrap('<div class="vf_frame"></div>');
			 $(".vf_frame").css({
								width: this.options.width+'px',
								height: this.options.height+'px',
								position: 'relative',
								overflow: 'hidden'
								});
			 this._slide.css({
							 width: (this._frameWidth*this._frameCount)+'px',
							 position: 'absolute'
							 });
		 },
		 
		 _slideNext: function() {
			 	if(this.noPrev) {
					$("#"+this.options.prevTrigger).fadeIn({duration: 100});	
				}
				this._slide.animate({left: '-='+(this._frameWidth*this.options.framesPerSlide)}, 1000);
				this._currentFrame = this._currentFrame+this.options.framesPerSlide;
				if(this._currentFrame>=this._frameCount) {
					$("#"+this.options.nextTrigger).fadeOut({duration: 100});
					this.noNext = true;
				}
		 },
		 
		 _slidePrev: function() {
			 if(this.noNext) {
				$("#"+this.options.nextTrigger).fadeIn({duration: 100}); 
			 }
			this._slide.animate({left: '+='+(this._frameWidth*this.options.framesPerSlide)}, 1000);
			this._currentFrame = this._currentFrame-this.options.framesPerSlide;
			if(this._currentFrame<=this.options.framesPerSlide) {
				$("#"+this.options.prevTrigger).fadeOut({duration: 100});
				this.noPrev = true;
			}
		 }
		 
		 });

$.extend($.ui.viewfinder, {
		 
		 defaults: {
			width: 300,
			height: 200,
			nextTrigger: 'vf_next',
			prevTrigger: 'vf_prev',
			slideClass: 'slide',
			framesPerSlide: 1
		 }
		 
		 });

})(jQuery);
