var currentHash = "";
var hashInterval;

function checkHash()
{
  if(document.location.hash != currentHash)
  {
    if(!loadFromHash())
    {
      currentHash = document.location.hash;
    }
  }
}

function firstLoad()
{
  if(!loadFromHash())
  {
    loadAlb(0);
    loadPhoto(0,0);
  }
  
  hashInterval = setInterval(checkHash, 100);
}

function loadFromHash()
{
  if(document.location.hash && (document.location.hash != ""))
  {
    var hash = unescape(document.location.hash).replace(/\+/g, ' ');
    if(hash.indexOf('#') == 0) hash = hash.substr(1);
    var parts = hash.split('/');
    
    var photoDefined = 1;
    
    var photo = "";
    var albName = "";
    
    if(parts.length == 0)
      return false;
    
    if(parts.length == 1) photoDefined = 0;
    else photo = parts[1];
    
    albName = parts[0];
    
    var albnum = -1; var pnum = -1;
    for(var i = 0, len = albArr.length; i < len; i++)
    {
      if(albArr[i][0] == albName)
      {
        albnum = i;
        loadAlb(i);
        if(photoDefined)
        {
          for(var j = 0, len2 = albArr[i][1].length; j < len2; j++)
          {
            if(albArr[albnum][1][j] == photo)
            {
              pnum = j;
              loadPhoto(albnum, j);
              break;
            }
          }
        }
        break;
      }
    }
    
    if(albnum == -1)
      return false;
      
    if(pnum == -1)
      loadPhoto(albnum, 0);
      
    return true;
  }
  else
    return false;
}

function loadAlb(albnum)
{
	targetDiv = document.getElementById('photoselect');
	targetDiv.innerHTML = "";
	
	albName = albArr[albnum][0];
	
	tmpDiv = document.createElement('div');
	tmpDiv.style.height = '160px'; tmpDiv.style.overflow = 'hidden';
	
	for(var i = 0, len = albArr[albnum][1].length; i < len; i++)
	{
		var thumbDiv = document.createElement('div');
		thumbDiv.className = 'photoThumb';
				
		with ({picid : i}) {
		thumbDiv.onclick = function(){loadPhoto(albnum, picid)};
		}

		tmpDiv.appendChild(thumbDiv);
		
		
		var thumbImgDiv = document.createElement('div');
		thumbDiv.appendChild(thumbImgDiv);
		
		var thumbImg = document.createElement('img');
		thumbImg.src = escape('img/'+albName+'/t/'+albArr[albnum][1][i]);
		thumbImgDiv.appendChild(thumbImg);
		
		//targetDiv.innerHTML += albArr[albnum][1][i];
	}
	
	tmpDiv.style.width = ((i*232)+5)+'px';
	
	targetDiv.appendChild(tmpDiv);
}

function loadPhoto(albnum, pnum)
{
	targetDiv = document.getElementById('main');
	targetDiv.innerHTML = "";
	
	albName = albArr[albnum][0];
	photo = albArr[albnum][1][pnum];
	
	containerDiv = document.createElement('div');
	containerDiv.style.position = 'absolute';
	containerDiv.style.left = '0px'; containerDiv.style.top = '0px';
	containerDiv.style.right = '0px'; containerDiv.style.bottom = '0px';
	
	labelDiv = document.createElement('div');
	labelDiv.className = 'photoLabel';
	labelDiv.style.left = ((targetDiv.clientWidth/2)-75)+'px';
	labelDiv.innerHTML = photo+' | <a href="'+albName+'/'+photo+'">full image</a>';
	
	var loader = document.createElement('img');
	loader.style.top = ((targetDiv.clientHeight/2)-16)+'px';
	loader.style.left = ((targetDiv.clientWidth/2)-16)+'px';
	loader.src = 'loader.gif';
	loader.style.position = 'absolute';
	
	
	var pimg = document.createElement('img');
	pimg.className = "mainPhoto"; 
	
	ext = targetDiv.clientWidth+'/'+targetDiv.clientHeight;
	
	pimg.src = escape('img/'+albName+'/c/'+ext+'/'+photo);
	containerDiv.appendChild(pimg);
	containerDiv.appendChild(labelDiv);
	targetDiv.appendChild(loader);
	targetDiv.appendChild(containerDiv);
	
	if(pageTracker != null)
    pageTracker._trackPageview('/'+albName+'/'+photo);
	
	document.location.hash = escape((albName+'/'+photo).replace(/ /g, '+'));
	currentHash = document.location.hash;
}