/**
 * jQuery reformshow
 * Version 0.02 - 05.07.2009
 * Author Stoyan Delev http://stoyandelev.com
 **/

(function($){  
	$.fn.reformshow = function(options) {  
   
		var defaults = {  
			firstShow 		: 0,
			pagginationSHow	: true,
			mainWidth		: 400,
			mainHeight		: 300,
			leftRightWidth	: 60,
			type			: 'animate'
		};  
		
		var options = $.extend(defaults, options); 
		var el = this; 
       	var currentShow = options.firstShow;
      
	    
	    el.addClass('refs');   	
		//el.find('img').hide();
		//el.find('img:eq(' + currentShow + ')').show(); 	
		
		var imgs = el.html();
		
		el.html('');
		el.append('<div class="refsLeftCol"><\/div>');
		el.append('<a href="#" class="refsLeftLink"><\/a>');
		el.append('<div class="refsMain"><\/div>');
		el.append('<div class="refsRightCol"><\/div>');
		el.append('<a href="#" class="refsRightLink"><\/a>');
		
		
		el.css({'width': ( options.mainWidth + ( 2 * options.leftRightWidth ) ) + 'px', 'height':  options.mainHeight + 'px'});
		$('.refsLeftCol').css({'width': options.leftRightWidth + 'px', 'height':  options.mainHeight + 'px'});
		$('.refsRightCol').css({'width': options.leftRightWidth + 'px', 'height':  options.mainHeight + 'px'});
		$('.refsMain').css({'width': options.mainWidth + 'px', 'height':  options.mainHeight + 'px'}).html( imgs );
		
		$('.refsLeftLink').css({'width': options.leftRightWidth + 'px', 'height':  options.mainHeight + 'px'});
		$('.refsRightLink').css({'width': options.leftRightWidth + 'px', 'height':  options.mainHeight + 'px'});
		
		
		
		var totalImgs	= $('.refsMain img').length;
		
		if ( totalImgs == 1 ) {
			$('.refsLeftLink').hide();
			$('.refsRightLink').hide();
		}
		
		
		
		$('a.refsLeftLink').click( function() {
			var prevId = currentShow == 0 ? ( totalImgs - 1 ) : currentShow - 1;
			currentShow = refshowimage(el, prevId, options, 'rightToLeft');
			return false;
		});

		$('a.refsRightLink').click( function() {
			var nextId = currentShow == ( totalImgs - 1 )? 0 : currentShow + 1;
			currentShow = refshowimage(el, nextId, options, 'leftToRight');
			return false;
		});
		
		if ( options.pagginationSHow ) {
		
			$('<p class="refsPagg"></p>').appendTo( el.parent());
			
			for( k = 0; k < totalImgs; k++ ) {
				$('<a href="#">' + ( k + 1 ) + '<\/a>').appendTo('.refsPagg');
			}

			$('.refsPagg a').click( function() {
				currentShow = refshowimage(el, ( $(this).html() - 1 ) , options, 'leftToRight');
				return false;	
			});
			
		}
		
		currentShow = refshowimage(el, currentShow, options, 'leftToRight');
		
		return;
	}; 
	
	
	function refshowimage(el, current, options, side ) { 
			
			$('.refsMain img').hide();
			var totalImgs	= $('.refsMain img').length;
					
			//show only current img and text
			
			//type of change
			
			switch( options.type) {
			
				case 'animate':
					if ( side == 'leftToRight' ) {
						$('.refsMain img:eq(' + current + ')').css({'left': options.mainWidth + 'px'}).show().animate({'left':  '0px'}, "fast");
					} else {
						$('.refsMain img:eq(' + current + ')').css({'left': '-' + options.mainWidth + 'px'}).show().animate({'left':  '0px'}, "fast").css({'left': '0px'});
					}
				break;
				
				case 'fade':
					$('.refsMain img:eq(' + current + ')').fadeIn();	
				break;
				
				default:	
					$('.refsMain img:eq(' + current + ')').show();
				break;
			}
			
			//set background image for left div
			var prevBack = current == 0 ? $('.refsMain img:eq(' + ( totalImgs - 1 ) + ')').attr('src') :  $('.refsMain img:eq(' + ( current - 1 ) + ')').attr('src');
			$('.refsLeftCol').css({ 'background' : 'url(' + prevBack + ') top right' });	

			//set background image for right div
			var nextBack = current == ( totalImgs - 1 ) ? $('.refsMain img:eq(0)').attr('src') :  $('.refsMain img:eq(' + ( current + 1 ) + ')').attr('src')		
			$('.refsRightCol').css({ 'background' : 'url(' + nextBack + ') top left' });
			
			//remove and set selected class of link for paggination
			//$('.img_paggin a').removeClass('selected');
			//$('.img_paggin a:eq(' + current + ')').addClass('selected');
			//currentImg = current;
			
			if ( options.pagginationSHow ) {
				$('.refsPagg a').removeClass('selected');
				$('.refsPagg a:eq(' + current + ')').addClass('selected');
			}
			
			return current;
	
	}
	
	 
})(jQuery);



