/*
 * jQuery beforeafter plugin
 * @author admin@catchmyfame.com - http://www.catchmyfame.com
 * @version 1.1
 * @date January 12, 2010
 * @category jQuery plugin
 * @copyright (c) 2009 admin@catchmyfame.com (www.catchmyfame.com)
 * @license CC Attribution-No Derivative Works 3.0 - http://creativecommons.org/licenses/by-nd/3.0/
 */
// before image : $j('div:eq(2)', obj)
// after image  : $j('div:eq(3)', obj)
var $j = jQuery.noConflict();
(function($){
	$j.fn.extend({ 
		beforeAfter: function(options)
		{
			var defaults = 
			{
				animateIntro : false,
				introDelay : 1000,
				introDuration : 1000,
				showFullLinks : false,
				imagePath : 'modules/mod_image_BeforeAfter/js/'
			};
		var options = $j.extend(defaults, options);

		var randID =  Math.round(Math.random()*100000000);
	
    		return this.each(function() {
			var o=options;
			var obj = $j(this);

			var imgWidth = $j('img:first', obj).width();
			var imgHeight = $j('img:first', obj).height();
			
			$j(obj)
			.width(imgWidth)
			.height(imgHeight)
			.css({'overflow':'hidden','position':'relative','padding':'0'});
			
			// Preload images and assign them IDs
			var image1 = $j('<img />').attr('src', $j('img:first', obj).attr('src'));
			var image2 = $j('<img />').attr('src', $j('img:last', obj).attr('src'));
			$j('img:first', obj).attr('id','beforeimage'+randID);
			$j('img:last', obj).attr('id','afterimage'+randID);
			
			$j('div', obj).css('float','left'); // Float all divs within the container left
			
			// Create an inner div wrapper (dragwrapper) to hold the images.
			$j(obj).prepend('<div id="dragwrapper'+randID+'"><div id="drag'+randID+'"><img width="8" height="56" alt="handle" src="'+o.imagePath+'handle-2hmbs.png" title="Bewegen Sie den Cursor &uuml;ber das Bild!" id="handle'+randID+'" /></div></div>'); // Create drag handle
			$j('#dragwrapper'+randID).css({'position':'absolute','padding':'0','left':(imgWidth/2)-($j('#handle'+randID).width()/2)+'px','z-index':'20'}).width($j('#handle'+randID).width()).height(imgHeight);
			$j('#dragwrapper'+randID).css({'opacity':.25}); // Sets the dragwrapper and contents to .25 opacity
				
			$j('div:eq(2)', obj).height(imgHeight).width(imgWidth/2).css({'position':'absolute','overflow':'hidden','left':'0px','z-index':'10', 'padding-left':'0px'}); // Set CSS properties of the before image div
			$j('div:eq(3)', obj).height(imgHeight).width(imgWidth).css({'position':'absolute','overflow':'hidden','right':'0px', 'padding-left':'0px'});	// Set CSS properties of the after image div
			$j('#drag'+randID).width(2).height(imgHeight).css({'background':'#888','position':'absolute','left':'3px', 'padding-left':'0px'});	// Set drag handle CSS properties
			$j('#beforeimage'+randID).css({'position':'absolute','top':'0px','left':'0px'});
			$j('#afterimage'+randID).css({'position':'absolute','top':'0px','right':'0px'});
			$j('#handle'+randID).css({'position':'relative','cursor':'pointer','top':(imgHeight/2)-($j('#handle'+randID).height()/2)+'px','left':'-3px'})
			
			$j(obj).append('<img src="'+o.imagePath+'lt-small.PNG" width="7" height="15" id="lt-arrow'+randID+'"><img src="'+o.imagePath+'rt-small.PNG" width="7" height="15" id="rt-arrow'+randID+'">');

			if(o.showFullLinks)
			{	
				$j(obj).after('<div class="balinks" id="links'+randID+'" style="position:relative"><span class="bflinks"><a id="showleft'+randID+'" href="javascript:void(0)">Vorher</a></span><span class="bflinks"><a id="showright'+randID+'" href="javascript:void(0)">Nachher</a></span></div>');
				$j('#links'+randID).width(imgWidth);
				$j('#showleft'+randID).css({'position':'relative','float':'left'}).click(function(){
					$j('div:eq(2)', obj).animate({width:imgWidth},200);
					$j('#dragwrapper'+randID).animate({left:imgWidth-$j('#dragwrapper'+randID).width()+'px'},200);
				});
				$j('#showright'+randID).css({'position':'absolute','right':'0px'}).click(function(){
					$j('div:eq(2)', obj).animate({width:0},200);
					$j('#dragwrapper'+randID).animate({left:'0px'},200);
				});
			}

			var barOffset = $j('#dragwrapper'+randID).offset(); // The coordinates of the dragwrapper div
			var startBarOffset = barOffset.left; // The left coordinate of the dragwrapper div
			var originalLeftWidth = $j('div:eq(2)', obj).width();
			var originalRightWidth = $j('div:eq(3)', obj).width();

			$j('#dragwrapper'+randID).draggable({handle:$j('#handle'+randID),containment:obj,axis:'x',drag: function(e, ui){
				var offset = $j(this).offset();
				var barPosition = offset.left - startBarOffset;
				$j('div:eq(2)', obj).width(originalLeftWidth + barPosition);
				$j('#lt-arrow'+randID).stop().animate({opacity:0},50);
				$j('#rt-arrow'+randID).stop().animate({opacity:0},50);
				}
			});

			if(o.animateIntro)
			{
				$j('div:eq(2)', obj).width(imgWidth);
				$j('#dragwrapper'+randID).css('left',imgWidth-($j('#dragwrapper'+randID).width()/2)+'px');
				setTimeout(function(){
					$j('#dragwrapper'+randID).css({'opacity':1}).animate({'left':(imgWidth/2)-($j('#dragwrapper'+randID).width()/2)+'px'},o.introDuration,function(){$j('#dragwrapper'+randID).animate({'opacity':.25},1000)});
					// The callback function at the end of the last line is there because Chrome seems to forget that the divs have overlay  hidden applied earlier
					$j('div:eq(2)', obj).width(imgWidth).animate({'width':imgWidth/2+'px'},o.introDuration,function(){$j('div:eq(2)', obj).css('overflow','hidden');clickit();});
				},o.introDelay);
			}
			else
			{
				clickit();
			}

			function clickit()
			{
				$j(obj).hover(function(){
						$j('#lt-arrow'+randID).stop().css({'z-index':'20','position':'absolute','top':imgHeight/2-$j('#lt-arrow'+randID).height()/2+'px','left':parseInt($j('#dragwrapper'+randID).css('left'))-10+'px'}).animate({opacity:1,left:parseInt($j('#lt-arrow'+randID).css('left'))-6+'px'},500);
						$j('#rt-arrow'+randID).stop().css({'position':'absolute','top':imgHeight/2-$j('#lt-arrow'+randID).height()/2+'px','left':parseInt($j('#dragwrapper'+randID).css('left'))+10+'px'}).animate({opacity:1,left:parseInt($j('#rt-arrow'+randID).css('left'))+6+'px'},500);
						$j('#dragwrapper'+randID).animate({'opacity':1},500);
					},function(){
						$j('#lt-arrow'+randID).animate({opacity:0,left:parseInt($j('#lt-arrow'+randID).css('left'))-6+'px'},500);
						$j('#rt-arrow'+randID).animate({opacity:0,left:parseInt($j('#rt-arrow'+randID).css('left'))+6+'px'},500);
						$j('#dragwrapper'+randID).animate({'opacity':.25},500);
					}
				);

				// When clicking in the container, move the bar and imageholder divs
				$j(obj).click(function(e){
					
					var clickX = e.pageX - this.offsetLeft;
					var img2Width = imgWidth-clickX;
					$j('#dragwrapper'+randID).stop().animate({'left':clickX-($j('#dragwrapper'+randID).width()/2)+'px'},600);
					$j('div:eq(2)', obj).stop().animate({'width':clickX+'px'},600,function(){$j('div:eq(2)', obj).css('overflow','hidden');}); // webkit fix for forgotten overflow
					$j('#lt-arrow'+randID).stop().animate({opacity:0},50);
					$j('#rt-arrow'+randID).stop().animate({opacity:0},50);
				});
				$j(obj).one('mousemove', function(){$j('#dragwrapper'+randID).stop().animate({'opacity':1},500);}); // If the mouse is over the container and we animate the intro, we run this to change the opacity since the hover event doesnt get triggered yet
			}
  		});
    	}
	});
})(jQuery);
