/*
 * 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
 *
 */
 
(function ($) {
	$.event.special.load = {
		add: function (hollaback) {
			if ( this.nodeType === 1 && this.tagName.toLowerCase() === 'img' && this.src !== '' ) {
				// Image is already complete, fire the hollaback (fixes browser issues were cached
				// images isn't triggering the load event)
				if ( this.complete || this.readyState === 4 ) {
					hollaback.handler.apply(this);
				}

				// Check if data URI images is supported, fire 'error' event if not
				else if ( this.readyState === 'uninitialized' && this.src.indexOf('data:') === 0 ) {
					$(this).trigger('error');
				}
				
				else {
					$(this).bind('load', hollaback.handler);
				}
			}
		}
	};
}(jQuery));

this.imagePreview = function(){	

	/* CONFIG */
		
		xOffset = 10;
		yOffset = 30;
		
		// these 2 variable determine popup's distance from the cursor
		// you might want to adjust to get the right result
		
	/* END CONFIG */
	jQuery("a.preview img").hover(function(e){
		var title_t=jQuery(this).parent().attr('title');		
		//jQuery(this).parent().attr('title','');		
		var c = (title_t != "") ? "<br/>" + title_t : "";				
		jQuery("body").append("<div id='preview' style='color: #215B9B;font-weight:bold;'><img src='"+ jQuery(this).parent().attr('href')+"' alt='Image preview' />"+ c +"</div>");
		
		var c_h=200;
		
		var cl_h=jQuery(window).height()/2-c_h+jQuery(window).scrollTop();		
		jQuery("#preview")
			.css("position","absolute")
			.css("top",cl_h+"px")
			.css("left","52%")			
			.fadeIn(765);						
    },
	function(){
		var title_t=jQuery(this).parent().attr('title');	
		jQuery(this).parent().attr('title',title_t);			
		jQuery("#preview").fadeOut('fast').remove();		
    });	

};


// starting the script on page load
jQuery(document).ready(function(){
	imagePreview();
});








this.imagePreview2 = function(){	

	/* CONFIG */
		
		xOffset = 10;
		yOffset = 30;
		
		// these 2 variable determine popup's distance from the cursor
		// you might want to adjust to get the right result
		
	/* END CONFIG */
	jQuery("a.preview2 img").hover(function(e){
		var title_t=jQuery(this).parent().attr('title');		
		//jQuery(this).parent().attr('title','');		
		var c = (title_t != "") ? "<br/>" + title_t : "";				
		jQuery("body").append("<div id='preview' style='color: #215B9B;font-weight:bold;'><img src='"+ jQuery(this).parent().attr('href')+"' alt='Image preview' />"+ c +"</div>");
		
		var c_h=200;
		
		var cl_h=jQuery(window).height()/2-c_h+jQuery(window).scrollTop();		
		jQuery("#preview")
			.css("position","absolute")
			.css("top",cl_h+"px")
			.css("left","30%")			
			.fadeIn(765);						
    },
	function(){
		var title_t=jQuery(this).parent().attr('title');	
		jQuery(this).parent().attr('title',title_t);			
		jQuery("#preview").fadeOut('fast').remove();		
    });	
	/*jQuery("a.preview").mousemove(function(e){
		jQuery("#preview")
			.css("top",(e.pageY - xOffset) + "px")
			.css("left",(e.pageX + yOffset) + "px");
	});		*/	
};


// starting the script on page load
jQuery(document).ready(function(){
	imagePreview2();
});










this.imagePreview3 = function(){	

	/* CONFIG */
		
		xOffset = 10;
		yOffset = 30;
		
		// these 2 variable determine popup's distance from the cursor
		// you might want to adjust to get the right result
		
	/* END CONFIG */
	jQuery("a.preview3 img").hover(function(e){
		var title_t=jQuery(this).parent().attr('title');		
		//jQuery(this).parent().attr('title','');		
		var c = (title_t != "") ? "<br/>" + title_t : "";				
		jQuery("body").append("<div id='preview3' style='color: #215B9B;font-weight:bold;z-index:65535;'><img src='"+ jQuery(this).parent().attr('rel')+"' alt='Image preview' />"+ c +"</div>");
		
		var c_h=200;
		
		var cl_h=jQuery(window).height()/2-c_h+jQuery(window).scrollTop();		
		jQuery("#preview3")
			.css("position","absolute")
			.css("top",cl_h+"px")
			.css("left","10%")			
			.fadeIn(765);						
    },
	function(){
		var title_t=jQuery(this).parent().attr('title');	
		jQuery(this).parent().attr('title',title_t);			
		jQuery("#preview3").fadeOut('fast').remove();		
    });	

};


