var request;
var queryString; 
var target;
var outertarget;
var contentLevel;
var currentWorkThumbnailDiv;
var subnavlink;
var numsubnavlinks;
var replacediv;
var current_work_id;
var work_parent_div;
var previousdata;
var nextdata;
var jumpToProjectId;
var jumpToProjectSlide;
var featured_project_div;
var flashDelay = 10;
var emailSignupSource;

function getContent(div, subid) {
	
	contentLevel = undefined;
	url= base_url + div;
	if (subid != undefined)
		url += '/direct/' + subid;
	
	if (div.substring(0,9) == 'featured_') {
		url = url.replace('innercontent','featured_work');
		contentLevel = 'featured_work_detail';
	}
	
	outertarget = div;
	target = div + '_inner';
	//alert(url);
    httpRequest("GET",url,true);
	flashTimeout = setTimeout("sendToActionScript('" + div + "')",flashDelay); 
}

function getInnerContent(parent_target, replace_target, id, sourcelink, numsourcelinks) {
	
	contentLevel = 'inner';
	subnavlink = sourcelink;
	numsubnavlinks = numsourcelinks;
	replacediv = replace_target;
	current_work_id = id;


	url= base_url + parent_target + '/' + replacediv + '/' + id;
	//alert(url);
	outertarget = parent_target;
    httpRequest("GET",url,true);
	
}

function getProject(projectid, parentdiv, prev, next) { //categoryid, projectindex, totalresults, 
	
	if (replacediv == undefined)
		replacediv = 'work_list_main';
	contentLevel = 'work_detail';
	work_parent_div = parentdiv;
	url= base_url.replace('/innercontent','') + 'work/index/' + projectid;
	
	//alert(url);
	
	previousdata = prev;
	nextdata=next;
	
    httpRequest("GET",url,true);
	
	//alert(projectid  + ' ' + categoryid + ' ' + projectindex + ' ' + totalresults);
	
	
}
//event handler for XMLHttpRequest

function getThumbnail(targetdiv, imgtag, width, height) {
	document.getElementById(targetdiv).innerHTML = '<img src="' + imgtag + '" width="' + width + '" height="' + height + '" border="0" />';
}

function clearThumbnail(targetdiv) {
	document.getElementById(targetdiv).innerHTML = '&nbsp;';
}

function getHuh() {
	
	contentLevel = 'huh';
	url= base_url.replace('innercontent/','huh');
	
    httpRequest("GET",url,true);

		//let's also toggle the event for the main huh button in case people keep clicking it. 
		//un-observe to destroy the listeners

		Event.stopObserving('huh_button', 'click');
		var huhFunction = {h: function (event) {
			closeHuh();
			pageTracker._trackEvent('AJAXcontent', 'closeHuh', 'Home Page');
		}}
		huhFunction.bh = huhFunction.h.bindAsEventListener(huhFunction);
		Event.observe('huh_button', 'click', huhFunction.bh);
		
		
	
}

function closeHuh(stage) {
	if (stage == undefined) {
		new Effect.BlindUp('huh');
		transitionTimeout = setTimeout("closeHuh('end')",1250); 
	} else if (stage == 'end') {
		clearTimeout(transitionTimeout);
		$('huh').update();
	}

		pageTracker._trackEvent('Closing', 'closeHuh');
		Event.stopObserving('huh_button', 'click');

		var huhFunction = {h: function (event) {
			getHuh();
			pageTracker._trackEvent('AJAXcontent', 'getHuh', 'Home Page');
		}}
		huhFunction.bh = huhFunction.h.bindAsEventListener(huhFunction);
		Event.observe('huh_button', 'click', huhFunction.bh);



}


function jumpToProject(projectid, slideid) {

	outertarget = 'creative_department';
	contentLevel = 'jumpToProject';
	flashDelay = 3000;

	jumpToProjectId = projectid;
	jumpToProjectSlide = slideid;
	
	url= base_url_openDetail + projectid + '/jumpTo';
	if (slideid != '' && slideid != undefined)
		url += '/' + slideid;
		
	//alert(url);
	target = outertarget + '_inner';
    httpRequest("GET",url,true);

	flashTimeout = setTimeout("sendToActionScript('" + outertarget + "')",flashDelay); 
	pageTracker._trackEvent('Shortcuts', 'jumpToProject', projectid, slideid);
	flashDelay = 10;

}


function handleCheck(){
    if(request.readyState == 4){
        if(request.status == 200){
			//put content			
			
			if (contentLevel == 'inner') {
				//alert(request.responseText); // don't do text replace up here; wait until after slideup
				manageInnerTransition('start');
			} else if (contentLevel == 'work_detail') {
				manageWorkTransition('start');
			} else if (contentLevel == 'featured_work_detail') {
				manageFeaturedTransitions('start');
			} else if (contentLevel == 'jumpToProject') {
				manageJumpToTransition('start');
			} else if (contentLevel == 'huh') { 
				makeHuh();
			} else if (contentLevel == 'emailSignup') { 
				emailSignUp('result',emailSignupSource);
			} else {
				if (outertarget != 'home') {
					//alert('146 ' + target);
					document.getElementById(target).innerHTML = preload_clear(request.responseText);
					
				}
				//start transitions -- drop out, scroll, slide in
				manageTransitions('start');
			}
			
        } else {
            alert("A problem occurred with communicating between the XMLHttpRequest object and the server program.: " + request.status);
        }
    }//end outer if
}

function preload_clear(text) { // for getting RESTful preload info out of our responses. 
	/*
		6/8/09: now doing preload of thumbnails here, so first split response, generate preload, and then stock the divs
	*/
	
	response = request.responseText.split('|END_THUMBNAILS|');
	if (response.length > 1) {
		thumb_array = response[0].split('|');
		if (thumb_array.length > 0) {
		
			pl = new Image();
			for (i = 0; i < thumb_array.length; i++) {
				pl.src = thumb_array[i];			
			}
			//alert(thumb_array.length);
			responseInner = response[1];
			
		} //end if there's length here.
	} else { //something's wrong; just put in the inner html
		responseInner = response[0];
	}
	
	//alert(responseInner);
	try {return responseInner;} catch (e) {alert(e);}

}//end preload_clear

/* Initialize a Request object that is already constructed */
function initReq(reqType,url,bool){
    /* Specify the function that will handle the HTTP response */
    request.onreadystatechange=handleCheck;
    request.open(reqType,url,bool);
    request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
    request.send(queryString);
}

/* Wrapper function for constructing a Request object.
 Parameters:
  reqType: The HTTP request type such as GET or POST.
  url: The URL of the server program.
  asynch: Whether to send the request asynchronously or not. */
function httpRequest(reqType,url,asynch){
    //Mozilla-based browsers
    if(window.XMLHttpRequest){
        request = new XMLHttpRequest();
    } else if (window.ActiveXObject){
        request=new ActiveXObject("Msxml2.XMLHTTP");
        if (! request){
            request=new ActiveXObject("Microsoft.XMLHTTP");
        }
     }
    //the request could still be null if neither ActiveXObject
    //initializations succeeded
    if(request){
		initReq(reqType,url,asynch);
    }  else {
		alert("Your browser does not permit the use of all of this application's features.");
    }
}

