onLoadHash = false;

var S_TempHash;
var A_ImageResults;
var A_Queue = new Array();
var I_Queue = 0;
var O_Interval;
var S_OnLoad = "";

// AJAX Functions & Variables
///////////////////////////////////////////

	// Create AJAX Request Object
		
		var XMLHTTPREQUEST_MS_PROGIDS = new Array(
		  "Msxml2.XMLHTTP.7.0",
		  "Msxml2.XMLHTTP.6.0",
		  "Msxml2.XMLHTTP.5.0",
		  "Msxml2.XMLHTTP.4.0",
		  "MSXML2.XMLHTTP.3.0",
		  "MSXML2.XMLHTTP",
		  "Microsoft.XMLHTTP"
		);

		function createRequestObj() {
			var httpRequest = null;
			
			// Create the appropriate HttpRequest object for the browser.
			if (window.XMLHttpRequest != null) {
				httpRequest = new XMLHttpRequest();
			} else if (window.ActiveXObject != null) {
				// Must be IE, find the right ActiveXObject.
				var success = false;
				for (var i = 0; i < XMLHTTPREQUEST_MS_PROGIDS.length && !success; i++) {
					try {
						httpRequest = new ActiveXObject(XMLHTTPREQUEST_MS_PROGIDS[i]);
						success = true;
					}
					catch (ex){
						
					}
				}
			}

			// Display an error if we couldn't create one.
			if (httpRequest == null) {
			
			}
			return httpRequest;
		}
		
	// Set Variable Equal to Request Object
		var contentOBJ = createRequestObj();
		
///////////////////////////////////////////

		function GetSource(cURL) {
			//alert("Show Source");
			contentOBJ.open("get", cURL, true);
			contentOBJ.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
			contentOBJ.onreadystatechange = DisplaySource;
			contentOBJ.send(null);
		}

		function DisplaySource() {
			if (contentOBJ.readyState == 4) {
				var S_PageSource = contentOBJ.responseText;
				
				GatherImages(S_PageSource);
			}		
		}		

// RegExp
///////////////////////////////////////////

function GatherImages(S_Content) {
	
	S_Expression = /<noscript id=\"\/(.*)\" class=\"noscript\">(.*)<\/noscript>/gi;
	A_ImageResults = S_Content.match(S_Expression);
	//alert(A_ImageResults);
}

///////////////////////////////////////////

function HashControls() {

	if (window) {
		if (window.location.hash != "" && window.location.hash != "#/" && window.location.hash != "#/View" && window.location.hash != "#/View/") {
			
			// Grab the Value after the Hash
			S_TempHash = window.location.hash.substr(1, window.location.hash.length-window.location.hash.indexOf("#"));
			onLoadHash = S_TempHash.replace(/\/View/, "/Project");
			
			if(onLoadHash == "/"){
				onLoadHash = false;
			}			
		}
	}

}

// PROJECTS Listener and Toggle
///////////////////////////////////////////

function ProjectControls() {

	var pg_links = document.getElementById("projects").getElementsByTagName("a");
	var I_MoreInfoLinks = document.getElementById("moreinfo").getElementsByTagName("a");
	var pg_link_count = (pg_links.length - I_MoreInfoLinks.length);


	// Loops Thru Links - Add listener - Check For Hash to Toggle
	for(a=0; a < pg_link_count; a++){
		
		//Set positoin from array
		pg_links[a].img = a;

		// Toggle When hash is Equal to ID of List Item
		if (pg_links[a].parentNode.parentNode.getAttribute("id").indexOf(onLoadHash) >= 0 && onLoadHash) {
			toggleView(pg_links[a], true);
		}
	
		// Add Click Listener to Link
	
		pg_links[a].onclick = function(){
			toggleView(this, false);
			this.blur();
			return false;
		}
	}
}


// Toggles Project Display & Set HASH

