var d = window.document;

//////////////////////////////////////
//  GENERIC RETURN ELEMENT FUNCTION //
//////////////////////////////////////

function getE( v ) {
  // e is for element!
  e = false;
  
  if ( d.getElementById ) {
    e = d.getElementById( v );
  }
  else if ( d.all ) {
    e = d.all[ v ];
  }

  return e;
}

/*
	AJAX BASE FUNCTIONALITY
*/

var xmlhttp = null;
var displayDiv = "";
var nextFunc = "";

////////////////////////////////
//	BASIC AJAX LOADER					//
////////////////////////////////

function getPage( url, second ) {

	if ( second != false ) nextFunc = second;
	else nextFunc = "";
	
	xmlhttp=null;

	if (window.XMLHttpRequest) {// code for all new browsers
  	xmlhttp=new XMLHttpRequest();
  }
	else if (window.ActiveXObject) {// code for IE5 and IE6
  	xmlhttp=new ActiveXObject( "Microsoft.XMLHTTP" );
  }
	if (xmlhttp!=null) {
		xmlhttp.onreadystatechange=function() {
			stateChange(nextFunc)
		};
		xmlhttp.open( "GET", url, true );
		xmlhttp.send( null );
  }
}

////////////////////////////////
//	EVENT HANDLER FOR HTTP		//
//	STATE CHANGE							//
////////////////////////////////

function stateChange(nextFunc) {
	e = getE( displayDiv );
	
	if (xmlhttp.readyState==4) {// 4 = "loaded"
		if (xmlhttp.status==200) {// 200 = OK
// 			alert(xmlhttp.responseText);
			e.innerHTML = xmlhttp.responseText;
			if ( nextFunc != "" ) eval( nextFunc );
		}
	}
}


	/* ---------------------- Pre-Loader for the Galleries ------------------------- */
	
function fadeOut(id) {
// 	var id = e.getAttribute("id");
	opacity(id, 100, 0, 115);
// 	alert(id);
}

function fadeIn(id) {
// 	var container = document.getElementById("content").style;
// 	container.backgroundImage = "none";
	
// 	var id = e.getAttribute("id");
	opacity(id, 1, 100, 115);
}




function opacity(id, startAlpha, endAlpha, milliseconds) {
	var speed = Math.round(milliseconds / 100);
	var timer = 0;
	
	if (startAlpha > endAlpha) {
		for (i=startAlpha; i>=endAlpha; i--) {
			setTimeout("changeOpacity('" + i + "', '" + id + "')", (timer*speed));
			timer++;
		}
	} else if (startAlpha < endAlpha) {
		for (i=startAlpha; i<=endAlpha; i++) {
			setTimeout("changeOpacity('" + i + "', '" + id + "')", (timer*speed));
			timer++;
		}
	}
}

function changeOpacity(newOpacity, id) {
	var object = document.getElementById(id).style;
	object.opacity = (newOpacity/100);
	object.filter = "alpha(opacity="+newOpacity+")";
	
// 	if (object.opacity == 0) {
// 		var container = document.getElementById("content").style;
// 		container.backgroundImage = "url('/_img/preloader.gif')";
// 		container.backgroundRepeat = "no-repeat";
// 		container.backgroundPosition = "center center";
// 	}
}




////////////////////////////
// CHECK ( checkboxes )
///////////////////////////
var checkflag = false;

function check(f) {
  if (checkflag == false) {
      for (i = 0; i < f.length; i++) {
          if (f[i].name != "Undecided" ) {
              f[i].checked = true;
          }
      }
      
      checkflag = "true";
      return "Uncheck All";     }
  
  else {
      for (i = 0; i < f.length; i++) {
          if (f[i].name != "Undecided" ) {
              f[i].checked = false;             }
      }
  
      checkflag = false;
      return "Check All";     }
}


	
	/* ----------------------------- Ask Kate Pop Question ----------------------------- */
	
// Why is this here?
	
// function popQuestion( clickTarget, popID, totalNumber ) {
// 	/* ---------- Set Up the Variables (Thanks IE) -------------- */
// 	var divs = document.getElementsByTagName("div");
// 	var questions = new Array();
// 	var answers = new Array();
// 	
// 	for (i=0; i<divs.length; i++) {
// 		if (divs[i].className == "kateQuestion") {
// 			questions.push(divs[i]);
// 		}
// 		
// 		if (divs[i].className == "kateAnswer") {
// 			answers.push(divs[i]);
// 		}
// 	}
// 	
// 	
// 	/* ------- Clear all the Answer Divs -------- */
// 	for (i=0; i<answers.length; i++) {
// 		answers[i].style.display = "none";
// 	}
// 	
// 	/* ------- Reset the Defualt Styles for all Questions ------ */
// 	for (i=0; i<questions.length; i++) {
// 		questions[i].style.borderLeft = "none";
// 		questions[i].style.borderRight = "none";
// 		questions[i].style.borderBottom = "2px solid #B3E5E4";
// 	
// 		if (i==0) {
// 			questions[i].style.borderTop = "none";
// 		}
// 	}
// 	
// 	/* ----------- Show the Appropriate Answer ------------------ */
// 	displayDiv = document.getElementById(popID).style;
// 	displayDiv.display = "block";
// 	
// 	/* ----------- Set the Styles to highlight the active element ------------- */
// 	clickTarget.style.borderLeft = "2px solid #B3E5E4";	
// 	clickTarget.style.borderRight = "2px solid #B3E5E4";
// 	clickTarget.style.borderBottom = "none";
// 	
// 	if (clickTarget.getAttribute("id") == questions[0].getAttribute("id")) {
// 		clickTarget.style.borderTop = "2px solid #B3E5E4";
// 	}
// }
// 



	/* ----------------- Accordian Function for the Timeline ------------------ */
	