function manageTransitions (stage) {
	
	if (stage == 'start') {
		//close what's open.
		
		delay = 1;
		
		if (now_open != undefined && now_open != outertarget && now_open != 'home') {
			//we're going to need to know the left offset for when we close this; otherwise dropout forces us to left 0.

			if (navigator.appName == 'Microsoft Internet Explorer') {
				xobj = document.getElementById('content_inner').getBoundingClientRect();
				document.getElementById(now_open).style.left = xobj.left;
			} else
				document.getElementById(now_open).style.left = document.getElementById('content_inner').offsetLeft + 'px';

			//new Effect.DropOut(now_open);
			//new Effect.SlideUp(now_open, {duration: 0.5});
			new Effect.Fade(now_open, {duration: 0.3});
			
			delay = 350;
			
		} // end if something's open. 

		transitionTimeout = setTimeout("manageTransitions('move')",delay); 
	
	} else if (stage == 'move') {
		clearTimeout(transitionTimeout);
		scrolltarget = outertarget + '_scroll';
		//alert(scrolltarget);
		new Effect.ScrollTo(scrolltarget, {duration: 1.5});
		
		transitionTimeout = setTimeout("manageTransitions('end')",1500); 
		
	} else if (stage == 'end') {
		clearTimeout(transitionTimeout);
			if (navigator.appName == 'Microsoft Internet Explorer') {
				xobj = document.getElementById('content_inner').getBoundingClientRect();
				document.getElementById(outertarget).style.left = xobj.left;
			} else
				document.getElementById(outertarget).style.left = document.getElementById('content_inner').offsetLeft + 'px';

		if (now_open != outertarget && outertarget != 'home')
			new Effect.SlideDown(outertarget);
	
		if (outertarget == 'home') //home is always open, so never close it
			now_open = undefined;
		else
			now_open = outertarget;
	}

} //end manage transitions
function manageInnerTransition (stage) {

	if (stage == 'start') {
		//change active links
		for (i = 0; i < numsubnavlinks; i++ ) {
			divname = 'subnav_'+ now_open + '_' + i;
			//alert(divname);
			document.getElementById(divname).className = 'subnav_item';
		}
		document.getElementById(subnavlink).className = 'subnav_item_active';

			if (navigator.appName == 'Microsoft Internet Explorer') {
				xobj = document.getElementById('content_inner').getBoundingClientRect();
				document.getElementById(now_open).style.left = xobj.left;
			} else
				document.getElementById(now_open).style.left = document.getElementById('content_inner').offsetLeft + 'px';
		//close what's open.
		try {	
			new Effect.BlindUp('work_detail');
		} catch (err) {
			//alert('235 ' + err);
		}
		//alert(replacediv);
		try { new Effect.BlindUp(replacediv); } catch (err2) {
			//alert('238 ' + err2);
		}
		transitionTimeout = setTimeout("manageInnerTransition('middle')",1000); 
		
	} else if (stage == 'middle') {

		clearTimeout(transitionTimeout);
		/*
			6/8/09: now doing preload of thumbnails here, so first split response, generate preload, and then stock the divs
		*/
		document.getElementById(replacediv).innerHTML = preload_clear(request.responseText);//second part of the response array is our content. 
		transitionTimeout = setTimeout("manageInnerTransition('end')", 50); 

	} else if (stage == 'end') {

		clearTimeout(transitionTimeout);
		new Effect.BlindDown(replacediv);
	}

} //end manage transitions

function manageWorkTransition (stage) {
	if (stage == 'start') {
		//close what's open.
		try {	
			new Effect.BlindUp('work_detail');
		} catch (err) {
			//alert('248 ' + err);
		}
		try {	
			new Effect.BlindUp(replacediv);
		} catch (err2) {}
		transitionTimeout = setTimeout("manageWorkTransition('end')",1000); 

		
	} else if (stage == 'end') {

		clearTimeout(transitionTimeout);
		try {	
			$('work_detail').remove();
		} catch (err) {}

		createWorkSlides();
	}

} //end manage work transitions


function manageFeaturedTransitions (stage) {
	if (stage == 'start') {
		//close what's open.
		
		if (now_open != undefined && now_open != outertarget) {
			//we're going to need to know the left offset for when we close this; otherwise dropout forces us to left 0.

			if (navigator.appName == 'Microsoft Internet Explorer') {
				xobj = document.getElementById('content_inner').getBoundingClientRect();
				document.getElementById(now_open).style.left = xobj.left;
			} else
				document.getElementById(now_open).style.left = document.getElementById('content_inner').offsetLeft + 'px';

			//new Effect.SlideUp(now_open, {duration: 0.5});
			new Effect.Fade(now_open, {duration: 0.3});
		} // end if something's open. 

		transitionTimeout = setTimeout("manageFeaturedTransitions('move')",750); 
	
	} else if (stage == 'move') {
		clearTimeout(transitionTimeout);
		scrolltarget = outertarget + '_scroll';
		//alert(scrolltarget);
		new Effect.ScrollTo(scrolltarget, {duration: 1.5});
		
		transitionTimeout = setTimeout("manageFeaturedTransitions('create')",1500); 

		
	} else if (stage == 'create') {

		clearTimeout(transitionTimeout);
		try {	
			$('work_detail').remove();
		} catch (err) {}

		createWorkSlides();
		transitionTimeout = setTimeout("manageFeaturedTransitions('end')",1000); 
		
	} else if (stage == 'end') {
		clearTimeout(transitionTimeout);
		new Effect.SlideDown(outertarget);
		now_open = outertarget;
	}

} //end manage featured transitions