function toggleView(link, onLoad){
		
	var content_Wrapper;
	
	if(link.getAttribute("href").indexOf("#/More") >= 0){
	
		// Get More Toggle Container
	
 		content_Wrapper = link.parentNode.parentNode.getElementsByTagName("div")[0];
	
	}else{
		
		// Get Project Toggle Container	
	
		content_Wrapper = link.parentNode.parentNode.getElementsByTagName("p")[0];
	
	}
	
	
	if (content_Wrapper.style.display != "block"){
	
		// Display Project
	
		content_Wrapper.style.display = "block";
		link.className = "on";
		
		// Fix Hash
		S_TempHash = link.getAttribute("href").substr(link.getAttribute("href").indexOf("#"), link.getAttribute("href").length);
		location.hash = S_TempHash.replace(/#\/Project/, "#/View");
		
		// Check if Onload Hash Exist and Set for user later on
		if (onLoad){
			S_OnLoad = link.getAttribute("href").substr(link.getAttribute("href").indexOf("#"), link.getAttribute("href").length);
		}
		
		Images_Wrapper = link.parentNode.parentNode.getElementsByTagName("p")[0];
		
		if (A_ImageResults == undefined) {
			// Add to queue to load
			A_Queue[I_Queue] = new Array(link.img, Images_Wrapper);
			I_Queue++;
			
			if (O_Interval == undefined) {
				O_Interval = window.setInterval("ProcessQueue()", 500);
			}
		} else {
				
			Images_HTML = Images_Wrapper.innerHTML;
		
			// CHECK For noscript or display images
			if (Images_HTML.match(/<noscript /i) || Images_HTML == "") {
				AddImgHTML(link.img, Images_Wrapper);
			}
		}
		
	} else {
	
		// Hide Project
		content_Wrapper.style.display = "none";
		link.className = "";

		// Cancel The Hash
		location.hash = "/";

	}
	
}

function AddImgHTML(PhotoID, O_Wrapper) {
	
	var S_ImgSource = A_ImageResults[PhotoID];

	// Remove Noscript
	S_ExpressionBegain = /<noscript id=\"\/(.*)\" class=\"noscript\">/gi;
	S_ExpressionEnd = /<\/noscript>/gi;
	S_ImgSource = S_ImgSource.replace(S_ExpressionBegain, "");
	S_ImgSource = S_ImgSource.replace(S_ExpressionEnd, "");
	
	Images_HTML = O_Wrapper.innerHTML.toLowerCase();
	
	// CHECK For noscript or display images
	if (Images_HTML.match(/<noscript /i) || Images_HTML == "") {
		O_Wrapper.innerHTML = S_ImgSource;
	}
	
	// Anchor fix for Onload
	if (S_OnLoad != "") {
		location.hash = S_OnLoad;
		S_OnLoad = "";
	}
}

function ProcessQueue() {
	if (A_ImageResults != undefined) {
		window.clearInterval(O_Interval);
		for (var i = 0; i<A_Queue.length; i++) {
			AddImgHTML(A_Queue[i][0], A_Queue[i][1]);
		}
	}
}

// MAILING LIST Form Control
/////////////////////////////////////////// 

var I_email;
var I_email_default_value;

function MailingListControls() {

	I_email = document.getElementById("email");
	I_email_default_value = "Join Our Email List";

	I_email.onfocus = function(){
		if(this.value == I_email_default_value){
			this.value = "";		
		}
	}

	I_email.onblur = function(){
		if(this.value == ""){
			this.value = I_email_default_value;
		}
	}

}
/*	This changes the state if typed in
I_email.onkeyup = function(){
	if(this.value != ""){
		I_email.className = "filled";
	}else{
		I_email.className = "";
	}
}
*/

function loadHtml() {
	GetSource("/");
}

// Add to DOM
addDOMLoadEvent(HashControls);
addDOMLoadEvent(ProjectControls);
addDOMLoadEvent(MailingListControls);
addDOMLoadEvent(loadHtml);
