latestrequest = 0;
lastpage = document.location.href;

function stripJS(el)
{
  re = /<!--JSREMOVE-->[\s\S]*?<!--\/JSREMOVE-->/gi;
  return el.replace(re, "");
}

function getAjaxObj()
{
  var oAjax = null;
  if(window.XMLHttpRequest)
    oAjax = new XMLHttpRequest;
  else if(window.ActiveXObject)
    oAjax = new ActiveXObject("Microsoft.XMLHTTP");
  else
    return false;
  return oAjax;
}

function doAjaxRequest(url, callback, pst)
{
  oAjax = getAjaxObj();
  if(typeof(oAjax) == "boolean" && oAjax == false)
    return false;

  latestrequest++;
  myrequest = latestrequest;
  oAjax.onreadystatechange = function() {
    if(myrequest != latestrequest || oAjax.readyState != 4)
      return;

    if(oAjax.status != 200)
      return;

    callback(oAjax.responseText);
  }

  lastpage = url;
  oAjax.open(pst==null?"GET":"POST", url, true);
  if(pst != null) {
    oAjax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
  }
  oAjax.send(pst);

  return true;
}

function displayAjax(content)
{
  re = /<!--ajax:([a-z0-9-_]+)-->([\s\S]*)<!--\/ajax:\1-->/gi;
  var i = 0;
  var matches = [];
  while(1) {
    match = re.exec(content);
    if(match == null) 
      break;
    matches[i] = match;
    i++;
  }

  for(i = 0; i < matches.length; i++) {
    var el = document.getElementById(matches[i][1]);
    el.innerHTML = stripJS(matches[i][2]);
    fixA(el);
    fixForm(el);
  }
}