function manageJumpToTransition (stage) {


	if (stage == 'start') {
		//close what's open.

		if (now_open != undefined) { // && now_open != outertarget
			//we're going to need to know the left offset for when we close this; otherwise dropout forces us to left 0.
			if (navigator.appName == 'Microsoft Internet Explorer') {
				xobj = document.getElementById('content_inner').getBoundingClientRect();
				document.getElementById(now_open).style.left = xobj.left;
			} else
				document.getElementById(now_open).style.left = document.getElementById('content_inner').offsetLeft + 'px';
			//new Effect.DropOut(now_open);
			//new Effect.SlideUp(now_open, {duration: 0.5});
			new Effect.Fade(now_open, {duration: 0.3});
			transitionTimeout = setTimeout("manageJumpToTransition('move')",1500); 
		} else { // end if something's open. 
			transitionTimeout = setTimeout("manageJumpToTransition('move')",50); 
		}
	
	} else if (stage == 'move') {
		clearTimeout(transitionTimeout);
		scrolltarget = outertarget + '_scroll';
		new Effect.ScrollTo(scrolltarget, {duration: 1.5});

		$('creative_department_inner').update(''); //this gets rid of our extra space before the nav. 
		
		//let's create this inner content.
		try {	
			$('work_detail').remove();
		} catch (err) {}
		
		//work list main may already exist, so remove...
		try {	
			$('work_list_main').remove();
		} catch (err) {}
		try {	
			$('work_list_main').remove(); //may exist twice
		} catch (err) {}
		
		try {	
			$('subnav_creative_department').remove(); 
		} catch (err) {}
		
		
		//now create the nav, hidden.

		nav_categories = request.responseXML.getElementsByTagName('work_category');
		nav_projects = request.responseXML.getElementsByTagName('project_nav');
		
		
		var nav_div = new Element('div', {'id' : 'subnav_creative_department', 'class' : 'subnav'});
		$('creative_department_inner').insert(nav_div);

		var work_list_div = new Element('div', {'id' : 'work_list_main'}).hide(); //the whole list gets hidden.
		$('creative_department_inner').insert(work_list_div);

		var work_list_left = new Element('div', {'class' : 'work_list_left', 'id' : 'content_left_creative_department'}); 
		$(work_list_div).insert(work_list_left);

		var work_list_right = new Element('div', {'class' : 'work_list_right', 'id' : 'content_right_creative_department'});
		$(work_list_div).insert(work_list_right);

		subnav_items = new Array();
		subnav_links = new Array();

		//links
		for (var i = 0; i < nav_categories.length; i++) {
			
			category_id = getNodeValue(nav_categories[i],'category_id');
			category_name = getNodeValue(nav_categories[i],'category_name');
			category_selected = getNodeValue(nav_categories[i],'category_selected');
			
			if (category_selected == 'Yes') {
				navclass = 'subnav_item_active';
				navtitle = 'this is our ' + category_name + ' work';
			} else
				navclass = 'subnav_item';
			
				linktitle = category_name + ' work';

			subnav_items[i] = new Element('div', {'id' : 'subnav_creative_department_' + i, 'class' : navclass});
			$(nav_div).insert(subnav_items[i]);
			
			subnav_links[i] = new Element('div', {'class' : 'subnav_item_inner', 'onClick' : "getInnerContent('creative_department', 'work_list_main', "+category_id+", 'subnav_creative_department_" + i + "', 3)"}).update(linktitle); //we'll see if that number matters. 
			$(subnav_items[i]).insert(subnav_links[i]);
			
		} //  end category
		
		identities_div = new Element('div', {'id' : 'identities_button'}).update('&nbsp;');
		$(nav_div).insert(identities_div);
		

		sn_function_print = {snp: function (event) {
			getInnerContent('creative_department', 'work_list_main', 1, 'subnav_creative_department_0', 3);
		}}
		sn_function_print.snp = sn_function_print.snp.bindAsEventListener(sn_function_print);
		Event.observe('subnav_creative_department_0', 'click', sn_function_print.snp);

		sn_function_web = {snw: function (event) {
			getInnerContent('creative_department', 'work_list_main', 2, 'subnav_creative_department_1', 3);
		}}
		sn_function_web.snw = sn_function_web.snw.bindAsEventListener(sn_function_web);
		Event.observe('subnav_creative_department_1', 'click', sn_function_web.snw);

		sn_function_motion = {snm: function (event) {
			getInnerContent('creative_department', 'work_list_main', 3, 'subnav_creative_department_2', 3);
		}}
		sn_function_motion.snm = sn_function_motion.snm.bindAsEventListener(sn_function_motion);
		Event.observe('subnav_creative_department_2', 'click', sn_function_motion.snm);
		
		
		sn_function_identities = {sni: function (event) {
			getContent('featured_identities');
			pageTracker._trackEvent('CrossLink', 'Identities', 'From jumpTo ' + navtitle );
		}}
		sn_function_identities.sni = sn_function_identities.sni.bindAsEventListener(sn_function_identities);
		Event.observe('identities_button', 'click', sn_function_identities.sni);

		
		
		//put in the title.
		
		var projectnavtitle = new Element('div', {'class' : 'title'}).update(navtitle);
		$(work_list_left).insert(projectnavtitle);
		
		
		projectnav_items = new Array();
		projectnav_inner = new Array();
		projectnav_thumb = new Array();
		projectnav_thumb_img = new Array();
		
		//project list if we're in the current category.
		for (var i = 0; i < nav_projects.length; i++) {
			project_nav_id = getNodeValue(nav_projects[i],'project_nav_id');
			project_nav_name = getNodeValue(nav_projects[i],'project_nav_name');
			project_thumbnail = getNodeValue(nav_projects[i],'project_thumbnail');
			project_thumbnail_width = getNodeValue(nav_projects[i],'project_thumbnail_width');
			project_thumbnail_height = getNodeValue(nav_projects[i],'project_thumbnail_height');

			projectnav_items[i] = new Element('div', {'class' : 'work_list_item', 'onmouseover' : "getThumbnail('work_list_thumb_"+project_nav_id+"','"+project_thumbnail+"', "+project_thumbnail_width+", "+project_thumbnail_height+")", 'onmouseout' : "clearThumbnail('work_list_thumb_"+project_nav_id+"')", 'onclick' : "getProject("+project_nav_id+", 'creative_department')"});
			$(work_list_left).insert(projectnav_items[i]);
			
			projectnav_inner[i] = new Element('div', {'class' : 'work_list_inner'}).update(project_nav_name);
			$(projectnav_items[i]).insert(projectnav_inner[i]);
			
			projectnav_thumb[i] = new Element('div', {'id' : 'work_list_thumb_' + project_nav_id, 'class' : 'work_list_item_thumbnail'}).update('&nbsp;');
			$(work_list_right).insert(projectnav_thumb[i]);
			
			projectnav_thumb_img[i] = project_thumbnail;

		} // end project nav
		
		//create thumbnails as images in the dom
		
		pl = new Image();
		for (i = 0; i < projectnav_thumb_img.length; i++) {
			pl.src = projectnav_thumb_img[i];			
		}
		
		//make the project slides
		createWorkSlides(jumpToProjectSlide);



		transitionTimeout = setTimeout("manageJumpToTransition('end')",1500); 
		
	} else if (stage == 'end') {
		clearTimeout(transitionTimeout);
		
			if (navigator.appName == 'Microsoft Internet Explorer') {
				xobj = document.getElementById('content_inner').getBoundingClientRect();
				document.getElementById(outertarget).style.left = xobj.left;
			} else
				document.getElementById(outertarget).style.left = document.getElementById('content_inner').offsetLeft + 'px';

		//if (now_open != outertarget)
		new Effect.SlideDown(outertarget);
	
		now_open = outertarget;

	} // end "end"



} //end manage transitions




function closeSlides (stage) {

	if (stage == 'start') {
		new Effect.BlindUp('work_detail');
		transitionTimeout = setTimeout("closeSlides('end')",1000); 
	} else {
		clearTimeout(transitionTimeout);
		new Effect.BlindDown('work_list_main');
	}

	//alert('end of function');
}

