/*
 * jQuery Dealer E-Process Scroller Plugin
 * Copyright (c) 2012 Dealer E-Process, all rights reserved
 * Version: 1.4.6.8
 * Requires: jQuery
 * This Plugin replaces modelscroller.js,scroller_fn.js, and dep_model_scroller.js
 */
(function( $ ){
	var methods = {
		init : function( options ) { 
			methods.debug('~===| DEP scroller |===~');
			//settings are defaults that can be overridden
			var settings={
				'time':1000,
				'height':102,
				'perpage':5,
				'easing':'swing',
				'itemTag':'li',
				'scrolleeTag':'ul',
				'prevImg':null,
				'nextImg':null,
				'itemClick': function(){methods.debug('clicked');return true;}
			};

			if ( options ) { 
				$.extend( settings, options );
			}
			$(this).addClass('dep_scroller').css('height',settings.height+'px');
			methods.debug('settings: ', settings);
			
			//main init
			this.opts = settings;
			this.l_button = $('<div class="prev"></div>');
			this.r_button = $('<div class="next"></div>');
			$(this).prepend(this.l_button);
			$(this).append(this.r_button);
			this.l_button.addClass('disabled');
			if (settings.prevImg != null) this.l_button.css('background-image','url("'+settings.prevImg+'")');
			if (settings.nextImg != null) this.r_button.css('background-image','url("'+settings.nextImg+'")');
			this.shrink_wrap = $(this).find('ul');
			this.shrink_wrap.css('float','left').wrap('<div class="scrollee"></div>');
			this.scrollee = $(this).find('.scrollee');
			this.peep_width = $(this).innerWidth();
			this.itemW = Math.round( (this.peep_width-(10*settings.perpage)) / settings.perpage );
			
			methods.debug('width debug: ', {'peep width':this.peep_width,'itemW':this.itemW});
			
			var this1=this;
			
			//init each item
			$(this).find(this.opts.itemTag).each(function(i,element){
				$(this)
					.addClass('item')
					.css('height',this1.opts.height+'px')
					.css('width',this1.itemW+'px')
					.css('margin-left','5px')
					.css('margin-right','5px')
					.click(this1.opts.itemClick)
					.find('img')
						.attr('alt','')
						.error(function(){
							//$(this).parents('li.item').remove();
							var fallback = $(this).attr('data-image2');
							var unavail = 'http://www.dealereprocess.net/en/images/unavailable_small.gif';
							$(this).attr('src',fallback);
							$(this).attr('data-image2',unavail);
							methods.debug('image error index: '+i+' replacing with '+fallback);
						});
			});
			
			//init buttons
			methods.scroll_button_init.apply(this,[this.l_button,  1]);	
			methods.scroll_button_init.apply(this,[this.r_button, -1]);	

			return this;	
		},
	
		scroll_button_init : function($button,direction){
			var this1 = this;
			$button.css('height','100%').click(function(){
				if( ! $(this1.scrollee).hasClass('moving') ){
					var pos = $(this1.scrollee).position().left;
					var w = (this1.itemW + 10) * this1.opts.perpage;//$(this1).outerWidth();
					var newpos = ( pos + (direction * w) );
					methods.debug('click',{'pos':pos,'w':w,'newpos':newpos});
					methods.scroller.apply(this1,[newpos]);
				}else{
					methods.debug('moving already');
				}
			});
			return this;
		},
		scroller : function(newpos){
			var this1 = this;
			var far_left = 0;
			var shrink_width = $(this.shrink_wrap).outerWidth();
			//var peep_width = $(this.scrollee).parent().width();
			var far_right = shrink_width - this.peep_width;
			far_right = far_right*-1;//negative number because it starts at left:0
			var disable_l = false;
			var disable_r = false;
			/*
			if(newpos> far_left + ((this1.itemW*3)-1) ) {
                                newpos= far_right;
                        }
                        if(newpos<far_right - ((this1.itemW*3)+1)){
                                newpos = far_left;
                        }
			*/
			if(newpos>=far_left) {
				newpos= far_left;
				disable_l = true;
			}
			if(newpos<=far_right){
				newpos = far_right;
				disable_r = true;
			}

			this.scrollee.addClass('moving').animate(
				{left: newpos},
				{
					duration : this1.opts.time,
					easing : this1.opts.easing,
					complete : function(){
						if(disable_l) {
							$(this1.l_button).addClass('disabled');
						}else if($(this1.l_button).hasClass('disabled')) {
							$(this1.l_button).removeClass('disabled');
						}

						if(disable_r) {
							$(this1.r_button).addClass('disabled');
						}else if($(this1.r_button).hasClass('disabled')) {	
							$(this1.r_button).removeClass('disabled');
						}
						$(this).removeClass('moving');
						methods.debug('Anim. Complete: ' , {
							'disable_l':disable_l,
							'disable_r':disable_r,
							'far_right':far_right,
							'far_left':far_left,
							'newpos':newpos,
							'shrink_width':shrink_width,
							'itemW':this1.itemW,
							'duration':this1.opts.time,
							'easing':this1.opts.easing
						});
					}//end complete

				}
			);//end animate

			return this;
		},
	
		debug: function (m,o){
                        //if ($.browser == 'msie') return;
                        if (typeof(JSON) == 'undefined') JSON = { "stringify":function(o){ return ''; } };
                        if (typeof(console) == 'undefined') console = { "log":function(m){ return; } };
                        if (typeof(o)==='object') o = JSON.stringify(o);
                        else o = '';
                        if (typeof(console === 'object' )) console.log(m+o);
                        return this;
                }
	};

	$.fn.depScroller = function( method ) {

		// Method calling logic
		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( 'Method ' +  method + ' does not exist' );
		}    

	};

})( jQuery );