function localURL(url)
{
  if(!url.match(/^[a-z]+:\/\//i))
    return true;

  re = /^([a-z]+:\/\/[^\/]+)/i
  m = re.exec(document.location.href);
  if(m != null && url.substr(0, m[1].length) == m[1])
    return true;

  return false;
}

function clickedA()
{
  if(!localURL(this.href))
    return true;

  return !doAjaxRequest(this.href, displayAjax);
}

function fixA(d)
{
  aElements = d.getElementsByTagName("a");
  for(i = 0; i < aElements.length; i++) {
    if(typeof(aElements[i].onclick)=="undefined")
      aElements[i].onclick = clickedA;
  }
  aElements = d.getElementsByTagName("area");
  for(i = 0; i < aElements.length; i++) {
    if(typeof(aElements[i].onclick)=="undefined")
      aElements[i].onclick = clickedA;
  }
}

function submittedForm()
{
  pstd = "";
  for(i = 0; i < this.elements.length; i++) {
    el = this.elements[i];
    if(el.name == "" || el.name == null)
      continue;

    eld = escape(el.name)+"="+escape(el.value);
    if(pstd != "")
      pstd += "&";
    pstd += eld;
  }

  starget = this.action == "" ? lastpage : this.action;
  return !doAjaxRequest(starget, displayAjax, pstd);
}

function fixForm(d)
{
  aElements = d.getElementsByTagName("form");
  for(i = 0; i < aElements.length; i++) {
    frm = aElements[i];
    canpost = true;
    for(j = 0; j < frm.elements.length; j++) {
      el = frm.elements[j];
      if(el.type == "file") {
        canpost = false;
        break;
      }
    }
    if(!canpost)
      continue;

    if(frm.target == "" || localURL(frm.target))
      frm.onsubmit = submittedForm;
  }
}

function fixAjax()
{
  document.body.innerHTML = stripJS(document.body.innerHTML);
  fixA(document);
  fixForm(document);
}

window.onload = fixAjax;


function imageLoad(info)
{
  ainfo = info.split("/");
  url = ainfo[0];
  num_images = ainfo[1];

  security = '<img id="gallery_security" style="position:absolute;z-index:9999999;" src="/images/transparent.gif" width="0" height="0">';
  document.getElementById('himage').innerHTML = security+'<img id="gallery_imgdisplay" style="visibility: hidden;" onload="javascript:fixPosition();" alt="Loading image..." style="border: 2px solid white;" src="/gallery/image/big/'+gallery_name+'/'+url+'" />';
  if(gallery_image > 0) {
    document.getElementById('hprev').innerHTML = '<a href="#" onclick="return galleryMove(-1);"><font color="white">&laquo; prev</font></a>';
  }
  if(gallery_image + 1 < num_images) {
    document.getElementById('hnext').innerHTML = '<a href="#" onclick="return galleryMove(1);"><font color="white">next &raquo;</font></a>';
  }
}

function fixHeight()
{
	document.getElementById('page').style.height = document.body.parentNode.scrollHeight+"px";
	document.getElementById('gallery_transparent').style.height = document.body.parentNode.scrollHeight+"px";
}

function fixPosition()
{
	var img = document.getElementById('gallery_imgdisplay');
	var sec = document.getElementById('gallery_security');

	img.style.visibility = 'visible';
	sec.style.width = img.offsetWidth+"px";
	sec.style.height = img.offsetHeight+"px";

	fixHeight();
}

window.onresize = fixHeight;

function imageInfo(gallery, iid)
{
  doAjaxRequest("/gallery/imageInfo/"+gallery+"/"+iid, imageLoad, null);
}

function galleryClose()
{
	document.getElementById('gallery_transparent').style.display = 'none';
	document.getElementById('gallery_info').style.display = 'none';

	return false;
}

function galleryMove(dist)
{
	gallery_image += dist;
	galleryOpen(gallery_name, gallery_image);
}

gallery_image = -1;
gallery_name = "";
gallery_created = false;
function galleryOpen(gallery, iid)
{
	gallery_name = gallery;
	gallery_image = iid;
	if(gallery_created) {
		document.getElementById('gallery_transparent').style.display = 'block';
		document.getElementById('gallery_info').style.display = 'block';
		document.getElementById('himage').innerHTML = '<font color="white">Loading...</font>';
		document.getElementById('hnext').innerHTML = "";
		document.getElementById('hprev').innerHTML = "";
	} else {
		gallery_created = true;
		ndiv = document.createElement('div');
		ndiv.id = "gallery_transparent";
		ndiv.style.position = "absolute";
		ndiv.style.left = "0px";
		ndiv.style.top = "0px";
		ndiv.style.right = "0px";
		ndiv.style.bottom = "0px";
		ndiv.style.width = "100%";
		ndiv.style.height = document.body.parentNode.scrollHeight;
		ndiv.style.background = "#000000";
		ndiv.style.zIndex = 10000;

		ndiv.style.filter = "alpha(opacity=50);";
		ndiv.style.opacity = 0.5;
		try {
			ndiv.style.setProperty("-moz-opacity", "50%", null);
		}
		catch(e){}

		hclose = '<a onclick="javascript:return galleryClose();" href="#"><font color="white">close</font></a>';

		ndiv2 = document.createElement('div');
		ndiv2.id = "gallery_info";
		ndiv2.style.position = "absolute";
		ndiv2.style.left = "0px";
		ndiv2.style.top = "0px";
		ndiv2.style.right = "0px";
		ndiv2.style.bottom = "0px";
		ndiv2.style.width = "100%";
		ndiv2.style.height = document.body.offsetHeight;
		ndiv2.style.zIndex = 10001;


		centered = '<table><tr><td colspan="3" style="position: relative;" id="himage" align="center"><font color="white">Loading...</font></td></tr><tr><td id="hprev" align="left" width="33%"></td><td align="center" width="33%">'+hclose+'</td><td id="hnext" align="right" width="33%"></td></tr></table>';
		ndiv2.innerHTML = '<table width="100%" height="100%" border="0"><tr><td valign="center" align="center">'+centered+'</td><tr></table>';


		document.body.appendChild(ndiv);
		document.body.appendChild(ndiv2);
	}
	imageInfo(gallery, iid);
	return false;
}

function setCookie(name, value, days)
{
  var today = new Date();
  var expires = new Date();
  expires.setTime(today.getTime() + 86400000*days);
  document.cookie = name + "=" + escape(value) + ";expires=" + expires.toGMTString()+";path=/";
}


function blockSwitch(id)
{
	var block = document.getElementById('block'+id);
	if(block.className.substr(block.className.length-4) == "open") {
		block.className = "block block_closed";
		setCookie('block'+id, 0, 30);
	} else {
		block.className = "block block_open";
		setCookie('block'+id, 1, 30);
	}

	return false;
}

function trim(s)
{
	s = s.replace(/^\s+/,'');
	s = s.replace(/\s+$/,'');
	return s;
}

function blockInit()
{
	cookies = document.cookie.split(";");
	for(i = 0; i < cookies.length; i++) {
		cookie = cookies[i].split("=");
		cookiename = trim(cookie[0]);
		cookievalue = (cookie.length >= 2 ? trim(cookie[1]) : false);
		if(cookiename.substr(0, 5) == "block" && cookievalue == "0") {
			blockSwitch(cookiename.substr(5));
		}
	}
}


