/*

	Original Code:
	Lightbox JS: Fullsize Image Overlays 
	by Lokesh Dhakar - http://www.huddletogether.com

	Licensed under the Creative Commons Attribution 2.5 License - http://creativecommons.org/licenses/by/2.5/
	(basically, do anything you want, just leave my name and link)
	
	Zoom function and modifications by Pardal Freudenthal
	
*/



	$ = function (id) {
	return document.getElementById(id);
	}

	
// www.sean.co.uk

	function pause(millis) {
		var date = new Date();
		var curDate = null;
		
		do { 
			curDate = new Date(); 
		}
		while((curDate - date) < millis);
	} 
	
	
	
//
// getPageScroll()
// Returns array with x,y page scroll values.
// Core code from - quirksmode.org
//  
	function getPageScroll(){

		var yScroll;
	
		if (self.pageYOffset) {
			yScroll = self.pageYOffset;
		} else if (document.documentElement && document.documentElement.scrollTop){	 
			yScroll = document.documentElement.scrollTop;
		} else if (document.body) {
			yScroll = document.body.scrollTop;
		}
	
		arrayPageScroll = new Array('', yScroll)
		return arrayPageScroll;
	}

//
// getPageSize()
// Returns array with page width, height and window width, height
// Core code from - quirksmode.org
// Edit for Firefox by pHaez
//
	function getPageSize(){
		
		var xScroll, yScroll;
		

			xScroll = document.body.offsetWidth;
			yScroll = document.body.offsetHeight;
		
    
		var windowWidth, windowHeight;
		if (self.innerHeight) {	
			windowWidth = self.innerWidth;
			windowHeight = self.innerHeight;
		} else if (document.documentElement && document.documentElement.clientHeight) { 
			windowWidth = document.documentElement.clientWidth;
			windowHeight = document.documentElement.clientHeight;
		} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
			xScroll = document.body.offsetWidth;
			yScroll = document.body.offsetHeight;
		}
		 
		
		if(yScroll < windowHeight) {
			pageHeight = windowHeight;
		} else { 
			pageHeight = yScroll;
		}
	
		if(xScroll < windowWidth){	
			pageWidth = windowWidth;
		} else {
			pageWidth = xScroll;
		}
	
	
		arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
		return arrayPageSize;
	}

  var control = 1;
	function hideContainer() {
		objContainer = $('screenshot-container');
		objContainer.style.display = "none";
    control = 0;
	}
	
	
	
	function showContainer(objLink) {

   control = 1;
  
		var objContainer = $('screenshot-container');
    var objImage = $('containerImage');
		objImage.src = objLink.rel; 

    var arrayPageSize = getPageSize();
	  var arrayPageScroll = getPageScroll();
		

  	 
	  imgPreload = new Image();

	  imgPreload.onload = function() {
 			var endHeight = imgPreload.height;
			var endWidth = imgPreload.width;
      
  		var thumb = new Image();
  		if (document.all) {
  			thumb = objLink.firstChild;
  		}else {
  			thumb = objLink.firstChild.nextSibling;
  		}

      
      var imgHeight = thumb.height;
      var imgWidth = thumb.width;
      
			if (navigator.appVersion.indexOf("MSIE")!=-1) {
				pause(200);
			} 


     
      zoom = function() {

        if(control == 0)  {
          clearInterval(active);
        }
       
        else if ((imgHeight < endHeight) && (imgWidth < endWidth)) {
        
        objImage.setAttribute('width', imgWidth);
        objImage.setAttribute('height', imgHeight);
        
    		var containerTop = arrayPageScroll[1] + ((arrayPageSize[3] - 15 - imgHeight) / 2);
    		var containerLeft = ((arrayPageSize[0] - 20 - imgWidth) / 2);
  			
   		
  			
    		objContainer.style.top = (containerTop < 0) ? "0px" : containerTop + "px";
    		objContainer.style.left = (containerLeft < 0) ? "0px" : containerLeft + "px";
        
        
        
        objContainer.style.display = "block";
        
        imgWidth = parseInt(imgWidth) + parseInt(imgWidth)*0.15;
        imgHeight = parseInt(imgHeight) + parseInt(imgHeight)*0.15;
        
        } else {
  
          objImage.setAttribute('width', endWidth);
          objImage.setAttribute('height', endHeight);
          
      		var containerTop = arrayPageScroll[1] + ((arrayPageSize[3] - 15 - endHeight) / 2);
      		var containerLeft = ((arrayPageSize[0] - 20 - endWidth) / 2);
          
      		objContainer.style.top = (containerTop < 0) ? "0px" : containerTop + "px";
          objContainer.style.left = (containerLeft < 0) ? "0px" : containerLeft + "px";
   				
          
          objContainer.style.display = "block";
          clearInterval(active);
          }
          
     } 

      var active = setInterval("zoom()",10);

      return false;
    }
    
    imgPreload.src = objLink.rel;
  
	}
  
