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 setRadioValue(radioObj, newValue)
{
	if(!radioObj)
		return;
	var radioLength = radioObj.length;
	if(radioLength == undefined)
	{
		radioObj.checked = (radioObj.value == newValue.toString());
		return;
	}
	for(var i = 0; i < radioLength; i++)
	{
		radioObj[i].checked = false;
		if(radioObj[i].value == newValue.toString())
		{
			radioObj[i].checked = true;
		}
	}
}


function setField(theName, theValue, fieldType)
{
	var obj = $("[name='" + theName + "']");
	obj.val(theValue);
	return;
}

function getAbsPosition(theElement)
{
	pos = new Object();
	pos.x = 0;
	pos.y = 0;
	
	while (theElement)
	{
		pos.x += theElement.offsetLeft;
		pos.y += theElement.offsetTop;
		theElement = theElement.offsetParent;
	}
	return pos;
}

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);
}

function showPopup(url, anchorPoint, boxWidth, boxHeight)
{
	// don't display popup more than once
	if ($('#popup').length > 0)
		return;

	// prefix URL with slash if necessary
	if (url.indexOf('://') == -1 && url.substr(0, 1) != '/')
		url = '/' + url;
		
	var windowWidth = document.documentElement.clientWidth + document.documentElement.scrollLeft;
	var windowHeight = document.documentElement.clientHeight + document.documentElement.scrollTop;
	var x, y;
  if (!boxWidth)
		boxWidth = 400;
	if (!boxHeight)
		boxHeight = 400;
  if (anchorPoint)
	{
		var pos = $(anchorPoint).offset();
		x = pos.left + $(anchorPoint).width() + 10;
		y = pos.top;
	}
	if (!x) x = 360;
	if (!y) y = 360;

  if (x + boxWidth > windowWidth)
  	x = windowWidth - boxWidth - 15;
  if (y + boxHeight > windowHeight)
  	y = windowHeight - boxHeight - 15;

	var div = $('<div>');
	div.attr({
		'id': 'popup',
		'class': 'popup'
	});
	div.css({
		'zIndex': 3,
		'position': 'absolute',
		'top': y,
		'left': x,
		'display': 'none'
	});
	div.height(boxHeight);
	div.width(boxWidth);
	
  var masthead = $('<div>');
	masthead.addClass('masthead');
	masthead.css('cursor', 'move');
	masthead.width(boxWidth);
	
  var img = $('<img>');
	img.attr({
		'src': '/images/icons-16/close.png'
	});
	img.css({
		'float': 'right',
		'padding': '2px 1px',
		'cursor': 'default'
	});
	img.bind('click', function() { $('#popup').remove(); } );
	masthead.append(img);
  
  var h1 = $('<h1>');
	h1.attr({
		'id': 'popup_title',
		'class': 'popupTitle'
	});
	masthead.append(h1);
	div.append(masthead);
  
  var iframe = $('<iframe>');
  iframe.attr({
  	'id': 'popup_iframe',
  	'frameBorder': '0',
  	'src': url
  });
  iframe.css('margin', '6px 0 0 1px');
	iframe.height(boxHeight - 29);
	iframe.width(boxWidth - 2);
	iframe.load(function() { $('#popup_title').text(iframe.contents().find('title').text()); });
	div.append(iframe);
	
	//alert($("<div>").append(div).html());
	//return;
	
	$('body').append(div);
	div.fadeIn(1000);
	div.draggable();

	return;
}

// Gallery functions

// must be populated elsewhere
var galleryPath = '';
// must also populate var imageList = new Array(...);

// one of these must be populated elsewhere
var imageCaption = new Array();
var defaultCaption= '';

// set to desired value (in milliseconds)
var interval = 4000;

var galleryImage = new Array();
var imageRef = new Array();
var bg, gallery;
var nImages;
var imageIndex;
var frameIndex = 0;
var loadDone;
var timeOutDone;
var galleryTimeoutID;

function showGallery()
{
	nImages = imageList.length;
	imageIndex = Math.floor(Math.random() * nImages);

  bg = $('#veil');
  bg.width(document.body.scrollWidth);
  bg.height(document.body.scrollHeight);
  bg.fadeTo(0, 0);

  gallery = $('#gallery');
  galleryImage[0] = $('#image1');
  galleryImage[1] = $('#image2');
  captionField = $('#caption');
  refField = $('#ref');
  
  galleryImage[0].attr('src', '');
  galleryImage[1].attr('src', '');
  galleryImage[0].hide();
  galleryImage[1].hide();

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

  loadImage(true);
}

function loadImage(first)
{
	loadDone = false;
	timeOutDone = false;

	if (++imageIndex >= nImages)
		imageIndex = 0;

	var caption = imageCaption[imageIndex] == undefined  ? defaultCaption : imageCaption[imageIndex];
  galleryImage[1 - frameIndex].attr('src', galleryPath + imageList[imageIndex]);
  galleryImage[1 - frameIndex].attr('alt', caption);
  galleryImage[1 - frameIndex].attr('title', caption);
  imageRef[1 - frameIndex] = imageList[imageIndex];

  if (first)
  {
    galleryImage[1 - frameIndex].ready(function() { showNextSlide(); loadImage(false) });
  }
 	else
 	{
    galleryImage[1 - frameIndex].ready(function() { loadDone = true; nextSlide(); });
 		galleryTimeoutID = setTimeout(function() { timeOutDone = true; nextSlide(); }, interval);
	}
}

function nextSlide()
{
	//alert("load: " + loadDone + "\ntimeOut: " + timeOutDone);
	if (!loadDone || !timeOutDone)
		return;
	
	fadeThisSlide();
}

function fadeThisSlide()
{
	galleryImage[frameIndex].fadeOut(1000, function() { loadImage(false) });
	if (captionField.length == 1)
		captionField.animate( {marginLeft: '-700px'}, 500, showNextSlide);
	else
		showNextSlide();	
}

function showNextSlide()
{
	frameIndex = 1 - frameIndex;
	galleryImage[frameIndex].fadeIn(1000);
	if (captionField.length == 1)
	{
	  captionField.text(galleryImage[frameIndex].attr('alt'));
		captionField.animate( {marginLeft: 0}, 1000);
	}
	if (refField.length == 1)
	{
		var ref = imageRef[frameIndex].replace(/(_|\.jpg)/ig, '');
		refField.text(ref);
	}
}

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

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

  
function showHide(show, showSections, hideSections)
{
	showHideList(show, showSections);
	showHideList(!show, hideSections);
}

function showHideList(show, sections)
{
	for (var i in sections)
	{
		element = $('#' + sections[i]);
		if (element)
			element.css('display', show ? '' : 'none');
		else
			alert(Section + sections[i] + " does not exist!");
	}
}

