function createXHR() 
{
    var request = false;
        try {
            request = new ActiveXObject('Msxml2.XMLHTTP');
        }
        catch (err2) {
            try {
                request = new ActiveXObject('Microsoft.XMLHTTP');
            }
            catch (err3) {
		try {
			request = new XMLHttpRequest();
		}
		catch (err1) 
		{
			request = false;
		}
            }
        }
    return request;
}


function gradient(id, level)
{
	var box = document.getElementById(id);
	box.style.opacity = level;
	box.style.MozOpacity = level;
	box.style.KhtmlOpacity = level;
	box.style.filter = "alpha(opacity=" + level * 100 + ")";
	box.style.display="block";

	return;
}


function fadein(id) 
{
	var level = 0;
	while(level <= 1)
	{
		setTimeout( "gradient('" + id + "'," + level + ")", (level* 1000) + 10);
		level += 0.01;
	}
}

function zxcWWHS(){
 if (window.innerHeight) return [window.innerWidth-10,window.innerHeight-10,window.pageXOffset,window.pageYOffset];
 else if (document.documentElement.clientHeight) return [document.documentElement.clientWidth-10,document.documentElement.clientHeight-10,document.documentElement.scrollLeft,document.documentElement.scrollTop];
 return [document.body.clientWidth,document.body.clientHeight,document.body.scrollLeft,document.body.scrollTop];
}

// Open the lightbox
function openbox(url, fadin)
{
  var myWidth = 0, myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }

  var scrOfX = 0, scrOfY = 0;
  if( typeof( window.pageYOffset ) == 'number' ) {
    //Netscape compliant
    scrOfY = window.pageYOffset;
    scrOfX = window.pageXOffset;
  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
    //DOM compliant
    scrOfY = document.body.scrollTop;
    scrOfX = document.body.scrollLeft;
  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
    //IE6 standards compliant mode
    scrOfY = document.documentElement.scrollTop;
    scrOfX = document.documentElement.scrollLeft;
  }

  var box = document.getElementById('box'); 

  box.style.left = ((myWidth / 2) - 320) + 'px';
  box.style.top = ((myHeight / 2) - 320 + scrOfY) + 'px';

  var filterBox = document.getElementById('filter');
  filterBox.style.display='block';
  // filterBox.style.border='1px solid red';

  var winDimen = zxcWWHS();

// alert(winDimen[1]);

var pageWidth, pageHeight;
if( window.innerHeight && window.scrollMaxY ) // Firefox 
{
pageWidth = window.innerWidth + window.scrollMaxX;
pageHeight = window.innerHeight + window.scrollMaxY;
}
else if( document.body.scrollHeight > document.body.offsetHeight ) // all but Explorer Mac
{
pageWidth = document.body.scrollWidth;
pageHeight = document.body.scrollHeight;
}
else // works in Explorer 6 Strict, Mozilla (not FF) and Safari
{ 
pageWidth = document.body.offsetWidth + document.body.offsetLeft; 
pageHeight = document.body.offsetHeight + document.body.offsetTop; 
}


  filterBox.style.width= pageWidth + 'px'; 
  filterBox.style.height= pageHeight + 'px'; 

  
//   var title = document.getElementById('boxtitle');
//   title.innerHTML = url;
  
  var content = document.getElementById('boxcontent');
  content.style.padding="0";

  content.innerHTML = "<img src=" + url + " />";

  if(fadin)
  {
	 gradient("box", 0);
	 fadein("box");
  }
  else
  { 	
    box.style.display='block';
  }  	
}	

// Close the lightbox

function closebox()
{
   document.getElementById('box').style.display='none';
   document.getElementById('filter').style.display='none';
}


function switchImageTo(senderId, itemId)
{
        // find thumbnail source
        srcValue = document.getElementById(senderId)['src'];
        srcValue = srcValue.substring(srcValue.indexOf('/redim/') + 7, 9999);
        srcValue = srcValue.substring(srcValue.indexOf('/') + 1, 9999);

        // find main image size
        targetValue = document.getElementById('mainImage' + itemId)['src'];
        var urlEnd = targetValue.indexOf('/redim/') + 7;
        targetValue = targetValue.substring(0, targetValue.indexOf('/', urlEnd) + 1);

        // find main image source
        origValue = document.getElementById('mainImage' + itemId)['src'];
        origValue = origValue.substring(origValue.indexOf('/redim/') + 7, 9999);
        origValue = origValue.substring(origValue.indexOf('/') + 1, 9999);

        // find thumbnail size
        thumbValue = document.getElementById(senderId)['src'];
        var thumbEnd = thumbValue.indexOf('/redim/') + 7;
        thumbValue = thumbValue.substring(0, thumbValue.indexOf('/', thumbEnd) + 1);

	// switch lightbox source
	document.getElementById('mainImage' + itemId).onclick = new Function("openbox('/redim/640x640/" + srcValue + "', 1);");

        // switch images
        document.getElementById('mainImage' + itemId)['src'] = targetValue + srcValue;
        document.getElementById(senderId)['src'] = thumbValue + origValue;

	// switch exif info
	exifMain = document.getElementById('exifMainImage').innerHTML;
	document.getElementById('exifMainImage').innerHTML = document.getElementById('exif' + senderId).innerHTML;
	document.getElementById('exif' + senderId).innerHTML = exifMain;
}

// Loading images asynchronously with no delay

// function preloading(i, url)
// {
// 	var xhr=createXHR();   
// 	xhr.onreadystatechange=function()
// 	{ 
// 		if(xhr.readyState == 4)
// 		{
// 			i.src = url;
// 		} 
// 	}; 
// 
// 	xhr.open("GET", url , true);
// 	xhr.send(null); 
// } 


// function loadAll()
// {
// 	preloading(new Image(), "images/acores.jpg");
//  	preloading(new Image(), "images/prison.jpg");
//	preloading(new Image(), "images/shark.jpg");
// }


// window.onload=loadAll;