function popTimeline(clickTarget) {
	/* ----------- First Hide All of the Accordian Divs ----------------- */
	var divs = document.getElementsByTagName("div");
	var tDetails = new Array();
	
	for (i=0; i<divs.length; i++) {
		if (divs[i].className == "timelineDetails") {
			tDetails.push(divs[i]);
		}
	}
	
	for (i=0; i<tDetails.length; i++) {
		tDetails[i].style.display = "none";
	}
	
	/* ------- Get the Div we want to Show ------- */
	divToShow = clickTarget.parentNode.nextSibling;
	
	/* -------- FireFox Treats the empty carraige return as a node, so get the next one ------- */
	if (divToShow.nodeType != 1) {
		divToShow = clickTarget.parentNode.nextSibling.nextSibling;
	}
	
	/* ---------- Pop It --------------- */
	divToShow.style.display = "block";
}




	/* ------------------ Function to call ActionScript function inside the Flash Gallery --------------------------- */
	
	
function getMovie(movieName) {
		if(navigator.appName.indexOf("Microsoft") !== -1)
		{
				return window[movieName];
		}
		else if(navigator.userAgent.indexOf("Safari") !== -1)
		{
				return document.getElementById(movieName);
		}
		else
		{
				if(document[movieName].length != undefined)
				{
						return document[movieName][1];
				}
				return document[movieName];
		}
}


	/* --- Submit the Form --- */
	
function talkToFlash(formID) {
	var thisForm = document.getElementById(formID);

	getMovie("flashGallery").reload(thisForm.searchField.value, thisForm.color.value, thisForm.shape.value, thisForm.theme.value, thisForm.environment.value);
}

function talkToFlash_New(formID, selectElement) {
	var thisForm = document.getElementById(formID);

	getMovie("flashGallery").reloadNew(thisForm.searchField.value, thisForm.color.value, thisForm.shape.value, thisForm.theme.value, thisForm.environment.value, thisForm.galleryChoice.value);
	
	highlightSelectedChoices(selectElement);
}

function highlightSelectedChoices(selectElement) {
	if(selectElement.tagName != "INPUT" && selectElement.tagName != "FORM") {
		if(selectElement.value != "") {
			selectElement.style.backgroundColor = "#3C2415";
			selectElement.style.color = "white";
		} else {
			selectElement.style.backgroundColor = "white";
			selectElement.style.color = "#3C2415";
		}
	}
}

	/* --------------------------------------------------- */

function showDefaultGallery(formID) {
	var thisForm = document.getElementById(formID);
	for (i=1; i<thisForm.length - 1; i++) {
		if(thisForm[i].name != "newSearch") {
			thisForm[i].value = "";
		}
	}
	
	getMovie("flashGallery").reload(false, false, false, false, false);
}

function showDefaultGallery_New(formID) {
	var thisForm = document.getElementById(formID);
	for (i=0; i<thisForm.length; i++) {
		if(thisForm[i].name != "newSearch") {
			thisForm[i].value = "";
			thisForm[i].style.backgroundColor = "white";
			thisForm[i].style.color = "#3C2415";
		}
	}
	
	getMovie("flashGallery").reloadNew(false, false, false, false, false, "");
}

	/* --------------------------------------------------- */

function showNewImages(formID) {
	var thisForm = document.getElementById(formID);
	for (i=1; i<thisForm.length - 1; i++) {
		thisForm[i].value = "";
	}
	
	getMovie("flashGallery").reload(false, false, false, false, false, true);
}



////////////////////////////////
//	CHECK EMAIL QUALITY				//
////////////////////////////////

function checkEmailGood(emailFieldID) {
	e = getE( emailFieldID );
	email = e.value;
	reg = /\@[A-Za-z0-9\-]{2,}\.[A-Za-z0-9\-]{2,}/;
	if ( !email.match( reg ) ) {
		e.style.backgroundColor = "#FFD0D0";
		e.style.border = "1px solid red";
		return false;
	}
	else {
		e.style.border = "1px solid #ffffff";
		e.style.backgroundColor = "#FFF";
		return true;
	}
}


	// ------------------------- new Convenience Functions for Light Box --------------------------- //
	
function renameLightBox(incomingID) {
	var container = document.getElementById("lightBoxTitle");
	container.innerHTML = "<form class='lightBoxTitleForm' action='' method='post' onsubmit='saveNewLightBoxName(this); return false;'><p><input class='titleInput' type='text' name='newLightBoxTitle' /><input type='hidden' name='lightBoxID' value='" + incomingID + "' /><input type='submit' value='save' /></p></form>";
	container.children[0][0].focus();
}

function saveNewLightBoxName(incomingForm) {
	var lightID = incomingForm.lightBoxID.value
	var lightTitle = incomingForm.newLightBoxTitle.value;
	if(lightTitle == "") {
		lightTitle = "Undefined";
	}
	
	popContent( "lightBoxTitle", "setLightBoxTitle.php?lightBoxID=" + lightID + "&newTitle=" + lightTitle, changeNameInList(lightTitle));
}

function changeNameInList(newTitle) {
	var savedContainer = document.getElementById("savedLightBoxes");
// 	alert(savedContainer.children.length);
	for(i=1; i<savedContainer.children.length; i++) {
		if(savedContainer.children[i].firstChild && savedContainer.children[i].firstChild.className == "active") {
			savedContainer.children[i].firstChild.innerHTML = newTitle;
		}
	}
}

	// Stuff for the email form
function toggleEmailForm() {
	var emailForm = document.getElementById("sendToFriend");
	if(emailForm.style.display == "none" || emailForm.style.display == "") {
		emailForm.style.display = "block";
	} else {
		emailForm.style.display = "none";
	}
}