
      var running = false;
      var images;


    function getImageNo(id) {
		return id.substr(4,id.length-4);
	}

	function getLeft(arrayID) {
		return images[arrayID].left;
	}
	function getTop(arrayID) {
		return images[arrayID].top;
	}
	function updateLeftTop(image) {
		var arrayID = parseInt(getImageNo(image.id)) -1;
		if(arrayID) {	
			// only update top left with small images
			var width = images[arrayID].width;
	
			if(parseInt($(image).getWidth()) < parseInt(width)){
			    images[arrayID].left = $(image).style.left;
			    images[arrayID].top = $(image).style.top;
			}
		}

	}

      function openImage(image){
	  var arrayID = parseInt(getImageNo(image.id)) -1;

         var width = images[arrayID].width;
         var height = images[arrayID].height;

	  var imageID = image.id;
		topOfPile(image);

         if(parseInt($(image).getWidth()) > parseInt(width)){
            shrinkAll();
         } else {
	    updateLeftTop(image);
		shrinkAll();
	     var scrollY = getScrollY() + 100;
	     var scrollX = (getWidth() / 2) - 300;
            new Effect.Morph(image, {
              style: {
                   width: width+'px',
		     top:   scrollY+'px',
		     left:  scrollX+'px'
                 }, 
	       duration: 0.75,
	       transition: Effect.Transitions.sinoidal,
               afterFinish: function(){ image.src =  images[arrayID].url;} 
            });
         }

      }

	function resetSearch(){
	removeImages();
Effect.BlindUp('resourceBar',{duration:0.25});
	$('centerSearchBox').appear();
}
      function shrinkAll() {
       

	$$('img.image').each( function(image) {
	   var arrayID = parseInt(getImageNo(image.id)) -1;

         var width = images[arrayID].width;
	  var revertWidth = width / 3;

	     if(parseInt($(image).getWidth()) > parseInt(width)){
				var arrayID = parseInt(getImageNo(image.id)) -1;
				var revertLeft = getLeft(arrayID);
				var revertTop = getTop(arrayID);
		
               new Effect.Morph(image, {
                 style: {
                      width: revertWidth+'px',
					  top:  revertTop,
					  left: revertLeft
                    }, 
                 duration: 0.75,
		 transition: Effect.Transitions.sinoidal,

		 afterFinish: function() { image.src = images[arrayID].thumb; }
               });
            }
           
         }); 
      }

      function topOfPile(image){
         var zIndex = parseInt($(image).style.zIndex);
         $$('img.image').each( function(img) {if(parseInt(img.style.zIndex) >= zIndex) { img.style.zIndex = parseInt(img.style.zIndex) -1;} });
		 var images = $$('img.image');
         $(image).style.zIndex = images.length + 100;
      }
      function removeImages() {
		  $$('img.image').each( function(img) {img.remove()} );  
	  }
	  
      function loadImages(searchTerm) {
      var params = {};
	  if(!searchTerm) {
		  params['q'] = $('searchBox').value;
	  } else {
	  	 params['q'] = searchTerm
	  }
	  removeImages();

       new Ajax.Request('json.php', { 
               parameters: params ,
               onComplete: function(transport) {
							var response = transport.responseText;
							var counter = 0;
							var screenWidth = getWidth() - 200;
							var screenHeight = getHeight() - 200;
							images = response.evalJSON(true);
							if(images.length > 0){
							 if(!searchTerm) {
								$('centerSearchBox').fade();
							 }
							//Effect.BlindDown('resourceBar',{duration:0.25});
							overlay.show();
							$('gallerylink').href= "http://james.linnegar.com/gallery/"+params['q'];
							//$('topSearchBox').Apear()
							images.each(function(image) {
								counter++;
								
								var left=(Math.floor(Math.random()*100))%2;
								var top =(Math.floor(Math.random()*100))%2;
								
								
								var randomleft=Math.floor(Math.random()*(screenWidth/2));
								var randomtop=Math.floor(Math.random()*((screenHeight-140)/2));
								
								if(left == 0)
									randomleft = (screenWidth/2)-randomleft;
								else
									randomleft = (screenWidth/2)+randomleft;
								
								if(top == 0) 
									randomtop = (screenHeight/2)-randomtop;
								else
									randomtop = (screenHeight/2)+randomtop;  									  
								
								var tag = image.thumb;
								var tagId = "img_"+image.imageID
								var newTag = Builder.node('img', {className:'image', id: tagId});   
								
								newTag.src = tag;
								newTag.style.display= "none";
								
								$('cloud').appendChild(newTag);
								newTag.style.top = randomtop+"px";
								newTag.style.left = randomleft+"px";

								image.left = randomleft+"px";
								image.top  = randomtop+"px";
								
								newTag.style.zIndex = parseInt(image.imageID) + parseInt(100);
								new Draggable(newTag, {starteffect: null, endeffect: null, onEnd: function() { updateLeftTop(newTag) } });
								Event.observe(newTag, 'click', function(){topOfPile(newTag)});
                      			Event.observe(newTag, 'dblclick', function(){openImage(newTag)});
								Event.observe(newTag, 'mouseover', function(){highlight(newTag)});
								Event.observe(newTag, 'mousemove', function(){highlight(newTag)});
								Event.observe(newTag, 'mouseout', function(){unhighlight(newTag)});
	

							    $(newTag).appear({duration:0.3});
								
							
							});
							}
							
					 	}
               }); 
 
			   
      }

 function checkGalleryImages() {
      var params = {};
 
       new Ajax.Request('json.php', { 
               parameters: params ,
               onComplete: function(transport) {
							var response = transport.responseText;
							var counter = 0;
							var screenWidth = getWidth() - 200;
							var screenHeight = getHeight() - 200;
							var html = "";
							images = response.evalJSON(true);
							images.each(function(image) {
											image.left = "9";	 
											html = html + "<br />"+ image.left;	 
								
							
							});
							document.write(html);
					 	}
               }); 
 
			   
      }