function createWorkSlides (advanceTo) {

	//alert(request.getAllResponseHeaders());

	//alert(request.responseXML);
	//alert(request.responseText);
	//slides = request.responseText.split('\n'); //gives us the number of slides for this project

	project_details = request.responseXML.getElementsByTagName('project_details');
	
	slides = request.responseXML.getElementsByTagName('slide');
	
	//let's make a container for the slides: 

	var outer_div = new Element('div', {'id' : 'work_detail', 'class' : 'work_detail'}).hide();

	$(target).insert(outer_div);

	var safariFlashBug = 'no';

	//alert(target);
	var project_project;
	var project_client;
	var project_text;
	var project_url;
	var project_rightlinks;
	var project_vitals;
	var project_vitals_header;
	var project_vitals_footer;
	var project_vitals_body;
	var project_vitals_inner;
	var project_commentary;
	var project_behind_the_scenes;
	var project_category_id;

	//now make slides themselves

	slide = new Array();
	//slide_close = new Array();
	project_image = new Array();
	project_image_caption = new Array();
	project_image_caption_inner = new Array();
	slide_navigation = new Array();
	//slide_prev = new Array();
	//slide_next = new Array();
	nextlink = new Array();
	prevlink = new Array();
	
	
	//make information here. 

	
	for (var i = 0; i < project_details.length; i++) {
		
		project_name = getNodeValue(project_details[i],'project_name');
		project_intro_text = getNodeValue(project_details[i],'project_intro_text');
		project_body = getNodeValue(project_details[i],'project_body');
		project_url = getNodeValue(project_details[i],'project_url');
		project_category_id = getNodeValue(project_details[i],'project_category_id');
		behind_the_scenes_file = getNodeValue(project_details[i],'behind_the_scenes_file');
		commentary_file = getNodeValue(project_details[i],'commentary_file');
		vitals = getNodeValue(project_details[i],'vitals');
		client = getNodeValue(project_details[i],'client');
		client_body = getNodeValue(project_details[i],'client_body');
		previousdata = getNodeValue(project_details[i],'previous_link');
		nextdata = getNodeValue(project_details[i],'next_link');
	
		//process anything that needs special treatment
		
		
	
	}
	
	
//	slide_close = new Element('div', {'class' : 'slide_close', 'onclick' : 'closeSlides(\'start\')'}); //need to add onclick for close script.

	if (contentLevel == 'featured_work_detail') {
	
	} else if (contentLevel == 'jumpToProject' && strpos(navigator.userAgent, 'MSIE') > 0){
	
		slide_close = new Element('div', {'class' : 'slide_close_invisible'}); 
		$(outer_div).insert(slide_close);
	
	} else {
		slide_close = new Element('a', {'class' : 'slide_close'}); 
		$(slide_close).observe('mousedown', function(event){ 
			$(slide_close).removeClassName('slide_close');
			$(slide_close).toggleClassName('slide_close_click');
			closeSlides('start');
		}); 
		$(outer_div).insert(slide_close);
	}

	slide_container = new Element('div', {'class' : 'slide_container'}); 
	$(outer_div).insert(slide_container);
	
	project_project = new Element('div', {'class' : 'slide_project'}).update(project_name); // title
	$(outer_div).insert(project_project);

	//by default we're going to do slide 0, so here's our initial state of linkage: 

	slide_navigation = new Element('div', {'class' : 'slide_navigation'}); 
	$(outer_div).insert(slide_navigation);

	slide_prev = new Element('a', {'class' : 'slide_prev', 'id' : 'slide_nav_prev'}); 
	$(slide_navigation).insert(slide_prev);

	slide_next = new Element('a', {'class' : 'slide_next', 'id' : 'slide_nav_next'}); 
	$(slide_navigation).insert(slide_next);


	project_client = new Element('div', {'class' : 'slide_client'}).update('Client: ' + client); 
	$(outer_div).insert(project_client);

	try {
		project_text = new Element('div', {'class' : 'slide_text'}).update(project_body.replace('\\','')); //clean backslashes
	} catch (e) { //alert(e);
	};
	$(outer_div).insert(project_text);


	//let's do the right sidebar
	
	slide_rightlinks = new Element('div', {'class' : 'slide_rightlinks'}); 
	$(outer_div).insert(slide_rightlinks);
	
	if (vitals.length > 0) {
		slide_vitals = new Element('div', {'class' : 'slide_vitals'}); 
		$(slide_rightlinks).insert(slide_vitals);
		
		slide_vitals_header = new Element('div', {'class' : 'slide_vitals_header'}); 
		$(slide_vitals).insert(slide_vitals_header);
		
		slide_vitals_body = new Element('div', {'class' : 'slide_vitals_body'}); 
		$(slide_vitals).insert(slide_vitals_body);
		
		slide_vitals_footer = new Element('div', {'class' : 'slide_vitals_footer'}); 
		$(slide_vitals).insert(slide_vitals_footer);
		
		slide_vitals_inner = new Element('div', {'class' : 'slide_vitals_inner'}).update(vitals); 
		$(slide_vitals_body).insert(slide_vitals_inner);

	} 
	
	if (behind_the_scenes_file.length > 0) {
		slide_behind_the_scenes = new Element('div', {'class' : 'slide_behind_the_scenes'}).update(behind_the_scenes_file); 
		$(slide_rightlinks).insert(slide_behind_the_scenes);
		
	}
	
	if (commentary_file.length > 0) {
		slide_commentary = new Element('div', {'class' : 'slide_commentary'}).update(commentary_file); 
		$(slide_rightlinks).insert(slide_commentary);
		
	}
	
	if (project_url.length > 0) {
		//make link
		linktext = '<a href="'+ project_url +'" target="_blank">Launch Site</a>';
		project_url = new Element('div', {'class' : 'slide_url'}).update(linktext); 
		$(slide_rightlinks).insert(project_url);
	}

		

	for (var i = 0; i < slides.length; i++) {
	
		//make a container for this div
		/*
		//if doing rest:
		
		media_id = data[0];
		media_file = data[1];
		directory = data[2];
		media_width = data[3];
		media_height = data[4];
		media_body = data[5];
		project_name = data[6];
		project_intro_text = data[7];
		project_body = data[8];
		project_url = data[9];
		behind_the_scenes_file = data[10];
		commentary_file = data[11];
		vitals = data[12];
		client = data[13];
		client_body = data[14];
		previousdata = data[15];
		nextdata = data[16];


		
		*/
		slide[i] = new Element('div', {'id' : 'slide_' + i, 'class' : 'work_slide'}).hide(); //do all the rest with this hidden. 
		$(slide_container).insert(slide[i]);
		media_id = getNodeValue(slides[i],'media_id');
		media_file = getNodeValue(slides[i],'media_file');
		media_thumbnail = getNodeValue(slides[i],'media_thumbnail');
		media_width = getNodeValue(slides[i],'media_width');
		media_height = getNodeValue(slides[i],'media_height');
		media_type = getNodeValue(slides[i],'media_type');
		media_body = getNodeValue(slides[i],'media_body');
		
		
		//determine i of next and prev links. 

		nexti = i + 1;
		previ = i - 1;
		
		//assemble end next/prev from the xml
		if (nexti >= slides.length && nextdata.length > 0)
			nextlink[i] = nextdata;
		else if (nexti >= slides.length) //this might be a featured project, so just wrap around.
			nextlink[i] = 'gotoSlide(0, '+ i + ')';
		else 
			nextlink[i] = 'gotoSlide('+ nexti +', '+ i + ')';
			
		if (previ < 0 && previousdata.length > 0)
			prevlink[i] = previousdata;
		else if (previ < 0) //this might be a featured project, so just wrap around.
			prevlink[i] = 'gotoSlide('+ (slides.length - 1) +', '+ i + ')';
		else 
			prevlink[i] = 'gotoSlide('+ previ +', '+ i + ')';

		swfVersion = GetSwfVer();

		if (media_type.substr(0,5) == 'image') {
			imgtag = '<img src="' + media_file + '" width="' + media_width + '" height="' + media_height + '" alt="' + project_name + '" border="0">';
		} else if (media_type == 'flv' && swfVersion == '-1') {
			imgtag = '<img src="' + media_thumbnail + '" width="' + media_width + '" height="' + media_height + '" alt="' + project_name + '" border="0">';
		} else if (media_type == 'flv' && swfVersion != '-1') {
			
				imgtag = '<object codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=10,0,0,0" id="movie_div_' + i +  '" width="' + media_width + '" height="' + media_height + '" align="top">';
				imgtag += '<param name="allowScriptAccess" value="sameDomain" />';
				imgtag += '<param name="allowFullScreen" value="false" />';
				imgtag += '<param name="movie" value="' + file_root + 'swfs/ActualPlayer.swf" />';
				imgtag += '<param name="quality" value="high" />';
				imgtag += '<param name="scale" value="noscale" />';
				imgtag += '<param name="wmode" value="transparent" />';
				imgtag += '<param name="salign" value="t" />';
				imgtag += '<param name="bgcolor" value="#ffffff" />';
				imgtag += '<param name="FlashVars" value="videoPath=' + media_file + '&videoImage=' + media_thumbnail + '" />';
				imgtag += '<embed src="' + file_root + 'swfs/ActualPlayer.swf" FlashVars="videoPath=' + media_file + '&videoImage=' + media_thumbnail + '" quality="high" scale="noscale" salign="t" bgcolor="#ffffff" width="' + media_width + '" height="' + media_height + '" name="test" align="top" allowScriptAccess="sameDomain" allowFullScreen="false" type="application/x-shockwave-flash" pluginspage="http://www.adobe.com/go/getflashplayer"></embed>';
				imgtag += '</object>';
			
			if (strpos(navigator.userAgent, 'Safari') > 0)
				safariFlashBug = 'yes';

			
		} else 
			imgtag = '';
			
	
		project_image[i] = new Element('div', {'class' : 'slide_image', 'id' : 'slide_image_' + i}).update(imgtag); 

		$(slide[i]).insert(project_image[i]);

		if (strpos(navigator.userAgent, 'MSIE 6') > 0 && media_type == 'flv' && swfVersion != '-1'){
			var flashvars = {
			videoPath: media_file,
			videoImage: media_thumbnail
			};
			var params = {
				allowScriptAccess: "sameDomain",
				allowFullScreen: "false",
				quality: "high",
				scale: "noscale",
				wmode: "transparent",
				salign: "t",
				bgcolor: "#ffffff"
			};
			//alert(file_root + "swfs/expressInstall.swf");
			//swfobject.registerObject('movie_div_' + i, "10.0.0", file_root + "swfs/expressInstall.swf");
			swfobject.embedSWF(file_root + 'swfs/ActualPlayer.swf', 'movie_div_' + i, media_width, media_height, '10.0.0', file_root + 'swfs/expressInstall.swf', flashvars, params);
		}

		if (media_body.length > 0) {
			project_image_caption[i] = new Element('div', {'class' : 'slide_image_caption'}); 
			$(slide[i]).insert(project_image_caption[i]);
			project_image_caption_inner[i] = new Element('div', {'class' : 'slide_image_caption_inner'}).update(media_body); 
			$(project_image_caption[i]).insert(project_image_caption_inner[i]);
			
		}

			
	}// end for
	
	//last-minute clean-up

	if (advanceTo == undefined)
		advanceTo = 0;

	//$(slide_prev).writeAttribute('onClick', prevlink[advanceTo]);
	//$(slide_next).writeAttribute('onClick', nextlink[advanceTo]);

	//bind next/previous functions -- they need to be registered this way to work in IE.
		
		var prevFunction = {pv: function (event) {
			$(slide_prev).removeClassName('slide_prev');
			$(slide_prev).toggleClassName('slide_prev_click');
			eval(prevlink[advanceTo]);pageTracker._trackEvent('ProjectSlides', 'Previous', project_name, advanceTo);
		}}
		prevFunction.bpv = prevFunction.pv.bindAsEventListener(prevFunction);
		Event.observe(slide_prev, 'mousedown', prevFunction.bpv);

		var nextFunction = {nx: function (event) {
			$(slide_next).removeClassName('slide_next');
			$(slide_next).toggleClassName('slide_next_click');
			eval(nextlink[advanceTo]);pageTracker._trackEvent('ProjectSlides', 'Next', project_name, advanceTo);
		}}
		
		nextFunction.bnx = nextFunction.nx.bindAsEventListener(nextFunction);
		Event.observe(slide_next, 'mousedown', nextFunction.bnx);

	//now make slide 0 visible. 
		
	if (outertarget.substring(0,9) == 'featured_') {
		$(outer_div).show();
		new Effect.Appear(slide[advanceTo]);
	} else {
		new Effect.BlindDown(outer_div);
		new Effect.Appear(slide[advanceTo]);
	}


	//safari bug. 
	if (safariFlashBug == 'yes') {
		Stimeout = setTimeout("safariFixer();",1000);
	}
}//end createslides

