/*
 * 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(){	
	/* CONFIG */
		
		xOffset = -300;
		yOffset = 30;
		
		// these 2 variable determine popup's distance from the cursor
		// you might want to adjust to get the right result
                xx = $("#wrap").offset();
		
	/* END CONFIG */
	$("a.preview").click(function(e){
		this.t = this.title;
		this.title = "";
               

		var c = (this.t != "") ? "<br/>" + this.t : "";
                var a = $("<a href='#' id='close_preview'>close</a>");
              
           
		 var p = $("<p id='preview'></p>");
               var width=this.getAttribute('size');
              // alert (width);
              var prev =this.previousSibling;
              var a3 = $("<a href='#' id='prev_preview'>prev</a>");
                         a3.bind("click", function(e){loadMe(prev);});
                 p.append(a3);
              if (prev == null)
              {
                 a3.hide();      
             }
                 a.bind("click", function(e){ $("#preview").remove();});
              var next = this.nextSibling;
           
              var a2 = $("<a href='#' id='next_preview'>next</a>");
                     a2.bind("click", function(e){ loadMe(next);});
                      p.append(a2);
              if (next == null)
              {
                  a2.hide(); 
              }
               var off = xx.left+ ((780 - this.getAttribute('size'))/2);
               
                    
                          
                p.append(a);
                p.append ($("<img src='"+ this.href +"' alt='Image preview' />"));
		$("body").append(p);								 
		$("#preview").css("top",(50/*e.pageY - xOffset*/) + "px").css("left",(off) + "px").fadeIn("fast");
                //$("#preview").css("top",(offset.top) + "px").css("left",(offset.left) + "px").fadeIn("fast");
              //   $("#preview").css("top",(0) + "px").css("left",(0) + "px").fadeIn("fast");
          
              return false;
    }/*,
	function(){
		/*this.title = this.t;
                 if 
		$("#preview").remove();*/
  /*  });	*/
	/*$("a.preview").mousemove(function(e){
		$("#preview")
			.css("top",(e.pageY - xOffset) + "px")
			.css("left",(e.pageX + yOffset) + "px");	
	}*/);		
};

function loadMe(objct)
{
  

   
             
              var prev =objct.previousSibling;
               var next = objct.nextSibling;
                $('#prev_preview').unbind();
                 $('#next_preview').unbind();
                
              if  (prev != null)
              {
                $('#prev_preview').bind("click", function(e){loadMe(prev);});
               $('#prev_preview').show();
              }
              else
             $('#prev_preview').hide();
               
               
               
                 if  (next != null)
              {
                $('#next_preview').bind("click", function(e){loadMe(next);});
                $('#next_preview').show();
              }
              else
               $('#next_preview').hide();
              
             
          var i = $("#preview img");
         
                           
                i[0].src=  objct.href;
}
// starting the script on page load
$(document).ready(function(){
	imagePreview();
});
