//MH
/*
 * Known bugs: 
 * 	scrollTo always scrolls directly to the pane, even with wrapAround enabled
 * 	scrollTo only calls the call back once ( at scroll completion )
 * */
(function( $ ){
	
	var methods={
		scrollTo		: function ( pane ) {
							var $this=$(this),
								data=$this.data('imagescroll'),
								settings=data['settings'],								
								width=$this.find('.image .allimages .imgContainer:first').width(),
								numberOfImages=$this.find('.image .allimages .imgContainer:not(.wrapAroundLast,.wrapAroundFirst)').length,
								currentPane=Math.abs(parseInt($this.find('.image .allimages').css('margin-left')))/width,
								destination,
								panes;
							if ( currentPane==pane ) { return this; }
							if ( settings.wrapAround ){	
								if ( pane > (numberOfImages-settings.numbershown +1) ){
									pane=numberOfImages-settings.numbershown +1;
								}
							}	
							destination=pane*-width;
							panes=Math.abs(pane-currentPane);
							$this.find('.image .allimages').animate({'marginLeft':(destination)+'px'}, {duration:panes*settings.speed, complete:function(){methods['checkVisibility']($this)}});								
							return this;
						},
		jumpTo			: function ( pane ) {
							var $this=$(this),
								data=$this.data('imagescroll'),
								settings=data['settings'],								
								width=$this.find('.image .allimages .imgContainer:first').width(),
								numberOfImages=$this.find('.image .allimages .imgContainer:not(.wrapAroundLast,.wrapAroundFirst)').length,
								currentPane=Math.abs(parseInt($this.find('.image .allimages').css('margin-left')))/width,
								destination;
							if ( currentPane==pane ) { return this; }
							if ( settings.wrapAround ){	
								if ( pane > (numberOfImages-settings.numbershown +1) ){
									pane=numberOfImages-settings.numbershown +1;
								}
							}	
							destination=pane*-width;
							$this.find('.image .allimages').css({'marginLeft':(destination)+'px'});
							methods['checkVisibility']($this);
							return this;			
						},
		checkVisibility : function (selected,offset){
							var $selected=$(selected),
								data=$selected.data('imagescroll'),
								settings=data['settings'],
								numbershown = settings.numbershown,
								offset = offset||0;

							if( $selected.find('.image .allimages .imgContainer').length == 0 ){
								$selected.find('.image .arrow').hide();
								return $selected;
							}				
							var numberOfImages=$selected.find('.image .allimages .imgContainer').length,
								width=$selected.find('.image .allimages .imgContainer:first').width(),
								maxWidth=numberOfImages*width;
							$selected.find('.image .allimages').css('width', maxWidth+'px');
							
							var pos=parseInt($selected.find('.image .allimages').css('margin-left'))+offset;					
							
							if ( settings.wrapAround ){	
								if ( pos>=0 ) {
									pos=-maxWidth+width+(width*settings.numbershown);
									$selected.find('.image .allimages').css({'marginLeft':pos+'px'});
								}else if ( pos<=-maxWidth+(width*settings.numbershown) ){
									pos=-width;
									$selected.find('.image .allimages').css({'marginLeft':pos+'px'});
								}
							}
							
							if ( pos>=0 ){
								$selected.find('.image .arrow:first').hide();
							} else {
								$selected.find('.image .arrow:first').show();
							}
							if ( pos<=-maxWidth+width){
								$selected.find('.image .arrow:last').hide();
							} else {
								$selected.find('.image .arrow:last').show();
							}
							//call callback
							if ( typeof(settings.callback)==="function" ){
								currentImage=pos*-1/width +1;
								if ( settings.wrapAround ){					
									numberOfImages-=2;
									currentImage-=1;
								}					
								settings.callback($selected,currentImage,numberOfImages);
							}
							return $selected;
						},		
		init			: function ( options ) {
							var settings = {
								'numbershown' : '1',
								'autoscroll' : 'false',
								'speed' : '600',
								'callback' : false,
								'wrapAround': false,
								'autoPlay':false
							};
							return this.each(function() {
								if ( options ) { 
									$.extend( settings, options );
								}
																
								var $this=$(this),
									data= $this.data('imagescroll');

								if ( ! data ){
									if ( $this.find('.image.container .allimages').length==0 ) {
										//fake innerwrap
										var t =$this.children();
										$this.append('<div class="image container"><div class="allimages"></div><div class="arrow left"></div><div class="arrow right"></div></div>');
										$this.find('.image.container .allimages').append(t);
										$this.find('.allimages').children().wrap('<div class="imgContainer"></div>');
									}
									
									$this
										.find('.image .arrow')
										.hover(function(){$(this).addClass("over");},function(){$(this).removeClass("over");})
										.bind('click.imagescroll',function(){
											var data=$this.data('imagescroll'),
												settings=data['settings'];
											if ( $this.find('.image .allimages:animated').length > 0 ) { return; }
											
											var width=$this.find('.image .allimages .imgContainer:first').width();
											var pos=parseInt($this.find('.image .allimages').css('margin-left'));
											if ( $(this).hasClass('right') ){
												$this.find('.image .allimages').animate({'marginLeft':(pos-width)+'px'}, {duration:settings.speed, complete:function(){methods['checkVisibility']($this)}});
											} else {				
												$this.find('.image .allimages').animate({'marginLeft':(pos+width)+'px'}, {duration:settings.speed, complete:function(){methods['checkVisibility']($this)}});
											}				
											if ( $(this).hasClass('over') ) { 
												//settings.autoPlay=false; 
												window.clearTimeout($this.data('autoPlay'));
											}
											if ( settings.autoPlay ){
												if ( typeof(settings.autoPlay)==='string' && parseInt(settings.autoPlay)=='NaN') {
													settings.autoPlay=$.fx.speeds[settings.autoPlay];
												}else{
													settings.autoPlay=parseInt(settings.autoPlay);
												}
												$this.data('autoPlay',window.setTimeout(function(){
														$this.find('.image .arrow:last:visible').click();
												},settings.autoPlay));
											}
										});
									
									$this.data('imagescroll',{'settings' : settings});
								}else{ 
									settings=data['settings'];	
									$.extend( settings, options );
									$this.data('imagescroll',{'settings' : settings});
								}
								
								
								
								if ( settings.wrapAround ){
									if ( $this.find('.allimages').find('.wrapAroundLast,.wrapAroundFirst').length == 0 ){
										var last=$this.find('.allimages').children(':last').clone().addClass('wrapAroundLast');
										var first=$this.find('.allimages').children(':first').clone().addClass('wrapAroundFirst');
										$this
											.find('.allimages')
											.append(first)
											.prepend(last)
											.css({'marginLeft':'-'+first.width()+'px'});
									}
								}else{
									$this
										.find('.allimages')
										.find('.wrapAroundLast,.wrapAroundFirst')
										.remove();
								}

								
									
								methods['checkVisibility']($this);
								if ( settings.autoPlay ){
									if ( typeof(settings.autoPlay)==='string' && parseInt(settings.autoPlay)=='NaN') {
										settings.autoPlay=$.fx.speeds[settings.autoPlay];
									}else{
										settings.autoPlay=parseInt(settings.autoPlay);
									}
									$this.data('autoPlay',window.setTimeout(function(){
											$this.find('.image .arrow:last:visible').click();
									},settings.autoPlay));
								}else{
									window.clearTimeout($this.data('autoPlay'));
								}
							});
						}
	}
	
	$.fn.imagescroll = function( method ) {  
			if ( methods[method] ) {
				return methods[ method ].apply( this, Array.prototype.slice.call( arguments, 1 ));
			}else if (  typeof method === 'object' || ! method  ){
				return methods.init.apply( this, arguments );
			} else {
				$.error( '$.imagescroll does not support the method ' + method );
				return this;
			}

	};
})( jQuery );

