//Kyle's awesome library
//No stealing!

function clearDefaultText(input) {
	if(input.value == input.defaultValue) {
		input.value = '';
	}
	$(input).addClass('black').removeClass('gray');
}

function addDefaultText(input) {
	if(input.value == '') {
		input.value = input.defaultValue;
		$(input).addClass('gray').removeClass('black');
	}
}

/* GENERIC AJAX FUNCTIONS */

function ajaxFunction()
{
var xmlHttp;
try
  {
  // Firefox, Opera 8.0+, Safari
  xmlHttp=new XMLHttpRequest();
  }
catch (e)
  {
  // Internet Explorer
  try
    {
    xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    }
  catch (e)
    {
    try
      {
      xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
    catch (e)
      {
      alert("Your browser does not support AJAX!");
      return false;
      }
    }
  }

return xmlHttp;
}


function getURLParam(strParamName){
  var strReturn = "";
  var strHref = window.location.href;
  if ( strHref.indexOf("?") > -1 ){
    var strQueryString = strHref.substr(strHref.indexOf("?")).toLowerCase();
    var aQueryString = strQueryString.split("&");
    for ( var iParam = 0; iParam < aQueryString.length; iParam++ ){
      if (
aQueryString[iParam].indexOf(strParamName.toLowerCase() + "=") > -1 ){
        var aParam = aQueryString[iParam].split("=");
        strReturn = aParam[1];
        break;
      }
    }
  }
  return unescape(strReturn);
}


/* GENERIC MODAL FUNCTIONS */

//this function creates a modal dialogue with the text
//given inside it
function makeModal(text, type) {
	var overlay = document.getElementById('overlay');
	var modalBox = document.getElementById('modalBox');
	var modalStyle = modalBox.getElementsByTagName('div')[0];
	var info = document.getElementById('modaltext');
	var close = document.getElementById('close');

	//make all other elements unusable by using overlay
	setOverlaySize(overlay);
	overlay.style.display = 'block';

	//decide the style of the closebox
	setModalBoxType(type, overlay, modalStyle);

	//make modal box appear and add the information
	info.innerHTML = text;
	modalStyle.appendChild(info);

	modalBox.style.display = 'block';

	//move modalBox to the center
	centerDiv(modalBox);

	close.href = "JavaScript:closeModal('modaltext')";

}


//this function closes a modal dialogue given the id of the object it holds
function closeModal(id) {
	var modalBox = document.getElementById('modalBox');

	//if there was a child, remove it
	if(id!=null) {
		var child = document.getElementById(id);
		document.getElementById('hideMe').appendChild(child);
	}

	//close the overlay
	document.getElementById('overlay').style.display = 'none';

	//close the dialogue
	modalBox.style.display = 'none';
}


function closeModal() {
	//close the overlay
	document.getElementById('overlay').style.display = 'none';

	//close the dialogue
	document.getElementById('modalBox').style.display = 'none';
}


//this function uses ajax to grab html from a script and
//display it in a modal dialogue of the given type
function ajaxModal(script, type) {
	//get the ajax object
	var ajax = ajaxFunction();

	if(ajax == null) {
		return;
	}

	ajax.onreadystatechange=function() {

		if(ajax.readyState==1) {
			//display overlay
			var over = document.getElementById('overlay');
			setOverlaySize(over);
			over.style.display = 'block';
		}
		else if(ajax.readyState==4) {
			//display the dialogue
			var modalBox = document.getElementById('modalBox');
			var modalStyle = modalBox.getElementsByTagName('div')[0];
			var innerBox = document.getElementById('modaltext');

			//decide the style of the closebox
			setModalBoxType(type, document.getElementById('overlay'), modalStyle)
	
			innerBox.innerHTML = ajax.responseText;
			
			modalStyle.appendChild(innerBox);

			modalBox.style.display = 'block';

			//move to the center
			centerDiv(modalBox);

			document.getElementById('close').href = "javascript:closeModal('modaltext')";
		}
	}

	ajax.open("GET",script,true);
	ajax.send(null);
}


//this function changes the inner html of the modal
//dialogue box with ajax given a script and the name
//of the inner box (modal1)
function ajaxChangeModal(script, boxId) {
	//get the ajax object
	var ajax = ajaxFunction();

	if(ajax == null) {
		return;
	}

	ajax.onreadystatechange=function() {
		if(ajax.readyState==4) {
			//display the dialogue
			document.getElementById(boxId).innerHTML = ajax.responseText;
			centerDiv(document.getElementById('modalBox'));
		}
	}

	ajax.open("GET",script,true);
	ajax.send(null);
}


//this function sets a div to be in the center depending
//on its size
function centerDiv(div) {
	//set modal box size
	var arrayPageSize = getPageSize();
	var arrayPageScroll = getPageScroll();

	div.style.top = (arrayPageScroll[1] + ((arrayPageSize[3] - 4 - div.offsetHeight) / 2) + 'px');
	div.style.left = (((arrayPageSize[0] - 4 - div.offsetWidth) / 2) + 'px');
}


//this function sets the overlay size to the full size of the page
function setOverlaySize(overlay) {
	var pageSize = getPageSize();

	overlay.style.width = (pageSize[2]) + 'px';
	overlay.style.height = (pageSize[1]) + 'px';
}


//this function sets the modal box type
function setModalBoxType(type, overlay, modal) {
	switch (type) {
		//normal
		case 1:
			modal.setAttribute("id",'modalDefault');
			overlay.onclick = function() {closeModal(null)}
			break;
		//warning
		case 2:
			modal.setAttribute("id",'modalWarning');
			overlay.onclick = function() {closeModal(null)}
			break;
		//district
		case 3:
			break;

		default:
			modal.setAttribute("id",'modalDefault');
			break;
	}
}

//this function sets the close box type
function setCloseBoxType(type, overlay) {
	switch (type) {
		//normal
		case 1:
			document.getElementById('closeBox').setAttribute("class",'closeNormal');
			break;
		//warning
		case 2:
			document.getElementById('closeBox').setAttribute("class",'closeWarning');
			overlay.onclick = function() {closeModal(null)}
			break;
		//normal with no close
		case 3:
			document.getElementById('closeBox').setAttribute("class",'closeNone');
			break;

		default:
			document.getElementById('closeBox').setAttribute("class",'closeNormal');
			break;
	}
}


// getPageScroll()
// Returns array with x,y page scroll values.
// Core code from - quirksmode.org
//
function getPageScroll(){

	var yScroll;

	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
	} else if (document.documentElement && document.documentElement.scrollTop){	 // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
	} else if (document.body) {// all other Explorers
		yScroll = document.body.scrollTop;
	}

	arrayPageScroll = new Array('',yScroll) 
	return arrayPageScroll;
}