function safariFixer () {
	clearTimeout(Stimeout);
	x = window.pageXOffset;
	y = window.pageYOffset;
	window.scrollTo(x, (y-1));
	
}

function gotoSlide (goToI, currentI, step) {

	fadeTime = '0.25';

	if (step == undefined) {
		new Effect.Fade(slide[currentI], { duration: .25 });

		transitionTimeout = setTimeout("gotoSlide (" + goToI + ", " + currentI + ", 'end')",400); 
	} else {
		clearTimeout(transitionTimeout);

		
		//un-observe to destroy the listeners
		Event.stopObserving(slide_prev, 'mousedown', prevFunction);
		Event.stopObserving(slide_next, 'mousedown', nextFunction);
		
		if ($(slide_prev).hasClassName('slide_prev_click')) {
			$(slide_prev).removeClassName('slide_prev_click');
			$(slide_prev).toggleClassName('slide_prev');
			
		}
		if ($(slide_next).hasClassName('slide_next_click')) {
			$(slide_next).removeClassName('slide_next_click');
			$(slide_next).toggleClassName('slide_next');
		} 

		//now re-observe

		var prevFunction = {pv: function (event) {
			$(slide_prev).removeClassName('slide_prev');
			$(slide_prev).toggleClassName('slide_prev_click');
			eval(prevlink[goToI]);
		}}
		prevFunction.bpv = prevFunction.pv.bindAsEventListener(prevFunction);
		Event.observe(slide_prev, 'mousedown', prevFunction.bpv);

		var nextFunction = {nx: function (event) {
			$(slide_next).removeClassName('slide_next');
			$(slide_next).toggleClassName('slide_next_click');
			eval(nextlink[goToI]);
		}}
		nextFunction.bnx = nextFunction.nx.bindAsEventListener(nextFunction);
		Event.observe(slide_next, 'mousedown', nextFunction.bnx);

		//$(slide_prev).writeAttribute('onClick', prevlink[goToI]);
		//$(slide_next).writeAttribute('onClick', nextlink[goToI]);
		
		new Effect.Appear(slide[goToI], { duration: .25 });
	}
	
	
	
} // end gotoSlide

