/*
 * Image preview script 
 * powered by jQuery (http://www.jquery.com)
 * 
 * written by Alen Grakalic (http://cssglobe.com)
 * 
 * for more info visit http://cssglobe.com/post/1695/easiest-tooltip-and-image-preview-using-jquery
 *
 */
 
this.imagePreview = function(containerId, customXOffset, customYOffset){	
	/* CONFIG */
	xOffset = -50;
	yOffset = -20;
	/*
	if (customXOffset != null)
	{
		xOffset = customXOffset;
	}
	if (customYOffset != null)
	{
		yOffset = customYOffset;
	}	*/
		// these 2 variable determine popup's distance from the cursor
		// you might want to adjust to get the right result
		
	/* END CONFIG */
	
	var selector = "a.preview";
	if ( containerId != null ) selector = "#" + containerId + " " + selector;
	jQuery(selector).hover(function(e){
		var xoff = 0;
		var yoff = 0;
		
		if ( jQuery(this).parents(".smallThumbnail").position().left > 150 && customXOffset==null ) xoff = -200;
		if ( jQuery(this).parents(".smallThumbnail").position().top > 450 && customYOffset==null ) yoff = -400;
		this.t = this.title;
		this.title = "";	
		var c = (this.t != "") ? "<br/>" + this.t : "";
		jQuery("body").append("<p id='preview'><img src='"+ this.href +"' alt='Image preview' />"+ '' +"</p>");								 
		jQuery("#preview")
			.css("top",(e.pageY - yOffset) + "px")
			.css("left",(e.pageX + xOffset + xoff) + "px")
			.fadeIn("fast");						
    },
	function(){
		this.title = this.t;	
		jQuery("#preview").remove();
    });	
	jQuery("a.preview").mousemove(function(e){
		var xoff = 0;
		var yoff = 0;
		if ( jQuery(this).parents(".smallThumbnail").position().left > 150  && customXOffset==null) xoff = -200;
		if ( jQuery(this).parents(".smallThumbnail").position().top > 450  && customYOffset==null) yoff = -400;
		jQuery("#preview")
			.css("top",(e.pageY - yOffset + yoff) + "px")
			.css("left",(e.pageX + xOffset + xoff) + "px");
	});			
};

function fadeImages( container, imagesArr)
{
	if (imagesArr.length < 2) return false;
	
	var currentImg = jQuery("img", container).attr("src");
	while(1)
	{
		var offset = Math.floor( Math.random() * imagesArr.length );
		if (currentImg != imagesArr[ offset ] ) break;
	}
	
	var fadeImg = "<img src='" + imagesArr[ offset ] + "' style='display:none;position: absolute;left:0;top:0'>";
	
	container.append(fadeImg);
	
	jQuery("img", container).fadeIn("4000");
	
	if (jQuery("img", container).length>3)	
		container.children( 'img:nth-child(2)' ).remove();
	
	
}
