


// this is a little crutch function that will provide a more informative error if a simple typo occurs in the id tags.
function checkExists(elementId) {
	if (!document.getElementById(elementId)) alert("Expected to find element with id " + elementId +", but instead found " + document.getElementById(elementId));	
}
// POPUP SCRIPT
function popup(url,w,h,target,windowOptions) {
	// setup defaults, if target and windowOptions comes in undefined
	if ((!target)||(target=="")) target="_blank";
	if ((!windowOptions)||(windowOptions==""))  windowOptions= 'scrollbars,status,menubar,titlebar,toolbar,location,resizable';
	window.open(url,target,'width='+w+',height='+h+','+windowOptions);
}

var cachedImages = new Array();

function PhotoViewer(stem,numberOfImages) {
	this.stem = stem; 
	this.numberOfImages = numberOfImages;
	this.currentImage = 0;
	this.imageExtension = document.images[stem].getAttribute("imageextension");
	
	this.counter = document.getElementById(stem+"counter");
	this.theImage = eval("document." + stem);
	if (numberOfImages >1) {
		this.prevButton = eval("document."+stem+"prev");
		this.nextButton = eval("document."+stem+"next");
		eval("this.nextButton.onclick = function() {pv_increment('"+this.stem+"')}");
		eval("this.prevButton.onclick = function() {pv_decrement('"+this.stem+"')}");
	}
}
PhotoViewer.prototype.zeroFill = function(num) {
	if (parseInt(num)<10) num = "0"+num;
	return num;
}
PhotoViewer.prototype.increment = function() {	
	if (this.currentImage < this.numberOfImages-1) {
		this.theImage.src = "images/projects/"+this.stem+"_"+this.zeroFill(++this.currentImage) +"." + this.imageExtension;
	}
	this.reconcileOpacities();
	this.counter.innerHTML = ""+(this.currentImage+1);
}
PhotoViewer.prototype.decrement= function() {	
	if (this.currentImage > 0 ) {
		this.theImage.src = "images/projects/"+this.stem+"_"+this.zeroFill(--this.currentImage) +"." + this.imageExtension;
	}
	this.reconcileOpacities();
	this.counter.innerHTML = ""+(this.currentImage+1);
}
PhotoViewer.prototype.reconcileOpacities = function() {
	if (this.currentImage==0) this.setOpacity("prev",60);
	else this.setOpacity("prev",99);
	if (this.currentImage==this.numberOfImages-1) this.setOpacity("next",60);
	else this.setOpacity("next",99);
}
PhotoViewer.prototype.setOpacity = function(sPrevOrNext, newOpacity) {
	if (is.ie) eval("this."+sPrevOrNext+"Button.filters.alpha.opacity = "+newOpacity+";");
	else eval("this."+sPrevOrNext+"Button.style.MozOpacity = parseFloat("+newOpacity+"/100);");
}
PhotoViewer.prototype.switchTo = function() {
	// stub
}
// global functions, just to pass through to the right object. 
function pv_increment(which) {photoViewers[which].increment();}
function pv_decrement(which) {photoViewers[which].decrement();}

// the global array of photoViewers. 
var photoViewers = new Array();

function cacheImages() {
	for (var i=0; i<document.images.length; i++) {
		var workingImage = document.images[i];
		if (workingImage.getAttribute("overimage") && workingImage.getAttribute("offimage")) {
			eval("workingImage.onmouseover = function() {this.src =cachedImages['"+workingImage.getAttribute('overimage')+"'].src;}");
			eval("workingImage.onmouseout = function() {this.src =cachedImages['"+workingImage.getAttribute('offimage')+"'].src;}");
			var index = cachedImages.length;
			cachedImages[""+workingImage.getAttribute('overimage')] = new Image(); 
			cachedImages[""+workingImage.getAttribute('overimage')].src = workingImage.getAttribute('overimage')
			cachedImages[""+workingImage.getAttribute('offimage')] = new Image(); 
			cachedImages[""+workingImage.getAttribute('offimage')].src = workingImage.getAttribute('offimage')
		}

		if (workingImage.getAttribute("numberofimages") ) {
			photoViewers[workingImage.getAttribute("name")] = new PhotoViewer(workingImage.getAttribute("name"),workingImage.getAttribute("numberofimages"));
		}
	}
}

function setupButtonRollover(elt) {
	if (elt.getAttribute("overimage") && elt.getAttribute("offimage")) {			
		eval("elt.onmouseover = function() {elt.src ='"+elt.getAttribute('overimage')+"'}");
		eval("elt.onmouseout = function() {elt.src ='"+elt.getAttribute('offimage')+"'}");
	}
}
// this one minimizablePanelManager will suffice to initialize them all. 
var minimizablePanelManager;
var experimentPanel;
var panelOffsetX = 0;
var panelOffsetY = 0;

function roll(imgName, which) {
	if (eval(imgName + "_" + which + ".src")) {
		document.images[imgName +"_image"].src = eval(imgName + "_" + which + ".src");
	}
}

function calculateOffset() {
	var im = document.getElementById("layoutMarker");
	var offsetY = 0;
	while (im.offsetParent != null) {
		offsetY += im.offsetTop;
		im = im.offsetParent;
	}


	panelOffsetY = offsetY;
	
	for (var i=0; i<TabManagers.length; i++) {
		for (var j=0; j<TabManagers[i].panelNames.length; j++) {
			TabManagers[i].resetPanelPosition(j);
		}
	}
}



function pushStateToHash() {
	var arrManagerStates = new Array();
	for (var i=0; i<TabManagers.length; i++) {
		arrManagerStates[i] = TabManagers[i].activePanel;
	}
	window.parent.frames[1].pushStateToHash(arrManagerStates);
}
var Floaters = new Array();
function setUpFloaters() {
	for (var i=0;document.getElementById("floater"+i);i++) {
		Floaters[i]= new LyrObj("floater"+i);
		// note:  attaching these two properties at runtime. They are not normally LyrObj properties
		Floaters[i].incX = (Math.random()-0.5)/2;

		Floaters[i].incY = (Math.random()-0.5)/2;

		var x = 500*(Math.random()-0.5);
		var y = 300*(Math.random()+0.2);
		Floaters[i].moveLayerTo(x,y);

	}
	floaterTimeout();
}

function floaterTimeout() {
	for (var i=0;document.getElementById("floater"+i);i++) {
		Floaters[i].moveLayerBy(Floaters[i].incX,Floaters[i].incY);
		if (Floaters[i].targetX <-600) {
			Floaters[i].x = 1200;
			Floaters[i].targetX = 1200;
		}
		if (Floaters[i].targetY <-600) {
			Floaters[i].y = 1200;
			Floaters[i].targetY = 1200;
		}
		Floaters[i].updateLayer();
	}
	setTimeout("floaterTimeout();",50);
}



var TabManagers = new Array();


if (window.parent == window.self) document.location.href = "index.php";

function init() {
	cacheImages();
	minimizablePanelManager = new MinimizablePanelManager();

	for (var i=0; document.getElementById("Set"+i+"P0Tab"); i++) {
		var numberOfPanels = 0;
		while(document.getElementById("Set"+i+"P"+ numberOfPanels+"Tab")) numberOfPanels ++;	
		TabManagers[i] = new TabbedPanelSetManager(i,numberOfPanels);
	}	

	window.parent.contentReady();
	calculateOffset();
}

window.onload=init;
window.onresize= calculateOffset;
