(function( $ ){
	var methods = {
		init : function( options ) { 
			methods.debug('~===| incentive scroller |===~');
			var settings={
				'time':1000,
				'l_buttonString':'.prev',
				'r_buttonString':'.next',
				'height':218,
				'perpage':3,
				'easing':'swing',
				'goal':'/page/incentives'
			};

			if ( options ) { 
				$.extend( settings, options );
			}
			var this1=this;
			$(this).addClass('incentive_scroller');
			var loadsrc = $(this).find('a').attr('href');
			methods.debug(loadsrc);
			$(this).load(loadsrc,function(){
				
				//main init
				this1.height = settings.height; 
				$(this).prepend('<div class="prev"></div>');
				$(this).append('<div class="next"></div>');
				this1.shrink_wrap = $(this1).find('.shrinkwrap');
				this1.scrollee = $(this1).find('.scrollee');
				//this1.shell_str = $(this1).find('.depricated');
				//this1.button = '.depricated';
				this1.l_button = $(this1).find(settings.l_buttonString);
				this1.l_button.addClass('disabled');
				this1.r_button = $(this1).find(settings.r_buttonString);
				this1.time = settings.time;
				this1.easing = settings.easing;
				this1.perpage = settings.perpage;
				this1.goal = settings.goal;
				this1.peep_width = $(this1.scrollee).parent().innerWidth();
				this1.itemW = Math.round( (this1.peep_width-(10*settings.perpage)) / settings.perpage );
				//methods.debug('width stuff: ', {'peep width':this1.peep_width,'itemW':this1.itemW});
				
				//load each item
				$(this).find('li').each(function(i,element){
					//$(this).css('width','100px');
					$(this).addClass('load_img');
					var link = $(this).find('a');
					var loadsrc2 = $(link).attr('href');
					var data = {
						'trimplus' : $(link).attr('trimplus'),
						'vifnum'   : $(link).attr('vifnum'),
						'modelyear': $(link).attr('year'),
						'makeid'   : $(link).attr('makeid'),
						'modelid'  : $(link).text()
					};
					if(i>=32 ) {
						$(this).remove();
						return true;
					}
					$(this).load(loadsrc2,data,function(){
						$(this).removeClass('load_img');
						var non = $(this).find('.nothing');
						var imgfail = $(this).find('img[alt="http://www.dealereprocess.net/en/images/unavailable_small.gif"]');
						if(non.length >0 || imgfail.length >0){ 
							$(this).remove();
							methods.debug('image fail removed:' + data.modelid);
						}else{ 
							$(this).children('.item')
								.css('width',this1.itemW+'px')
								.css('margin-left','10px')
								.click(function(){
									window.location = this1.goal;
								}).find('.info').css('width',this1.itemW+'px');
							$(this).find('img').each(function(image_index,image_element){
								var src = $(image_element).attr('alt');
								if (src == '') {
									$(this).parents('li').remove();
									methods.debug('removed');
									return true;
								}
								$(image_element)
									.attr('alt',data.modelid)
									.error(function(){
										methods.debug('image error for '+data.modelid);
										$(this).attr('src','http://www.dealereprocess.net/en/images/unavailable_small.gif');
										
									})
									.attr('src',src);
							});
						}
					});
				});
				
				methods.debug('loaded');
				//init buttons
				methods.scroll_button_init.apply(this1,[this1.l_button,  1]);	
				methods.scroll_button_init.apply(this1,[this1.r_button, -1]);	
			});

			return this;	
		},
	
		scroll_button_init : function($button,direction){
			var this1 = this;
			var newheight = Math.round(this.height/3);
			$button.css('height',newheight+'px').css('top',newheight+'px');
			//methods.debug(newheight);
			$button.click(function(){
				if( ! $(this1.scrollee).hasClass('moving') ){
					var pos = $(this1.scrollee).position().left;
					var w = (this1.itemW + 10) * this1.perpage;//$(this1).outerWidth();
					var newpos = ( pos + (direction * w) );
					
					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 - 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.time,
					easing : this1.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,
							'itemW':this1.itemW
						});
					}//end complete

				}
			);//end animate

			return this;
		},
	
		//hide : function() {},
		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.incentiveScroller = 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 );

