/*

Noesis Internet Solutions Ltd. Copyright © 2008

$Version: 09/04/2008

Usage:

class="loadPage" for ajax request of page
class="loadImage for loading image (by extending DOM)

Mouseover -> loads
Mouseclick -> stops mouseout for clicking in loaded layer

Example:

HTML

<a href="page.php?name=tom" class="loadPage" />Show from DB</a>
<a href="original.jpg" class="loadImage" /><img scr="thumb.jpg" /></a>

CSS
#loadBox { width: auto; height: auto; position: absolute; background-color: #e7e7e7; }

REQUIRES

jquery (http://jquery.com/)
*/

this.loadData = function(){
			// options			
			xOffset = 20;
			yOffset = 10;
			loaderIcon = 'loading.gif';
			loaderIconID = '#loaderIcon'; 
			cont = '#loadData'; // Container
			attribute = 'rel';
			behaviour = 'click'; // clickable,hover
			
			// Box creator
			var showBox = function(e){
					var type = $(this).attr("class");
					var source = $(this).attr(attribute);
					
					source.replace(/window.location.host/, "");
					$(cont).length ? $(cont).remove() : $("body").append("<div id='" + cont.replace(/#/, "") + "'><\/div>");
					$(cont).append("<div id='" + loaderIconID.replace(/#/, "") + "'><img src='" + loaderIcon + "' \/></div>");
					
					getPosition(e);
					getData(source,type);
					
					// unbind to enable click/selection of floating box
					if(behaviour == 'select'){
								$(this).click(
												function(event){ $(this).unbind( "mouseout",event.preventDefault()); }
											)
					 }
					 if(behaviour == 'click'){
								$(this).click(
									function(event){
											window.location.href = $(this).attr("href") }
											)
										
					 }
			}
			
			
				var getPosition = function(e){
				
						var top = e.pageY;
						var left = e.pageX;
						
						var boxH = $(cont).height();
						var winH = $(window).height();
						
						var boxW = $(cont).width();
						var winW = $(window).width();
						
						if(top - $(window).scrollTop() + boxH > winH){
							top = top - boxH;
						}
						
						if(left - $(window).scrollLeft() + boxW > winW){
							left = left - boxW;
						}
						
						top = top + yOffset;
						left = left + xOffset;
						
						$(cont).css("top",top + "px").css("left",left + "px");
					}
			
			
				// Loader for box content
				var getData = function(source,type) {
					
						switch(type) {
								case "loadPage":
									$(cont).load(source)
									.error(function(){
													  $(cont).append("<p id='errorImg' class='error'>Die Daten konnten nicht geladen werden</p>").fadeIn();
													});
												
								  break;    
								case "loadImage":
									$(loaderIconID).remove();
									 var check = false;
									 var img = new Image();					
									  $(img).load(function () {
											$(this).css('display','none'); // since .hide() failed in safari
											$(cont).append(img);
											$(this).fadeIn();
									  }).error(function () {
									  	check = true;
									  	$(cont).remove("#errorImg");
										$(cont).append("<p id='errorImg' class='error'>Der Artikel wird im <br />Produktbild oben<br />gezeigt</p>");
									  }).attr('src', source);			
									
								  break;
								default:
								 break;
							}
							$(cont).css({ border:"solid 1px black" });
					}
		
			
			// mouse event
			$("a.loadPage,a.loadImage").mouseout(function(){
											$(cont).css({ border:"none" })
											$(cont).remove();	
										 }).mouseover(showBox).mousemove(getPosition);
												
			
			
	}
	
	$(document).ready(function(){
		loadData();
	});
