(function($) {

	/**
	 * Motionlab Homepage Slider
	 * @author Joe Critchley, Motionlab
	 * @date 2010-03-08
	 */

	$.fn.ml_homepage = function(options) {
	
		var defaults = {
			loadClass: 'slideshow-enabled',
			busyClass: 'loading',
			slideshowClass: 'slideshow',
			disabled: 'disabled',
			itemWidth: 710,
			easing: 'easeOutQuad',
			next: 'nextBtn',
			duration: 500
		};
		
		options = $.extend(defaults, options);
		
		return this.each(function() {
		
			/**
			 * Selectors
			 */
			var $obj = $(this),
				$slideshow = $('.' + options.slideshowClass, $obj),
				$innerSlideshow = $('ul', $slideshow),
				$images = $('li', $innerSlideshow),
				$next = null,
				$busy = null,
				$apr = $('.apr', $obj),
				$creditTerms = $('.creditTerms', $obj);
						
			/**
			 * Dynamics
			 */
			var animating = false,
				currentIndex = 1,
				loadTimeout = null;
						
			/**
			 * init()
			 * Initial function to get things set-up.
			 * @return void
			 */		
			function init() {
			
				$next = $('<a class="next" href="#">Next Kitchen</a>').prependTo($('.controls', $obj));
				//$next = $('.next');
				$busy = $('<div class="' + options.busyClass + '">Loading...</div>').prependTo($slideshow);
			
				// Bind the events.	
				$next.bind('click', next);
				//$('.homeSlideControls li a').bind('click', next);
									 				 				
				// Put the "[rel]" attributes into the selectors' data()
				$images.each(function(i) {
					var $__image = $(this);
					var $__a = $('a[rel]', $__image);	
					if($__a.length) {
						var __data = $.parseJSON($__a.attr('rel'));
						$.each(__data, function(j, x) {
							$__image.data(j, x);	
						});
					}				
				});
				
				// A bit of PE for "slideshow enabled" class
				$slideshow.addClass(options.loadClass);	

			}
			 			 					
			/**
			 * next()
			 * Go to the next image (or the beginning if at the end)
			 * @return Boolean false
			 */	
			function next(event) {
			
			//console.log('nextfired');
				
				if(animating == true) return false;
														
				animating = true;
										
				$.ajax({
					url: '/categories/homepage_slideshow_json',
					type:'post',
					data: {'currentIndex': currentIndex},
					dataType: 'json',
					beforeSend: function(data) {
						$busy.fadeIn(2000);
					},
					success: function(response) {						
						
						$busy.stop(true).fadeOut(100);
									
						// Prepare new image.
					
						$next = $('<li style="z-index: 1;"><a href="' + response.item.url + '"><img src="' + response.item.image + '" alt="' + response.item.title + '" /></a></li>')
						.appendTo($innerSlideshow);											
						
						// Refresh the extra details (titles, IFC, etc.)
						$('h2', $obj).html(response.item.title);
												
						// "Was price"
						var $was_wrap = $('.was_wrap', $obj);
						$was_wrap.parent().unbind('click').bind('click',function(){window.location = response.item.url;})
						if(response.item.was=='0') response.item.was = '';
						$was_wrap.html(response.item.was);
											
						if($was_wrap.html() == '') {
							$was_wrap.hide();
						} else {
							$was_wrap.html('<strong>SAVE &pound;' + $was_wrap.html() + ' NOW</strong>');
							$was_wrap.show();	
						}
												
						// "Now price"
						var $now_wrap = $('.now_wrap', $obj);
						$now_wrap.parent().unbind('click').bind('click',function(){window.location = response.item.url;});
						
						if(response.item.now=='0') response.item.now = '';
						if(response.item.now != '') response.item.now = '<strong>ONLY &pound;' + response.item.now + '</strong>';
						$now_wrap.html(response.item.now);
						if(response.item.now=='' && response.item.was=='')
						{
							$('.pricing').addClass('noPricing');
							$('.terms').hide();
						}
						else
						{
							$('.pricing').removeClass('noPricing');
							$('.terms').show();
						}
						
					
						// "Interest Free Credit" ?
						$('.creditTermsCreditWeekly').html('only &pound;' + response.item.weekly+' <br/> Per Week')
						if(response.item.ifc == false) {
							//$apr.fadeOut();
							$('#creditTermsCredit').fadeOut();
							$('#creditTermsNoCredit').fadeIn();
						}
						else {
							//$apr.fadeIn();
							$('#creditTermsNoCredit').fadeOut();
							$('#creditTermsCredit').fadeIn();
						}
																	
						// Fade out the current image, revealing the new one.
						$images.css('z-index', '2').filter(':first').fadeOut(1000, function() {
							$(this).remove();
							$images = $('li', $innerSlideshow);
							currentIndex = response.index;
							animating = false;	
						});
					
					}
				});
																													
				// return false; // Prevent default and stop bubbling already!
				
			}
			
			init();
		
		});
			
	};

})(jQuery);