/* png fix */
$(document).ready(function() {
	var i;
	for (i in document.images) {
		if (document.images[i].src) {
			var imgSrc = document.images[i].src;
			if (imgSrc.substr(imgSrc.length-4) === '.png' || imgSrc.substr(imgSrc.length-4) === '.PNG') {
				document.images[i].style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled='true',sizingMethod='crop',src='" + imgSrc + "')";
			}
		}
	}
});

(function($) {
		  
$.fn.fusionHover = function(o) {
    o = $.extend({		
		/* Picture */
		wrapClass: null,
		hoverClass: null,
		
		offset: 0,
		hoverImage: 'fhover/shadow.png',
		picEasing: null,
		picSpeed: 'fast',
		shadowSpeed: 'slow',
		
		/* Div block */
		divEasing: null,
		divEnterDirection: null,
		divLeaveDirection: null,
		divSpeed: 'slow',
		divStartOpacity: 0,
		divEndOpacity: 1,
		divTop: 0,
		divLeft: 0
    }, o || {});

    return this.each(function() {
		var t = $(this);
		var done = false;
		
		if ($.browser.mozilla) { t.ready(function() { loadImageStuff(t); });	}
		else if ($.browser.webkit) { t.load(function() {	loadImageStuff(t);	});	}
		else if ($.browser.opera) {	t.ready(function() { loadImageStuff(t); }); }
		else if ($.browser.msie) { t.ready(function() { loadImageStuff(t);	});	}
		else { t.ready(function() { loadImageStuff(t);	});	}
		
    });
	
	function loadImageStuff(t) {
		//t.wrap('<div class="fb_wrap">').css('position', 'relative'); // .wrap didn't work on IE9. The next line is the fix for this. Only left here for historical review.
		t.css('position', 'relative').after('<div class="fb_wrap">').appendTo(t.next('.fb_wrap'));
		
		var w = t.parents('.fb_wrap');
		w.css({height: t.css('height'), width: t.css('width')}).addClass(o.wrapClass);
		
		w.append('<img class="fb_shadow" src="'+o.hoverImage+'">');
		w.find('.fb_shadow').ready(function() {
			$(this).css({'-ms-filter': "progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled='true',sizingMethod='crop',src='" + o.hoverImage + "')"});
		});
		
		/* If there is a div block waiting to be executed, we do so */
		if (w.next('.fb_divs_blocks') != null) { 
			var r = w.next('.fb_divs_blocks'); 
			/* Let's set the coords depending on the entrance direction */
			var startTop = 0; var startLeft = 0;
			if (o.divEnterDirection != null) {
				switch(o.divEnterDirection) {
					case 'bottom':
						var startTop = (w.height()+50); var startLeft = 0; break;
					case  'top':
						var startTop = (-1)*(w.height()+50); var startLeft = 0; break;
					case 'left':
						var startTop = 0; var startLeft = (-1)*(w.width()+50); break;
					case 'right':
						var startTop = 0; var startLeft = (w.width()+50); break;
					case 'top-right':
						var startTop = (-1)*(w.height()+50); var startLeft = (w.width()+50); break;
					case 'top-left':
						var startTop = (-1)*(w.height()+50); var startLeft = (-1)*(w.width()+50); break;
					case 'bottom-right':
						var startTop = (w.height()+50); var startLeft = (w.width()+50); break;
					case 'bottom-left':
						var startTop = (w.height()+50); var startLeft = (-1)*(w.width()+50); break;
				}
			}
			/* And now lets append this new div block to the picture, and set its position */
			r.appendTo(w).css({top: startTop+'px', left: startLeft+'px', position: 'absolute', opacity: o.divStartOpacity, 'filter': 'alpha(opacity='+(o.divStartOpacity*10)+')', '-ms-filter': 'Alpha(opacity='+(o.divStartOpacity*10)+')'}).show(); 
			
			/* Now let's set the leaveDirection coords */
			var leaveTop = ((o.divTop) ? o.divTop : 0); var leaveLeft = ((o.divLeft) ? o.divLeft : 0);
			if (o.divLeaveDirection != null) {
				switch(o.divLeaveDirection) {
					case 'bottom':
						var leaveTop = (w.height()+50); var leaveLeft = 0; break;
					case  'top':
						var leaveTop = (-1)*(w.height()+50); var leaveLeft = 0; break;
					case 'left':
						var leaveTop = 0; var leaveLeft = (-1)*(w.width()+50); break;
					case 'right':
						var leaveTop = 0; var leaveLeft = (w.width()+50); break;
					case 'top-right':
						var leaveTop = (-1)*(w.height()+50); var leaveLeft = (w.width()+50); break;
					case 'top-left':
						var leaveTop = (-1)*(w.height()+50); var leaveLeft = (-1)*(w.width()+50); break;
					case 'bottom-right':
						var leaveTop = (w.height()+50); var leaveLeft = (w.width()+50); break;
					case 'bottom-left':
						var leaveTop = (w.height()+50); var leaveLeft = (-1)*(w.width()+50); break;
				}
			}
		}
		
		/* Let's calculate the width offset depending on the height one... this is so we keep proportions right */
		var offset_w = t.width() * (o.offset / t.height());
		var vhe = (parseInt(w.css('height').replace('px', ''), 10) + (o.offset*2));
		var vwi = (parseInt(w.css('width').replace('px', ''), 10) + (offset_w*2));
		t.css({width: vwi , height: vhe , 'left': '-'+offset_w+'px' , 'top': '-'+o.offset+'px' });
		
		/* Now let's add the 'shadow' image. This one is the image that loads when the mouse goes over the picture */
		var s = w.find('.fb_shadow');	
		s.css({width: w.css('width'), height: w.css('height')});
		
		
		w.mouseenter (function() {
			/* Picture animation */
			t.stop().animate({height: w.css('height'), width: w.css('width'), 'top': '0px', 'left': '0px'}, o.picSpeed, o.picEasing);
			/* Add hoverClass to wrapper */
			w.addClass(o.hoverClass);
			/* Shadow animation */
			if ($.browser.msie ) { if ($.browser.version >= 9) { s.show().stop().animate({opacity: 1, 'filter': 'alpha(opacity=100)', '-ms-filter': 'Alpha(opacity=100)'}, o.shadowSpeed, function() {}); } }
			else { s.show().stop().animate({opacity: 1, 'filter': 'alpha(opacity=100)', '-ms-filter': 'Alpha(opacity=100)'}, o.shadowSpeed, function() {}); }
			/* Div animation */
			if (r) { r.stop().animate({opacity: o.divEndOpacity, 'filter': 'alpha(opacity='+(o.divEndOpacity*99)+')', '-ms-filter': 'Alpha(opacity='+(o.divEndOpacity*99)+')', top: o.divTop+'px', left: o.divLeft+'px'}, o.divSpeed, o.divEasing); }
		}).mouseleave(function() {
			/* Picture animation */
			t.stop().animate({height: vhe, width: vwi, 'top': '-'+o.offset+'px', 'left': '-'+offset_w+'px'}, o.picSpeed, o.picEasing);
			/* Remove hoverClass from wrapper */
			w.removeClass(o.hoverClass);
			/* Shadow animation */
			if ($.browser.msie ) { if ($.browser.version >= 9) { s.stop().animate({opacity: 0, 'filter': 'alpha(opacity=0)', '-ms-filter': 'Alpha(opacity=0)'}, o.shadowSpeed); } }
			else { s.stop().animate({opacity: 0, 'filter': 'alpha(opacity=0)', '-ms-filter': 'Alpha(opacity=0)'}, o.shadowSpeed); }
			/* Div animation */
			if (r) { r.stop().animate({opacity: o.divStartOpacity, 'filter': 'alpha(opacity='+(o.divStartOpacity*10)+')', '-ms-filter': 'Alpha(opacity='+(o.divStartOpacity*10)+')', top: leaveTop+'px', left: leaveLeft+'px'}, o.divSpeed, o.divEasing, function() { r.css({top: startTop+'px', left: startLeft+'px', position: 'absolute', opacity: o.divStartOpacity, 'filter': 'alpha(opacity='+(o.divStartOpacity*10)+')', '-ms-filter': 'Alpha(opacity='+(o.divStartOpacity*10)+')'}); }); }
		});
	}
};

})(jQuery);
