/*
 * jQuery Hover/Swap - A simple, complete, and unobtrusive roll-over solution.
 *
 * Copyright (c) 2010 David Belais
 *
 * Dual licensed under the MIT and GPL licenses:
 *   http://www.opensource.org/licenses/mit-license.php
 *   http://www.gnu.org/licenses/gpl.html
 *
 * Version: 0.1
 * 
 */
(function($){
	
	$.fn.hoverswap=function(options){
		
		var settings = $.extend({
			align:null,	// Alignment of the roll-over image in relation to the original. Alignment can be any CSS alignement value e.g. center middle" , "50% 50%", etc.. When alignment is set rollovers are triggered by the hover event of the images *parent* node.
			srcAttr:"longdesc", // The attribute holding the roll-over src URL. Unless you are using it for something else, use 'longdesc' in order to author valid XHTML.
			blankgif:'http://upload.wikimedia.org/wikipedia/commons/c/ce/Transparent.gif', //the location of the transparent gif used to fix png's in IE6.
			url:null
		}, options);
		
		var selector=this.selector;
		var context=this.context;
		var ie6=($.browser.msie && $.browser.version < 7);
		
		if(settings.align!==null){
			var aa=[];
			if(typeof(settings.align)=='string'){
				aa=$.grep(
					$.trim(settings.align).split(' '),
					function(e,i){return(e.length>0);}
				);
			}					

			if(typeof(settings.align)=='array'){aa=settings.align;}
			settings.align=[0.5,0.5];
			$.each(aa,function(i){
				if(this=='left'){settings.align[0]=0.0;}
				else if(this=='right'){settings.align[0]=1.0;}
				else if(this=='center'){settings.align[0]=0.5;}
				else if(this=='middle'){settings.align[1]=0.5;}
				else if(this=='top'){settings.align[1]=0.0;}
				else if(this=='bottom'){settings.align[1]=1.0;}
				else{settings.align[i]=Number((/\d*/).exec(this));}
				if(this.indexOf('%')>-1){settings.align[i]*=100.0;}
			});	
		}	
				
		this.each(function(){
			
			var img=this;
			
			if(!$(this).data().hoverswap){
				
				$(this).data('hoverswap',{
					orig:{
						css:{
							position:$(this).css('position')
						}
					},
					align:null
				});
				
			}
			
			if(!$(this).data().hoverswap.orig.src){
				
				var h;
				
				if(ie6){
					
					var f=/^progid\:DXImageTransform\.Microsoft\.AlphaImageLoader\(.*?src\=[\"\']?(.*\.png)[\"\']?.*?\)/.exec(this.runtimeStyle.filter);
					
					if(f && f.length>1){
						h=$(this).data('hoverswap');
						h.orig.src=f[1];
						$(this).data('hoverswap',h);
					}else{
						h=$(img).data('hoverswap');
						h.orig.src=$(this).attr('src');
						$(img).data('hoverswap',h);
						if(/\.png\b/.test($(this).attr('src'))){
							$(this).attr('src',settings.blankgif);
							this.runtimeStyle.filter = 'progid:DXImageTransform.Microsoft.AlphaImageLoader' + '(src="' + $(this).data().hoverswap.orig.src + '",sizingMethod="image");';
						}
					}
				}else{
					
					h=$(this).data('hoverswap');
					h.orig.src=$(this).attr('src');
					$(this).data('hoverswap',h);
				}
				
			}
			
			var parent=$(this).parent()[0];
			
			var image=new Image();
			if(settings.url){
				if(typeof(settings.url)=='string'){
					image.src=settings.url;
				}else if(typeof(settings.url)=='array'){
					image.src=settings.url.shift();
				}else if(typeof(settings.url)=='object'){
					image.src=settings.url[$(this).attr('src')];
				}
			}else{
				image.src=$(this).attr(settings.srcAttr);
			}
			
			$(image).load(function(){
								
				var hoverswap=$(img).data('hoverswap');
				hoverswap.orig.offset=$(img).offset();
				hoverswap.orig.position=$(img).position();
				hoverswap.orig.width=$(img).width();
				hoverswap.orig.height=$(img).height();
				hoverswap.hover={
					src:$(this).attr('src'),
					width:this.width,
					height:this.height
				};
				
				if(settings.align && img.nodeName.toLowerCase()!='input'){
					
					hoverswap.align=settings.align;

					var shift={
						x:settings.align?(hoverswap.orig.width-this.width)*(hoverswap.align[0]):0,
						y:settings.align?(hoverswap.orig.height-this.height)*(hoverswap.align[1]):0
					};
					
					$(this).attr("alt",'').attr("width",this.width).attr("height",this.height).css({
						width:this.width,
						height:this.height,
						position:'absolute',
						zIndex:99999999999,
						visibility:'hidden'
					}).addClass('temp-rollover').appendTo(parent);
					
					if(ie6 && /\.png\b/.test($(this).attr('src'))){	
						this.runtimeStyle.filter = 'progid:DXImageTransform.Microsoft.AlphaImageLoader' + '(src="' + $(this).attr('src') + '",sizingMethod="image");';
						$(this).attr('src',settings.blankgif);
					}
					
					$(parent).hover(function(){
						var h=$(img).data('hoverswap');
						h.orig.offset=$(img).offset();
						h.orig.position=$(img).position();
						h.hover.offset={
							top:h.orig.offset.top+shift.y,
							left:h.orig.offset.left+shift.x
						};
						h.hover.position={
							top:h.orig.position.top+shift.y,
							left:h.orig.position.left+shift.x
						};
						$(image).css({
							left:h.hover.position.left,
							top:h.hover.position.top,
							width:h.hover.width,
							height:h.hover.height,
							visibility:"visible"
						}).offset(h.hover.offset);
						$(img).css({visibility:"hidden"});
						$(img).data('hoverswap',hoverswap);
					},function(){
						$(img).css({visibility:"visible"});
						$(image).css({visibility:"hidden"});
						$(selector,context).css({visibility:"visible"});
					});
				}

				$(img).data('hoverswap',hoverswap);
			});
			
			if(image.complete){$(image).trigger('load');}
			
		}).filter(function(){return !$(this).data('hoverswap').align;}).hover(function(){
			if(ie6 && /\.png$/.test($(this).data('hoverswap').hover.src)){
				this.runtimeStyle.filter='progid:DXImageTransform.Microsoft.AlphaImageLoader' + '(src="' + $(this).data('hoverswap').hover.src + '",sizingMethod="image");';
				$(this).attr('src',settings.blankgif);
			}else{
				$(this).attr('src', $(this).data('hoverswap').hover.src);
			}
		},function(){
			if(ie6 && /\.png$/.test($(this).data().hoverswap.orig.src)){
				this.runtimeStyle.filter='progid:DXImageTransform.Microsoft.AlphaImageLoader' + '(src="' + $(this).data().hoverswap.orig.src + '",sizingMethod="image");';
				$(this).attr('src',settings.blankgif);
			}else{
				$(this).attr('src', $(this).data().hoverswap.orig.src);
			}
		});
		
		return this;
	};
})(jQuery);