function makeHuh() {
	//parse XML and make the div. 
		imgdata = request.responseXML.getElementsByTagName('huh');

	for (var i = 0; i < imgdata.length; i++) {
		
		image = getNodeValue(imgdata[i],'image');
		width = getNodeValue(imgdata[i],'width');
		height = getNodeValue(imgdata[i],'height');
	
	}
	
	slide_close = new Element('div', {'id' : 'huh_close'}); 
	
	$(slide_close).toggleClassName('huh_slide_close');
	
	$('huh').insert(slide_close);
	$(slide_close).observe('click', function(event){ 
		$(slide_close).removeClassName('huh_slide_close');
		$(slide_close).toggleClassName('huh_slide_close_click');
		closeHuh();
	});
	
		imgtag = '<img src="' + image + '" width="' + width + '" height="' + height + '" alt="huh" border="0" />';


		huh_inner = new Element('div', {'id' : 'huh_inner', 'style' : 'height: ' +((height * 1) + 60) + 'px'}).update(imgtag + '<br style="clear: left;">'); 
		$('huh').insert(huh_inner);

		document.getElementById('huh_close').style.marginLeft = (width * 1) -0 + 'px';
		
		inner_x = Math.round((660 - width) / 2);

		
		huh_text = new Element('div', {'id' : 'huh_text', 'style' : 'margin-left: ' + inner_x + 'px'}); 
		huh_link_home = new Element('div', {'id' : 'huh_link_home','style' : 'float: left; margin-left: ' + ((width * 1) - 150) + 'px'}).update('want more?'); 
		$(huh_inner).insert(huh_text);
		$(huh_inner).insert(huh_link_home);

	$(huh_link_home).observe('click', function(event){ 
		closeHuh();
		window.open(base_url.replace('innercontent/','news/huh'),'newswindow');
	});

		new Effect.BlindDown('huh');

		
}


function print_r( array, return_val ) {
    // Prints out or returns information about the specified variable  
    // 
    // version: 904.610
    // discuss at: http://phpjs.org/functions/print_r
    // +   original by: Michael White (http://getsprink.com)
    // +   improved by: Ben Bryan
    // +      input by: Brett Zamir (http://brettz9.blogspot.com)
    // +      improved by: Brett Zamir (http://brettz9.blogspot.com)
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // -    depends on: echo
    // *     example 1: print_r(1, true);
    // *     returns 1: 1
    
    var output = "", pad_char = " ", pad_val = 4;

    var formatArray = function (obj, cur_depth, pad_val, pad_char) {
        if (cur_depth > 0) {
            cur_depth++;
        }

        var base_pad = repeat_char(pad_val*cur_depth, pad_char);
        var thick_pad = repeat_char(pad_val*(cur_depth+1), pad_char);
        var str = "";

        if (typeof obj === 'object' && obj !== null && obj.constructor && obj.constructor.name !== 'PHPJS_Resource') {
            str += "Array\n" + base_pad + "(\n";
            for (var key in obj) {
                if (obj[key] instanceof Array) {
                    str += thick_pad + "["+key+"] => "+formatArray(obj[key], cur_depth+1, pad_val, pad_char);
                } else {
                    str += thick_pad + "["+key+"] => " + obj[key] + "\n";
                }
            }
            str += base_pad + ")\n";
        } else if(obj == null || obj == undefined) {
            str = '';
        } else { // for our "resource" class
            str = obj.toString();
        }

        return str;
    };

    var repeat_char = function (len, pad_char) {
        var str = "";
        for(var i=0; i < len; i++) { 
            str += pad_char; 
        }
        return str;
    };
    output = formatArray(array, 0, pad_val, pad_char);

    if (return_val !== true) {
        if (document.body) {
            echo(output);
        }
        else {
            try {
                XULDocument; // We're in XUL, so appending as plain text won't work
                echo('<pre xmlns="http://www.w3.org/1999/xhtml" style="white-space:pre;">'+output+'</pre>');
            }
            catch(e) {
                echo(output); // Outputting as plain text may work in some plain XML
            }
        }
        return true;
    } else {
        return output;
    }
}


function getNodeValue(obj,tag) {
	try {
		val = obj.getElementsByTagName(tag)[0].firstChild.nodeValue;
	} catch (err) {
		val = '';
	}
	
	return val;
}

function addComment(news_id){
    //Mozilla-based browsers
	if(window.XMLHttpRequest){
		request = new XMLHttpRequest();
	} else if (window.ActiveXObject){
		request=new ActiveXObject("Msxml2.XMLHTTP");
		if (! request){
			request=new ActiveXObject("Microsoft.XMLHTTP");
		}
	}
    //the request could still be null if neither ActiveXObject
    //initializations succeeded

	/**** getting the user inputs ****/
	var comment				= document.getElementById("comment_box").value;
	var response			= document.getElementById("recaptcha_response_field").value; //get_response();// 
	var name				= document.getElementById("comment_name").value;
	var email				= document.getElementById("comment_email").value;
	var url					= document.getElementById("comment_url").value;
	var challenge			= document.getElementById("recaptcha_challenge_field").value; //get_challenge(); // //recaptcha_challenge_field
	
	if(request){
		document.getElementById('news_archive_inner').style.height = 'auto';

		var request_str = file_root + "index.php/news/comment_ajax/"+news_id;
		var params = "name="+name+"&email="+email+"&url="+url+"&recaptcha_response_field="+response+"&recaptcha_challenge_field="+challenge+"&comment="+comment+"&news_id="+news_id;
		request.open("POST", request_str, true);
		
		request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		request.setRequestHeader("Content-length", params.length);
		request.setRequestHeader("Connection", "close");

		request.onreadystatechange = function() {//Call a function when the state changes.
			if(request.readyState==4){
				document.getElementById('news_archive_comment').innerHTML	= request.responseText;
				
				// update the comment number under the news;
				var comment_number		= document.getElementById('comment_number_update').innerHTML;
				document.getElementById('news_comment_number').innerHTML		= comment_number;
				
				showRecaptcha();
//				setTimeout('setHeight();',800);
		
			}
		}
		request.send(params);

    }  else {
        alert("Your browser does not permit the use of all of this application's features.");
	}
	
	return false;
}

