jQuery.IntaminImageHover = {

    /** Actually build the structure */
    build: function(div, image, swf, video, width, height, alignment, videowidth, videoheight, thumbwidth, thumbheight)  {
        // Set up the defaults if any

        this.each(function() {
            jQuery.IntaminImageHover.makeHover(this, div, image, swf, video, width, height, alignment, videowidth, videoheight, thumbwidth, thumbheight);
        });

        return this;
    },

    /** Setting the Offset and the State of the Objects */
    makeHover: function(source, div, image, swf, video, width, height, alignment, videowidth, videoheight, thumbwidth, thumbheight) {
		var hovered = false;
		
		var BaseWidth = jQuery(image).width();
		var BaseHeight = jQuery(image).height();
		var BasePosX = jQuery(image).css("left");
		var BasePosY = jQuery(image).css("top");
		
		jQuery(div).mousemove(function(e){
			var Hover = false;
			var BildWidth = jQuery(source).width();
			var BildHeight = jQuery(source).height();
			var BildX = jQuery(source).offset().left;
			var BildY = jQuery(source).offset().top;
			
			if (e.pageX > BildX && e.pageX < (BildX + BildWidth) && e.pageY > BildY && e.pageY < (BildY + BildHeight))
				Hover = true;
			if (Hover){
				if (!hovered && !jQuery(source).hasClass("deactivated")){
					jQuery.IntaminImageHover.makeGreater(image, swf, video, width, height, alignment, videowidth, videoheight, thumbwidth, thumbheight);
					hovered = true;
				}
			}else{
				if (hovered && !jQuery(source).hasClass("deactivated")){
					jQuery.IntaminImageHover.makeSmaller(image, BaseWidth, BaseHeight, BasePosX, BasePosY);
					hovered = false;
				}
			}
		});
	},
	
	makeGreater: function(image, swf, video, iwidth, iheight, alignment, videowidth, videoheight, thumbwidth, thumbheight) {
		
		var playbutton = jQuery(".BtnMoviePlay", jQuery(image));
		var closebutton = jQuery(".BtnMovieClose", jQuery(image));
		var description = jQuery(".DescriptionSpan", jQuery(image));		
		
		var imageWidth = jQuery(image).width();//thumbwidth;
		var imageHeight = jQuery(image).height();//thumbheight;//
		var imagePosX = parseInt(jQuery(image).css("left"));
		var imagePosY = parseInt(jQuery(image).css("top"));
		var Attachment1Height = parseInt(jQuery(playbutton).css("height"));
		var Attachment1Width = parseInt(jQuery(playbutton).css("width"));
		var Attachment1X = 0;
		var Attachment1Y = 0;
		jQuery(playbutton).hide();
		jQuery(closebutton).hide();
		jQuery(description).hide();
		
		
		var newPosX = 0;
		var newPosY = 0;
		
		switch (alignment) {
			case "topleft":
				newPosX = imagePosX;
				newPosY = imagePosY;
				Attachment1X = ((imageHeight / 2) - (Attachment1Height / 2));
				Attachment1Y = ((imageWidth / 2) - (Attachment1Width / 2));
			break;
			case "topcenter":
				newPosX = imagePosX + imageWidth/2 - iwidth/2;
				newPosY = imagePosY;
				Attachment1X = ((imageHeight / 2) - (Attachment1Height / 2));
				Attachment1Y = ((imageWidth / 2) - (Attachment1Width / 2) + iwidth/2 - imageWidth/2);
			break;
			case "topright":
				newPosX = imagePosX + imageWidth - iwidth;
				newPosY = imagePosY;
				Attachment1X = ((imageHeight / 2) - (Attachment1Height / 2));
				Attachment1Y = ((imageWidth / 2) - (Attachment1Width / 2) + iwidth - imageWidth);
			break;
			case "middleleft":
				newPosX = imagePosX;
				newPosY = imagePosY + imageHeight/2 - iheight/2;
				Attachment1X = ((imageHeight / 2) - (Attachment1Height / 2) + iheight/2 - imageHeight/2);
				Attachment1Y = ((imageWidth / 2) - (Attachment1Width / 2));
			break;
			case "middlecenter":
				newPosX = imagePosX + imageWidth/2 - iwidth/2;
				newPosY = imagePosY + imageHeight/2 - iheight/2;
				Attachment1X = ((imageHeight / 2) - (Attachment1Height / 2) + iheight/2 - imageHeight/2);
				Attachment1Y = ((imageWidth / 2) - (Attachment1Width / 2) + iwidth/2 - imageWidth/2);
			break;
			case "middleright":
				newPosX = imagePosX + imageWidth - iwidth;
				newPosY = imagePosY + imageHeight/2 - iheight/2;
				Attachment1X = ((imageHeight / 2) - (Attachment1Height / 2) + iheight/2 - imageHeight/2);
				Attachment1Y = ((imageWidth / 2) - (Attachment1Width / 2) + iwidth - imageWidth);
			break;
			case "bottomleft":
				newPosX = imagePosX;
				newPosY = imagePosY + imageHeight - iheight;
				Attachment1X = ((imageHeight / 2) - (Attachment1Height / 2) + iheight - imageHeight);
				Attachment1Y = ((imageWidth / 2) - (Attachment1Width / 2));
			break;
			case "bottomcenter":
				newPosX = imagePosX + imageWidth/2 - iwidth/2;
				newPosY = imagePosY + imageHeight - iheight;
				Attachment1X = ((imageHeight / 2) - (Attachment1Height / 2) + iheight - imageHeight);
				Attachment1Y = ((imageWidth / 2) - (Attachment1Width / 2) + iwidth/2 - imageWidth/2);
			break;
			case "bottomright":
				newPosX = imagePosX + imageWidth - iwidth;
				newPosY = imagePosY + imageHeight - iheight;
				Attachment1X = ((imageHeight / 2) - (Attachment1Height / 2) + iheight - imageHeight);
				Attachment1Y = ((imageWidth / 2) - (Attachment1Width / 2) + iwidth - imageWidth);
			break;
		}
		
		jQuery(image).show();
		jQuery(image).css("opacity", "0");
		jQuery(image).animate({
						 opacity: 1,
						 width: iwidth,
						 height: iheight,
						 left: newPosX,
						 top: newPosY
						 }, 200, function(){
							
							jQuery(playbutton).css("top", Attachment1X).css("left", Attachment1Y);
							jQuery(playbutton).css("display", "block");
							jQuery(description).fadeIn("fast");
						 });
		
		// Define Buttons und Video
		jQuery(playbutton).unbind("click");
		jQuery(playbutton).bind("click", function(){ 
			// deactivate others
			jQuery(".thumbimage").addClass("deactivated");
			jQuery(".ObjectContainer", jQuery(image)).show();
			jQuery(".ObjectContainer", jQuery(image)).flash({
				src : swf,
				height : videoheight,
				width : videowidth,
				wmode: "opaque",
				flashvars: {autoStart : 1, file : video}
			});
			jQuery("span", jQuery(image)).hide(); 
			jQuery(playbutton).hide();
			jQuery(closebutton).show();
		});

		jQuery(closebutton).unbind("click");
		jQuery(closebutton).bind("click", function(iposX, iposY){
			jQuery(".thumbimage").removeClass("deactivated");
			jQuery(image).css({ 
							 opacity: 0,
						 width: iwidth,
						 height: iheight,
						 left: iposX,
						 top: iposY
						 });
			jQuery(".ObjectContainer", jQuery(image)).html("");
			jQuery(this).hide();
			jQuery(image).hide();
		});
		
	},
	
	makeSmaller: function(image, iwidth, iheight, iposX, iposY) {
		/*jQuery(image).animate({
						 opacity: 0,
						 width: iwidth,
						 height: iheight,
						 left: iposX,
						 top: iposY
						 }, 0, function(){ //200
							jQuery(image).hide();
						 });*/
		jQuery(image).stop();
		jQuery(image).css({
			opacity: 0,
			width: iwidth,
			height: iheight,
			left: iposX,
			top: iposY
		});
		jQuery(image).hide();
		
		jQuery(".ObjectContainer", jQuery(image)).hide().html("");
			jQuery("span", jQuery(image)).show();
			// activate others
			$j(".thumbimage").removeClass("deactivated");
	}
	
}

jQuery.fn.extend(
	{
		IntaminImageHover : jQuery.IntaminImageHover.build
	}
);