// starting the script on page load
jQuery(document).ready(function(){
	imagePreview3();
});




this.imagePreview4 = function(){	

	jQuery("a.preview4").hover(function(e){
		var mini = jQuery(this);
		var magic_scroll = jQuery(window).scrollTop();
		var title_t=jQuery(this).attr('title');		

		var c = (title_t != "") ? "<br/>" + title_t : "";				
		
		
		var src = jQuery(this).attr('rel');
		
		var ww = jQuery(window).width();
		var wh = jQuery(window).height();
		
		var mw = mini.width();
		var mh = mini.height();
		var ofset_w = $(mini).offset().left;
		var ofset_h = $(mini).offset().top;
		
		var x_x = ofset_w + (mw/2);
		var y_y = ofset_h - magic_scroll + (mh/2);
		
		var c_x = (ww/2);
		var c_y = (wh/2);
		
	
		
		jQuery("#preview").html("<img id='prievv' src='"+src+"' alt='" + c + "' />" + c)
		.css("width","auto")
		.css("height","auto");
		
		
		
		
						
		
				
		
		var himg = jQuery("#preview");
		var hwidth = 512;
		var hheight = 462;
		
		
		

		/*alert("Tooltip width = "+hwidth+"\r\nTooltip height = "+hheight+"\r\n\r\nWindow width = "+ww+"\r\nWindow height = "+wh+"\r\n\r\nMini img width = "+mw+"\r\nMini img height = "+mh+"\r\n\r\nScroll from top = "+magic_scroll+"\r\nLeft = "+ofset_w+"\r\nTop = "+ofset_h);*/

		
		if (((hwidth+ofset_w+mw+15)<ww)&&((ofset_h-magic_scroll-15)>hheight)) {
				
			var c_top = (ofset_h-hheight-15);
			var c_left = (ofset_w+mw+15);
				
		} else if (((hwidth+ofset_w+mw+15)<ww) && ((wh-(ofset_h-magic_scroll+mh+15))>hheight)) {
					
			var c_top = (ofset_h+mh+15);
			var c_left = (ofset_w+mw+15);		
				
		} else if (((ofset_w+15)>hwidth) && ((ofset_h-magic_scroll-15)>hheight)) {
		
			var c_top = (ofset_h-hheight-15);
			var c_left = (ofset_w-hwidth-15);					
		
		} else if (((ofset_w+15)>hwidth) && ((magic_scroll+wh)>(15+ofset_h+mh+hheight))) {
		
			var c_top = (ofset_h+mh+15);
			var c_left = (ofset_w-hwidth-15);			
				
		} else {
		
			
				if (( x_x < c_x) && (y_y < c_y)) {
		
					
					
					var c_top = (ofset_h+mh+15);
					var c_left = (ofset_w+mw+15);		
					
		
				} else if (( x_x > c_x) && (y_y > c_y)) {
					
					
					var c_top = (ofset_h-hheight-15);
					var c_left = (ofset_w-hwidth-15);		
					
				} else if (( x_x > c_x) && (y_y < c_y)) {
				
					
					
					var c_top = (ofset_h+mh+15);
					var c_left = (ofset_w-hwidth-15);		
						
				
				} else {
				
					
					
					var c_top = (ofset_h-hheight-15);
					var c_left = (ofset_w+mw+15);			
					
				
				}
				
			}

			jQuery("#preview")
			.css("top",c_top+"px")
			.css("left",c_left+"px")
			.css("width","auto")
			.css("height","auto")
			.css("opacity","0.5");
			
			
			
			jQuery("#preview").animate({opacity: 1},765);
			
			
				
		
			
			
		

		
    },
	function(){
		/*pic.remove();*/
		var title_t=jQuery(this).parent().attr('title');	
		jQuery(this).parent().attr('title',title_t);		
		jQuery("#preview").html("")
			.css("width","1px")
			.css("height","1px")
			.css("left","-9999px");
		
		/*jQuery("#preview").fadeOut('fast').remove();*/
    });	

};


// starting the script on page load
jQuery(document).ready(function(){
	
	
	
	jQuery("body").append("<div id='preview' style='color: #215B9B;font-weight:bold;z-index:65535;'></div>");
	jQuery("#preview").css("position","absolute")
			.css("display","block")
			.css("left","-9999px")
			.css("width","1px")
			.css("height","1px")
			.css("top","10px");
	imagePreview4();
});