function emailSignUp(stage,source){
	
	emailSignupSource = source;
	
	if (stage == undefined || stage == 'start') {
		if (source == 'news')
			pt = 'news_archive_inner_content';
		else
			pt = 'content_inner';
		
		var email_signup_div = new Element('div', {'id' : 'email_signup'}).hide(); 
		//var email_signup_div = new Element('div', {'id' : 'email_signup'}); 
		
		var email_signup_inner = new Element('div', {'id' : 'email_signup_inner'}); 
		var email_signup_close = new Element('div', {'id' : 'signup_close'}).update('x'); 
		var email_signup_h = new Element('h1').update('Sign up for our mailing list.'); 
		var email_signup_p = new Element('p', {'id' : 'signup_info'}).update('We won\'t spam you; we swear.  All the standard nice-company policies apply here.'); 
		var email_signup_form = new Element('form',{'id' : 'email_signup_form', 'name' : 'email_signup_form', 'method' : 'post'}); 
		var email_signup_name = new Element('div',{'id' : 'email_signup_name'}); 
		var email_name = new Element('input',{'name' : 'name', 'type': 'text', 'value' : 'Name', 'class' : 'email_signup_text'}); 
		var email_signup_email = new Element('div',{'id' : 'email_signup_email'}); 
		var email_address = new Element('input',{'name' : 'email_address', 'type': 'text', 'value' : 'Email Address', 'class' : 'email_signup_text'}); 
		var email_submit = new Element('input',{'name' : 'email_submit', 'value' : 'Sign up!', 'type': 'submit', 'class' : 'email_signup_submit'}); 
		
		$(this.document.body).insert(email_signup_div);
		$(email_signup_div).insert(email_signup_inner);
		$(email_signup_inner).insert(email_signup_close);
		$(email_signup_inner).insert(email_signup_h);
		$(email_signup_inner).insert(email_signup_p);
		$(email_signup_inner).insert(email_signup_form);
		$(email_signup_form).insert(email_signup_name);
		$(email_signup_form).insert(email_name);
		$(email_signup_form).insert(email_signup_email);
		$(email_signup_form).insert(email_address);
		$(email_signup_form).insert(email_submit);
		
		/***** setting up the position of the email_signup_div ******/
		
		var w = 0;
		var h = 0;
		
		// IE
		if(!window.innerWidth)
		{
			//strict mode
			if(!(document.documentElement.clientWidth == 0))
			{
				w = document.documentElement.clientWidth;
				h = document.documentElement.clientHeight;
			}
			//quirks mode
			else
			{
				w = document.body.clientWidth;
				h = document.body.clientHeight;
			}
		} else
		{
			w = window.innerWidth;
			h = window.innerHeight;
		}
		
//		alert(w+'w h'+h);
		
		email_width		= 500;
		email_height	= 200;
		
//		alert(email_width+'e_w e_h'+email_height);
		
		var top =  Math.round((h - email_height)/2);
		var left = Math.round((w - email_width)/2);

/*
		var top = (h - email_height)/2;
		var left = (w - email_width)/2;
		alert(top+'top left'+left);
*/		Element.setStyle($('email_signup'), 'position:fixed; top:'+((top>0)?top:'20')+'px;left:'+((left>0)?left:'20')+'px;');
		
		/************************************************************/
		
		var sUFunction = {h: function (event) {
			closeSignup();
		}}
		sUFunction.bh = sUFunction.h.bindAsEventListener(sUFunction);
		Event.observe('signup_close', 'click', sUFunction.bh);


		var obj = document.getElementById('email_sign_up_button');

		try{
			if(obj.removeEventListener) {
				obj.removeEventListener('click','onclick',false); //Could not convert JavaScript argument
			} else {
				obj.detachEvent('onclick','onclick');			
			}

		} catch(e) {
			//alert('e' + e);
			
		}
		obj.onclick = null;
		Event.stopObserving('email_sign_up_button', 'click');
		var esFunction = {s: function (event) {
			closeSignup();
		}}
		esFunction.sc = esFunction.s.bindAsEventListener(esFunction);
		if (strpos(navigator.userAgent, 'MSIE') > 0) 
			Event.observe('email_sign_up_button', 'mousedown', esFunction.sc);
		else
			Event.observe('email_sign_up_button', 'click', esFunction.sc);

/*		
		//also change the email onclick so that it's not 
		try{
			var obj = document.getElementById('email_sign_up_button');
			//alert('onclick: ' + obj.onclick);
			
			if(obj.removeEventListener) {
				obj.removeEventListener('click','onclick',false); //Could not convert JavaScript argument
				//alert('remove');
			} else {
				obj.detachEvent('onclick','onclick');			
				//alert('detach');
			}

		} catch(e) {
			//alert('e' + e);
			
		}
		try {
			Event.stopObserving('email_sign_up_button', 'click');
			Event.observe('email_sign_up_button', 'click', sUFunction.bh);
			//#############
			//alert('listening?');
			//Event.observe('email_sign_up_button', 'click', function(event){
			//	closeSignup();
			//});
			} catch (e3) {
				alert('e3: ' + e3);
			}
*/
		var clearName = {c: function (event) {
			$(email_name).writeAttribute('value', '');
		}}
		clearName.ec = clearName.c.bindAsEventListener(clearName);
		var clearEmail = {c: function (event) {
			$(email_address).writeAttribute('value', '');
		}}
		clearEmail.ec = clearEmail.c.bindAsEventListener(clearEmail);

		Event.observe(email_name, 'focus',clearName.ec);
		Event.observe(email_address, 'focus', clearEmail.ec);

		var showName = {c: function (event) {
			if($(email_name).value == '')
				$(email_name).writeAttribute('value', 'Name');
		}}
		showName.ec = showName.c.bindAsEventListener(showName);
		var showEmail = {c: function (event) {
			if($(email_address).value == '')
				$(email_address).writeAttribute('value', 'Email Address');
		}}
		showEmail.ec = showEmail.c.bindAsEventListener(showEmail);

		Event.observe(email_name, 'blur',showName.ec);
		Event.observe(email_address, 'blur', showEmail.ec);

		//now for form submission
		Event.observe('email_signup_form', 'submit', function(event) {
			$('email_signup_form').request({
			
				onFailure: function() {return false;},
				onSuccess: function(t) {
					emailSignUp('submit',source);
				}
			});
			Event.stop(event); // stop the form from submitting
		});
			
		new Effect.BlindDown(email_signup_div);
		
		
	} else if (stage == 'submit') {
		
		var form = $('email_signup_form');
		var nm = form['name'];
		var em = form['email_address'];
		
		contentLevel = 'emailSignup';		
		url= base_url + 'email_list/subscribe';
		url = url.replace('innercontent/','');
		
		//gotta set query string.  Or not, now that I think of it.  Why not pass this stuff in the get?
		//url = url + '/' +  + '/' + ;
		queryString = 'email_address=' + urlencode ($(em).getValue()) + '&name=' + urlencode($(nm).getValue());
		//alert(url);
		//alert(queryString);
		// email_address=abc%40gmail.com&name=karl
		
		httpRequest("POST",url,true);//500

	} else if (stage == 'result') {
		result = request.responseText;
		// alert(result);
		$('signup_info').update(result);
		
		if (result == 'Thank you!  You\'ve been subscribed.' || result == 'You\'ve been unsubscribed.  Sorry to see you go!') {
			new Effect.Fade(document.getElementById('email_signup_form'), {duration: 1});
			transitionTimeout = setTimeout("closeSignup()",1500); 
		}
			

	}
	
	
		return false;


}

function closeSignup(stage) {
	
	if (stage == undefined) {
		Event.stopObserving('signup_close', 'click');
		new Effect.Fade('email_signup', {duration: 0.3});
		transitionTimeout = setTimeout("closeSignup('end')",500); 
	
		var sUFunction = {h: function (event) {
			emailSignUp('start','news'); // we hard code the stage and source here first
			return false;
		}}
		sUFunction.bh = sUFunction.h.bindAsEventListener(sUFunction);
		
		Event.stopObserving('email_sign_up_button', 'click');
		Event.observe('email_sign_up_button', 'click', sUFunction.bh);

	} else {
		$('email_signup').remove();
		clearTimeout(transitionTimeout);
	}
	return false;
}



