function getElement(theName)
{
  if (document.all)
  {
    return document.all[theName];
  }
  else if (document.getElementById)
  {
    return document.getElementById(theName);
  }
  else
  {
    return document[theName];
  }
}

function getNamedElement(name)
{
	if (document.getElementsByName)
  {
  	return document.getElementsByName(name);
  }
  else
  {
  	return document.forms[0].name;
  }
}

function getSelectedValue(theSelect)
{
  if (theSelect.selectedIndex < 0)
    return null;
  return theSelect.options[theSelect.selectedIndex].value;
}

function getRadioValue(theRadioSet)
{
	for (var i = 0; i < theRadioSet.length; i++)
	{
    if (theRadioSet[i].checked)
    {
			return theRadioSet[i].value;
    }
  }
  return null;
}

function arrayShuffle(aArray)
{
	for (var mTemp, j, i = aArray.length; i;) // mTemp, j initialized here for shorter code.
	{
		j = parseInt (Math.random () * i);
		mTemp = aArray [--i];
		aArray [i] = aArray [j];
		aArray [j] = mTemp;
	}
}

function popup(page, name, width, height)
{
  popupWindow=window.open(page,name,"menubar=no,resizable=yes,scrollbars=yes,width=" + width + ",height=" + height);
  popupWindow.focus();
	popupWindow.opener = window;
}

function showExhibitor(exhibitorID)
{
  popup("http://www.picturehouse.org/exhibitor.php?exhibitorID=" + exhibitorID, 'exhibitor', 400, 450);
}

var DragHandler;

// Draggable popup functions

function showPopup(url, anchorPoint, boxWidth, boxHeight)
{
	// don't display popup more than once
	if (document.getElementById('popup'))
		return;
	
	var browser = navigator.userAgent;
	var ie = browser.indexOf('MSIE') > -1;
	
	// prefix URL with slash if necessary
	if (url.indexOf('://') == -1 && url.substr(0, 1) != '/')
		url = '/' + url;

	var width = document.documentElement.clientWidth + document.documentElement.scrollLeft;
	var x, y;
	
	if (!boxWidth)
		boxWidth = 400;
	if (!boxHeight)
		boxHeight = 400;
	
	if (anchorPoint)
	{
		x = anchorPoint.offsetLeft + anchorPoint.offsetWidth + 15;
		y = anchorPoint.offsetTop + 5;
	}
	
	if (x > width - boxWidth) x = width - boxWidth - 15;
	if (!x) x = 360;
	if (!y) y = 360;

	var div = document.createElement('div');
	div.id = 'popup';
	div.style.zIndex = 3;
	div.style.position = 'absolute'; // (navigator.userAgent.indexOf('MSIE 6') > -1) ? 'absolute' : 'fixed';
	div.style.left = x + 'px'; 
	div.style.top = y + 'px';
	div.style.height = boxHeight + 'px';
	div.style.width = boxWidth + 'px';
	div.className = 'popup';
	div.src = url;
	document.body.appendChild(div); 
	
	var masthead = document.createElement('div');
	masthead.style.width = boxWidth + 'px';
	masthead.className = 'masthead';
	masthead.style.cursor = 'move';
	div.appendChild(masthead);
	
	var img = document.createElement('img');
	img.src = '/images/icons-16/close.png';
	img.align = 'right';
	img.style.padding = '2px 1px';
	img.style.cursor = 'default';
	img.onclick = function()
	{
	  document.body.removeChild(document.getElementById('popup'));
	};
	masthead.appendChild(img);
	
	var h1 = document.createElement('h1');
	h1.id = 'popup_title';
	h1.className = 'popupTitle';
	masthead.appendChild(h1);
	
	var iframe;
	if (ie)
	{
		iframe = document.createElement("<iframe onload='setTitle()'>");
	}
	else
	{
		iframe = document.createElement('iframe');
		iframe.onload = setTitle;
	}
	iframe.id = 'popup_iframe';
	iframe.frameBorder = '0';
	iframe.style.margin = '6px 0 0 1px';
	iframe.style.height = (boxHeight - 29) + 'px';
	iframe.style.width = (boxWidth - 2) + 'px';
	iframe.src = url;
	div.appendChild(iframe);
	
  DragHandler = {

    // private property.
    _oElem : null,

    // public method. Attach drag handler to an element.
    attach : function(oElem) {
        oElem.onmousedown = DragHandler._dragBegin;

        // callbacks
        oElem.dragBegin = new Function();
        oElem.drag = new Function();
        oElem.dragEnd = new Function();

        return oElem;
    },

    // private method. Begin drag process.
    _dragBegin : function(e) {
        var oElem = DragHandler._oElem = this;

        if (isNaN(parseInt(oElem.style.left))) { oElem.style.left = '0px'; }
        if (isNaN(parseInt(oElem.style.top))) { oElem.style.top = '0px'; }

        var x = parseInt(oElem.style.left);
        var y = parseInt(oElem.style.top);

        e = e ? e : window.event;
        oElem.mouseX = e.clientX;
        oElem.mouseY = e.clientY;

        oElem.dragBegin(oElem, x, y);

        document.onmousemove = DragHandler._drag;
        document.onmouseup = DragHandler._dragEnd;
        return false;
    },

    // private method. Drag (move) element.
    _drag : function(e) {
        var oElem = DragHandler._oElem;

        var x = parseInt(oElem.style.left);
        var y = parseInt(oElem.style.top);

        e = e ? e : window.event;
        oElem.style.left = x + (e.clientX - oElem.mouseX) + 'px';
        oElem.style.top = y + (e.clientY - oElem.mouseY) + 'px';

        oElem.mouseX = e.clientX;
        oElem.mouseY = e.clientY;

        oElem.drag(oElem, x, y);

        return false;
    },

    // private method. Stop drag process.
    _dragEnd : function() {
        var oElem = DragHandler._oElem;

        var x = parseInt(oElem.style.left);
        var y = parseInt(oElem.style.top);

        oElem.dragEnd(oElem, x, y);

        document.onmousemove = null;
        document.onmouseup = null;
        DragHandler._oElem = null;
    }
  };
  
  DragHandler.attach(div);
   
}

