/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

(function($) {
	$.fn.PanoSlider = function(options) {
		
		var $this = $(this);
		
		$this.current = 0;
		$this.settings = {
			'width' : '800',
			'height' : '600',
			'navigator':'#slider-nav'
		};
		
		if ( options ) { 
			$.extend( $this.settings, options );
		}
    
		$this._init = function(){
			var $children = $this.children();
			
			if($children.length > 0){
				$this._addNavButton('prev','<');
			}
			
			for(var i=0; i<$children.length; i++){
				if($($children[i]).attr('tagName') == 'A'){
					$($children[i]).addClass('plink');
					$($children[i]).css('z-index', i);
					$($children[i]).css('left', (i*parseInt($this.settings['width'])));
					//$($children[i]).click(function(event){
						//event.preventDefault();
						//console.log($(this).attr('href'));
					//})
					$this._addNavButton(i, (i+1));
					
				} else {
					$(this).remove();
				}
			}
			if($children.length > 0){
				$this._addNavButton('next','>');
			}
		}
		
		$this._addNavButton = function (rel, cha){
			var button = $('<a href="#" class="sbutton" >'+cha+'</a>');
			button.attr('rel', rel);
			$($this.settings['navigator']).append(button)
			button.click(function(event){
				event.preventDefault();
				$this._move($(this).attr('rel'))
			});
		}
		
		$this._move = function(direction){
			var $children = $this.children();
			if(direction == 'prev'){
				if($this.current > 0){
					$this.current--;
				}
			} else if(direction == 'next') {
				if($this.current < $children.length - 1){
					$this.current++;
				}
			} else {
				$this.current = parseInt(direction) ;
			}
			
			var pos = 0;
			for(var i=0; i<$children.length; i++){
				pos = (i - $this.current) * parseInt($this.settings['width']);
				$($children[i]).animate({
					left: pos
				})
			}
		}
		$this._init();
	};	
}
)(jQuery);
