/**
 * Rollers - White Pixels Rollover Plugin, by default swaps images for ones with an _on suffix
 * 
 * This can be overridden by passing a suffix property to define an alternate suffix or a rollTo URL for an image to roll to.
 * Similarly an alternate source image can be specified rollFrom so it swaps back to a different image.
 * 
 * rollTo and rollFrom are best used when applying the rollover to a single images, rather than an array of images,
 * as all images in the array will use the same rollTo/rollFrom sources.
 * 
 * @param options $
 */
(function($) {
	// plugin definition
	$.fn.roller = function(options) {
	
		//Update the options object
		var opts = $.extend({}, $.fn.roller.defaults, options);	
		$(this).each(function(){
			//If no specific rollTo/From are provided then use the suffix to generate them
			if (opts.rollTo === ""){
				if (opts.rollType != 'back') {
					$.data(this, 'rollTo', addSuffix(this.src, opts.suffix));
				} else {
					var srcTo = $(this).css('backgroundImage');
					srcTo = srcTo.replace(/"/g,"").replace(/url\(|\)$/ig, "");
					$.data(this, 'rollTo', addSuffix(srcTo, opts.suffix));					
				}
			} else {
				$.data(this,'rollTo', opts.rollTo);
			}
			if (opts.rollFrom === ""){
				if (opts.rollType != 'back') {
					$.data(this, 'rollFrom', this.src);
				} else {
					var srcFrom = $(this).css('backgroundImage');
					srcFrom = srcFrom.replace(/"/g,"").replace(/url\(|\)$/ig, "");			
					$.data(this, 'rollFrom', srcFrom);
				}
			} else {
				$.data(this,'rollFrom', opts.rollFrom)
			}			
			//Preload any required images
			this.RollToImage= new Image();
			this.RollToImage.src = $.data(this,'rollTo');
			this.RollFromImage= new Image();
			this.RollFromImage.src = $.data(this,'rollFrom');	
			
			if(opts.rollType != 'back'){
				//Attach hover actions
				$(this).hover(
					function(){
						//Over function
						this.src = this.RollToImage.src;
					},
					function(){
						//Out function
						this.src = this.RollFromImage.src;
					});				
			} else {
				//Attach hover actions
				$(this).hover(
					function(){
						//Over function
						$(this).css('backgroundImage','url(' + this.RollToImage.src + ')');
					},
					function(){
						//Out function
						$(this).css('backgroundImage','url(' + this.RollFromImage.src + ')');
					});					
			}
				
		})
		var original = this.src;

			
		return this;
	};
	
	//Default to an _on suffix
	$.fn.roller.defaults = {
			suffix: '_on',	//Normally we roll to the _on filename
			rollTo: '',		//Typically we don't specify to and from
			rollFrom: '',
			rollType: 'front'	//If set to back change the CSS background image
		 };
	
  	/**
  	 * Removes suffix b from filename a without removing the file extension
  	 * @param string a A filename
  	 * @param string b A filename suffix eg "_on"
  	 */
	function remSuffix(a,b){
		var lastdot = a.lastIndexOf(b + '.');
		if (lastdot==-1){
			return a;
		} else {
			var ext = a.slice(lastdot+b.length,a.length);
			return a.slice(0,lastdot) + ext;
		}		
	}
	
	/**
	 * Adds suffix b to filename a without removing the file extension
	 * @param string a A Filename
	 * @param string b A suffix to append to the filename
	 */
	function addSuffix(a,b){
		var lastdot = a.lastIndexOf(".");
		if (lastdot==-1){
			return a;
		} else {
			var ext = a.slice(lastdot,a.length);
			return a.slice(0,lastdot) + b + ext;
		}
		
	}	
	
	//
	// private function for debugging
	//
	function debug($obj) {
	if (window.console && window.console.log)
		window.console.log($obj);
	};	
	
})(jQuery);