// Returns array with page width, height and window width, height
// Core code from - quirksmode.org
// Edit for Firefox by pHaez
//
function getPageSize(){
	
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}

	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}


//
// addLoadEvent()
// Adds event to window.onload without overwriting currently assigned onload functions.
// Function found at Simon Willison's weblog - http://simon.incutio.com/
//
function addLoadEvent(func)
{	
	var oldonload = window.onload;
	if (typeof window.onload != 'function'){
    	window.onload = func;
	} else {
		window.onload = function(){
		oldonload();
		func();
		}
	}

}


/* Animation Experiments */
var mousePos = new pos(0,0);

function drag(e, thing)
{
	var newMousePos = getMousePos(e);
	var mouseChange = new pos(newMousePos.x-mousePos.x, newMousePos.y-mousePos.y);
	thing = $(thing);
	var position = thing.position();
	var newPosition = new pos(position.left+mouseChange.x,position.top+mouseChange.y);

	thing.pos((newPosition.x<0) ? 0 : newPosition.x, (newPosition.y<0) ? 0 : newPosition.y);

	mousePos.x = newMousePos.x;
	mousePos.y = newMousePos.y;
}

function getMousePos(e)
{
	var posX;
	var posY;

	if(!e) var e = window.event;

	if(e.pageX || e.pageY) {
		posX = e.pageX;
		posY = e.pageY;
	}
	else if(e.clientX || e.clientY) {
		posX = e.clientX + document.body.scrollLeft
			+ document.documentElement.scrollLeft;
		posY = e.clientY + document.body.scrollTop
			+ document.documentElement.scrollTop;
	}

	return new pos(posX,posY);
}

function setMousePos(e)
{
	var newPos = getMousePos(e);

	mousePos.x = newPos.x;
	mousePos.y = newPos.y;
}

//change the selected objects' positions to the given coordinates
$.fn.pos = function(x, y)
{
	return $(this).css({left: x, top: y});
}

function pos(x, y)
{
	this.x = x;
	this.y = y;
}

		
