(function( $ ){
	var methods = {
		init : function( options ) { 
			methods.debug('~===| DEP incentive flipper |===~');
			var settings={
				'time':1000,
				'timeOut':3000,
				//'height':218,
				'easing':'swing',
				'slideClick':function(){return false;},
				'itemTag':'li',
				'container':'.shrinkwrap'
			};

			if ( options ) { 
				$.extend( settings, options );
			}
		
			this.opts = settings;
			$(this).addClass('incentive_flipper');
			var loadsrc = $(this).find('a').attr('href');
			methods.debug(loadsrc);
			var this1=this;
			$(this).load(loadsrc,function(){
				//main init
				methods.debug(': ');
				$(this1).find('.noclick').click(function(){alert('Please wait for content to load.');return false;});
				$(this1).css('background-image','none');
				$(this).find(this1.opts.container)
					.addClass('slideshow')
					.removeClass('shrinkwrap')//because of improper css scope in incentive_scroller.css
					.cycle({
						pause:true,
						timeOut:this1.opts.timeOut
					});

				
				//load each item
				$(this).find(this1.opts.itemTag).each(function(){
					//$(this).css('width','100px');
					$(this).addClass('load_img');
					var link = $(this).find('a');
					var loadsrc2 = $(link).attr('href');
					var bg = $(link).attr('bg');
					var data = {
						'trimplus' : $(link).attr('trimplus'),
						'vifnum'   : $(link).attr('vifnum'),
						//'modelyear': $(link).attr('year'),
						'makeid'   : $(link).attr('makeid'),
						'modelid'  : $(link).text()
					};
					var this_item = this;
					$(this).load(loadsrc2,data,function(){
						
						$.each(data,function(i,v){
							$(this_item).attr(i,v);
						});

						$(this).removeClass('load_img');
						var non = $(this).find('.nothing');
						if(non.length >0){ 
							$(this).remove();
						}else{ 
							$(this).children('.item')
								.css('background-image','url("'+bg+'")')
								.click(function(){
									this1.opts.slideClick.apply(this,[]);
								});
							$(this).find('img').each(function(img_index,img_element){
								var src = $(img_element).attr('alt');
								methods.debug(src);
								$(img_element).attr('src',src).fadeOut();
								$(img_element).remove();
							});
						}
					});
				});
				
				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;	
		},
		
		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.incentiveFlipper = 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 );


