﻿var imgSelect = 0;
 var oldImg = null;

 $(document).ready(function () {
		
	$(".CS_imageGallery a").attr("title","Click here to see the drawing in a larger format.");
	
	
	$(".CS_imageGallery a").click(function () {
	  			

	     var dHeight = $(document).height();

    	  
	      $("body").append('<div id="imageBoxBG"/>');	  
     
	      $("#imageBoxBG").animate({opacity: "0.8" }, 1000).css(
	      {
		    filter: "alpha(opacity=80)",
		    width: $(document).width() + "px",
		    height: $(document).height() + "px"
	      });
	  
	  
		var daImage = new Image();    	  

	      $("<img>").bind('load readystatechange', function(e){
       	    
        	    if (this.complete || (this.readyState == 'complete' && e.type == 'readystatechange')) 
        	    {
			            setUp($(this).attr("src"));			
		    }		        
		    
	      }).attr("src",$(this).attr("href"));
	      
	      return false;
  
	  });	
});

function setUp(src){
			if($("#imageBox").length > 0)
			    return;
			    
			$("body").append('<div id="imageBox"><div id="inner"><div><img id="imageBoxImage" class="imageBoximg" src="' + src + '" /></div></a> <div id="imageBoxClose"><img src="/Admin/DesignBilder/close.png" alt="Close" title="Close" /></div></div></div>');
			
						$("#imageBoxImage").bind('load readystatechange', function(e){
        		if (this.complete || (this.readyState == 'complete' && e.type == 'readystatechange')) {
					loaded("2: " + e.type);
				}
			});
				
			$("#imageBox").animate({opacity: 1.0},1000);
			
				  

	  
	  $("#imageBoxClose").click(function (){		
		    $("#imageBox").remove();
			$("#imageBoxBG").remove();
            oldImg = null;
	  });
}



function loaded(s)
{
	
		if($("#imageBoxImage").attr("src") == oldImg)
		{	
			return;			
		}
					
		oldImg = $("#imageBoxImage").attr("src");	
		
		$("#imageBoxImage").height("auto");
		
		if(($("#imageBoxImage").height() + 50) > $(window).height()) 
		{
			$("#imageBoxImage").height(($(window).height() - 60) + "px");
		}
		else
		{
			$("#imageBoxImage").height("auto");
		}

		$("#imageBox").animate({						
				top: ($(document).scrollTop( ) + 20).toString() + "px"
	  		},function(){
				$("#inner").animate({
					height: ($("#imageBoxImage").height() + 5) + "px"
				},function(){
					$(this).animate({
					width: ($("#imageBoxImage").width() + 5) + "px"
				},function(){
					$("#imageBoxImage").show();
				});
			});
	   });      
	  
	  
}