function setTitle()
{
	var h1 = document.getElementById('popup_title');
	h1.innerHTML = document.getElementById('popup_iframe').contentWindow.document.title;
}


// Gallery functions

var galleryTimeoutID;
var galleryImage = new Array();
var bg, gallery, captionText;
var credit = new Array();
var currentIndex = 0;

function showGallery()
{
  bg = getElement('veil');
  bg.style.width = document.body.scrollWidth + 'px';
  bg.style.height = document.body.scrollHeight + 'px';
  $(bg).fadeTo(0, 0);

  gallery = getElement('gallery');
  galleryImage[0] = getElement('image1');
  galleryImage[1] = getElement('image2');
  captionText = getElement('captionText');

  galleryImage[0].src = '';
  galleryImage[1].src = '';

	$(bg).show();
  $(bg).fadeTo('normal', 0.8);
  $(gallery).fadeIn();

  loadImage(1 - currentIndex);
  stopTimeout();
  nextSlide();
}

function closeGallery()
{
  stopTimeout();

  $(bg).fadeOut();
  $(gallery).fadeOut();
  $(galleryImage[0]).hide();
  $(galleryImage[1]).hide();
}

function nextSlide()
{
  if (!galleryImage[1 - currentIndex].complete)
  {
  	stopTimeout();
		galleryTimeoutID = setTimeout('nextSlide()', 1000);
		return;
  }
  
	currentIndex = 1 - currentIndex;
	if (captionText == null)
	{
		switchImage();
	}
	else
	{
		$(captionText).animate( {marginLeft: '-700px'} , 500, switchImage);
	}
}

function switchImage()
{
	$(galleryImage[1 - currentIndex]).fadeOut(1000, loadImage);
	$(galleryImage[currentIndex]).fadeIn(1000);
	if (captionText != null)
	{
	  captionText.innerHTML = '&copy; 2009 ' + credit[currentIndex];
		$(captionText).animate( {marginLeft: 0}, 1000, 'swing');
	}

	stopTimeout();
  galleryTimeoutID = setTimeout("nextSlide()", 5000);
}

function stopTimeout()
{
  if (galleryTimeoutID)
  {
  	clearTimeout(galleryTimeoutID);
  	galleryTimeoutID = 0;
  }
}
	