function urlencode( str ) {
    // URL-encodes string  
    // 
    // version: 907.503
    // discuss at: http://phpjs.org/functions/urlencode
    // +   original by: Philip Peterson
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +      input by: AJ
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: Brett Zamir (http://brett-zamir.me)
    // +   bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +      input by: travc
    // +      input by: Brett Zamir (http://brett-zamir.me)
    // +   bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: Lars Fischer
    // +      input by: Ratheous
    // %          note 1: info on what encoding functions to use from: http://xkr.us/articles/javascript/encode-compare/
    // *     example 1: urlencode('Kevin van Zonneveld!');
    // *     returns 1: 'Kevin+van+Zonneveld%21'
    // *     example 2: urlencode('http://kevin.vanzonneveld.net/');
    // *     returns 2: 'http%3A%2F%2Fkevin.vanzonneveld.net%2F'
    // *     example 3: urlencode('http://www.google.nl/search?q=php.js&ie=utf-8&oe=utf-8&aq=t&rls=com.ubuntu:en-US:unofficial&client=firefox-a');
    // *     returns 3: 'http%3A%2F%2Fwww.google.nl%2Fsearch%3Fq%3Dphp.js%26ie%3Dutf-8%26oe%3Dutf-8%26aq%3Dt%26rls%3Dcom.ubuntu%3Aen-US%3Aunofficial%26client%3Dfirefox-a'
                             
    var hash_map = {}, unicodeStr='', hexEscStr='';
    var ret = (str+'').toString();
    
    var replacer = function(search, replace, str) {
        var tmp_arr = [];
        tmp_arr = str.split(search);
        return tmp_arr.join(replace);
    };
    
    // The hash_map is identical to the one in urldecode.
    hash_map["'"]   = '%27';
    hash_map['(']   = '%28';
    hash_map[')']   = '%29';
    hash_map['*']   = '%2A';
    hash_map['~']   = '%7E';
    hash_map['!']   = '%21';
    hash_map['%20'] = '+';
    hash_map['\u00DC'] = '%DC';
    hash_map['\u00FC'] = '%FC';
    hash_map['\u00C4'] = '%D4';
    hash_map['\u00E4'] = '%E4';
    hash_map['\u00D6'] = '%D6';
    hash_map['\u00F6'] = '%F6';
    hash_map['\u00DF'] = '%DF';
    hash_map['\u20AC'] = '%80';
    hash_map['\u0081'] = '%81';
    hash_map['\u201A'] = '%82';
    hash_map['\u0192'] = '%83';
    hash_map['\u201E'] = '%84';
    hash_map['\u2026'] = '%85';
    hash_map['\u2020'] = '%86';
    hash_map['\u2021'] = '%87';
    hash_map['\u02C6'] = '%88';
    hash_map['\u2030'] = '%89';
    hash_map['\u0160'] = '%8A';
    hash_map['\u2039'] = '%8B';
    hash_map['\u0152'] = '%8C';
    hash_map['\u008D'] = '%8D';
    hash_map['\u017D'] = '%8E';
    hash_map['\u008F'] = '%8F';
    hash_map['\u0090'] = '%90';
    hash_map['\u2018'] = '%91';
    hash_map['\u2019'] = '%92';
    hash_map['\u201C'] = '%93';
    hash_map['\u201D'] = '%94';
    hash_map['\u2022'] = '%95';
    hash_map['\u2013'] = '%96';
    hash_map['\u2014'] = '%97';
    hash_map['\u02DC'] = '%98';
    hash_map['\u2122'] = '%99';
    hash_map['\u0161'] = '%9A';
    hash_map['\u203A'] = '%9B';
    hash_map['\u0153'] = '%9C';
    hash_map['\u009D'] = '%9D';
    hash_map['\u017E'] = '%9E';
    hash_map['\u0178'] = '%9F';
    
    // Begin with encodeURIComponent, which most resembles PHP's encoding functions
    ret = encodeURIComponent(ret);

    for (unicodeStr in hash_map) {
        hexEscStr = hash_map[unicodeStr];
        ret = replacer(unicodeStr, hexEscStr, ret); // Custom replace. No regexing
    }
    
    // Uppercase for full PHP compatibility
    return ret.replace(/(\%([a-z0-9]{2}))/g, function(full, m1, m2) {
        return "%"+m2.toUpperCase();
    });
}

function urldecode( str ) {
    // Decodes URL-encoded string  
    // 
    // version: 907.503
    // discuss at: http://phpjs.org/functions/urldecode
    // +   original by: Philip Peterson
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +      input by: AJ
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: Brett Zamir (http://brett-zamir.me)
    // +      input by: travc
    // +      input by: Brett Zamir (http://brett-zamir.me)
    // +   bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: Lars Fischer
    // +      input by: Ratheous
    // %          note 1: info on what encoding functions to use from: http://xkr.us/articles/javascript/encode-compare/
    // *     example 1: urldecode('Kevin+van+Zonneveld%21');
    // *     returns 1: 'Kevin van Zonneveld!'
    // *     example 2: urldecode('http%3A%2F%2Fkevin.vanzonneveld.net%2F');
    // *     returns 2: 'http://kevin.vanzonneveld.net/'
    // *     example 3: urldecode('http%3A%2F%2Fwww.google.nl%2Fsearch%3Fq%3Dphp.js%26ie%3Dutf-8%26oe%3Dutf-8%26aq%3Dt%26rls%3Dcom.ubuntu%3Aen-US%3Aunofficial%26client%3Dfirefox-a');
    // *     returns 3: 'http://www.google.nl/search?q=php.js&ie=utf-8&oe=utf-8&aq=t&rls=com.ubuntu:en-US:unofficial&client=firefox-a'
    
    var hash_map = {}, ret = str.toString(), unicodeStr='', hexEscStr='';
    
    var replacer = function(search, replace, str) {
        var tmp_arr = [];
        tmp_arr = str.split(search);
        return tmp_arr.join(replace);
    };
    
    // The hash_map is identical to the one in urlencode.
    hash_map["'"]   = '%27';
    hash_map['(']   = '%28';
    hash_map[')']   = '%29';
    hash_map['*']   = '%2A';
    hash_map['~']   = '%7E';
    hash_map['!']   = '%21';
    hash_map['%20'] = '+';
    hash_map['\u00DC'] = '%DC';
    hash_map['\u00FC'] = '%FC';
    hash_map['\u00C4'] = '%D4';
    hash_map['\u00E4'] = '%E4';
    hash_map['\u00D6'] = '%D6';
    hash_map['\u00F6'] = '%F6';
    hash_map['\u00DF'] = '%DF';
    hash_map['\u20AC'] = '%80';
    hash_map['\u0081'] = '%81';
    hash_map['\u201A'] = '%82';
    hash_map['\u0192'] = '%83';
    hash_map['\u201E'] = '%84';
    hash_map['\u2026'] = '%85';
    hash_map['\u2020'] = '%86';
    hash_map['\u2021'] = '%87';
    hash_map['\u02C6'] = '%88';
    hash_map['\u2030'] = '%89';
    hash_map['\u0160'] = '%8A';
    hash_map['\u2039'] = '%8B';
    hash_map['\u0152'] = '%8C';
    hash_map['\u008D'] = '%8D';
    hash_map['\u017D'] = '%8E';
    hash_map['\u008F'] = '%8F';
    hash_map['\u0090'] = '%90';
    hash_map['\u2018'] = '%91';
    hash_map['\u2019'] = '%92';
    hash_map['\u201C'] = '%93';
    hash_map['\u201D'] = '%94';
    hash_map['\u2022'] = '%95';
    hash_map['\u2013'] = '%96';
    hash_map['\u2014'] = '%97';
    hash_map['\u02DC'] = '%98';
    hash_map['\u2122'] = '%99';
    hash_map['\u0161'] = '%9A';
    hash_map['\u203A'] = '%9B';
    hash_map['\u0153'] = '%9C';
    hash_map['\u009D'] = '%9D';
    hash_map['\u017E'] = '%9E';
    hash_map['\u0178'] = '%9F';

    for (unicodeStr in hash_map) {
        hexEscStr = hash_map[unicodeStr]; // Switch order when decoding
        ret = replacer(hexEscStr, unicodeStr, ret); // Custom replace. No regexing
    }
    
    // End with decodeURIComponent, which most resembles PHP's encoding functions
    ret = decodeURIComponent(ret);

    return ret;
}