var MenuTween;
var workingMenuTimeout;
var helloOpen = false;
var workingOpen = false;

Window.onLoad(function() {
	$('top-nav-hello').addEvent('click', function(){
		if(helloOpen) return;
		clearTimeout(workingMenuTimeout);
		document.getElementById("working-sub-nav").style.top = -280 + "px";
		var initpath = document.getElementById("top-nav");
		initpath.style.left="0px";
		initpath.style.top="-357px";
		MenuTween = new JSTween("MenuTween","top-nav",7);
		MenuTween.Move(0,0);
		setTimeout(delayedVideoOpen, 1000);
		helloOpen = true;
	});
	$('close-hello').addEvent('click', function(){
		var initpath = document.getElementById("top-nav");
		initpath.style.left="0px";
		initpath.style.top="0px";
		MenuTween = new JSTween("MenuTween","top-nav",7);
		MenuTween.Move(-357,0);
		swfobject.removeSWF("flv-player");
		helloOpen = false;
	});
	$('top-nav-working').addEvent('mouseover', function(){
		clearTimeout(workingMenuTimeout);
		if(helloOpen || workingOpen) return;
		var initpath = document.getElementById("working-sub-nav");
		initpath.style.left="0px";
		initpath.style.top="-380px";
		MenuTween = new JSTween("MenuTween","working-sub-nav",7);
		MenuTween.Move(85,0);
		workingOpen = true;
	});
	$('top-nav-working').addEvent('mouseout', function(){
		if(helloOpen) return;
		workingMenuTimeout = setTimeout(closeWorking, 1000);
	});
	$('working-sub-nav').addEvent('mouseover', function(){
		clearTimeout(workingMenuTimeout);
	});
	$('working-sub-nav').addEvent('mouseout', function(){
		if(!workingOpen) return;
		workingMenuTimeout = setTimeout(closeWorking, 1000);
	});
});

function delayedVideoOpen()
{
	loadVideo("Hello355x199.flv", "");	
}

function closeWorking()
{
	var initpath = document.getElementById("working-sub-nav");
	initpath.style.left="0px";
	initpath.style.top="85px";
	MenuTween = new JSTween("MenuTween","working-sub-nav",7);
	MenuTween.Move(-380,0);
	workingOpen = false;
}

function loadVideo(item,caption)
{	
	if (isBlog==1){
		loadBlogVideo(item,caption);
		return
	}
	var objNewDiv = document.createElement("div");
	objNewDiv.id = "flv-player";
	
	document.getElementById("hello-video").appendChild(objNewDiv);
	
	/********Detection Vars***********/
	var flashWrapper = "flv-player";
	var flashVersion = "9.0.0";
	/********Flash Vars***********/
	var swfname = "StandAloneFLV.swf";
	var flashWidth = "355px";
	var flashHeight = "199px";
	//Object to pass variables to flash ------ var flashvars = {varName:"varValue, varName2:varValue2}
	var flashvars = {flv:"data/uploads/"+item,autoPlay:"true",caption:caption,closeButton:"false"};
	var params = {align:"middle",salign:"tl",wmode:"transparent"};
	var attributes = {};
	
	/*******************************DO NOT EDIT BELOW THIS LINE**********************************************/

	attributes.id = flashWrapper
	swfobject.embedSWF(swfname, flashWrapper, flashWidth, flashHeight, flashVersion, false, flashvars, params, attributes);
	
	
	//load the sub nav items for the movements and identities
	
	
	
}


function loadBlogVideo(item,caption)
{	
	var objNewDiv = document.createElement("div");
	objNewDiv.id = "flv-player";
	
	document.getElementById("hello-video").appendChild(objNewDiv);
	
	/********Detection Vars***********/
	var flashWrapper = "flv-player";
	var flashVersion = "9.0.0";
	/********Flash Vars***********/
	var swfname = "http://brainsonfire.com/StandAloneFLV.swf";
	var flashWidth = "355px";
	var flashHeight = "199px";
	//Object to pass variables to flash ------ var flashvars = {varName:"varValue, varName2:varValue2}
	var flashvars = {flv:"http://brainsonfire.com/data/uploads/"+item,autoPlay:"true",caption:caption,closeButton:"false"};
	var params = {align:"middle",salign:"tl",wmode:"transparent"};
	var attributes = {};
	
	/*******************************DO NOT EDIT BELOW THIS LINE**********************************************/

	attributes.id = flashWrapper
	swfobject.embedSWF(swfname, flashWrapper, flashWidth, flashHeight, flashVersion, false, flashvars, params, attributes);
	
	
	//load the sub nav items for the movements and identities
	
	
	
}

function ReqPage()
	{ 
		

		var req = null;
 
		if (window.XMLHttpRequest)
		{
 			req = new XMLHttpRequest();
			if (req.overrideMimeType) 
			{
				req.overrideMimeType('text/xml');
			}
		} 
		else if (window.ActiveXObject) 
		{
			try {
				req = new ActiveXObject("Msxml2.XMLHTTP");
			} catch (e)
			{
				try {
					req = new ActiveXObject("Microsoft.XMLHTTP");
				} catch (e) {}
			}
	        	}



		req.onreadystatechange = function()
		{ 
			
			if(req.readyState == 4)
			{
				if(req.status == 200)
				{
					
					var theObj = req.responseXML;
					
			
					var items = theObj.getElementsByTagName('column')[0].getElementsByTagName('elements')[0].getElementsByTagName('item')
					var _html="<ul>"
					for (var i = 1; i < items.length; i++) {
						_html +="<li><a href=\"http://www.brainsonfire.com/"+items[i].childNodes[0].childNodes[0].nodeValue+"\">"+items[i].childNodes[1].childNodes[0].nodeValue+"</a></li>"
					
					}
					_html+="</ul>"
					document.getElementById("igniting-movements").innerHTML = _html;
					//alert(_html)
					
					var items = theObj.getElementsByTagName('column')[1].getElementsByTagName('elements')[0].getElementsByTagName('item')
					var _html2="<ul>"
					for (var i = 1; i < items.length; i++) {
						_html2 +="<li><a href=\"http://www.brainsonfire.com/"+items[i].childNodes[0].childNodes[0].nodeValue+"\">"+items[i].childNodes[1].childNodes[0].nodeValue+"</a></li>"
					
					}
					_html2+="</ul>"
					document.getElementById("building-identities").innerHTML = _html2;
					
					_getBlogImage()//now load image for blog
				}	
				else	
				{
					//alert("Error: returned status code " + req.status + " " + req.statusText);
				}	
			} 
		}; 
		var theUrl = document.location.toString();
		if (theUrl.indexOf("www")>0){
			req.open("GET", "http://www.brainsonfire.com/data/xml/contentAJAX.aspx", true); 
			req.send(null); 
		}else{
			req.open("GET", "http://brainsonfire.com/data/xml/contentAJAX.aspx", true); 
			req.send(null); 
		}
	}

function _getBlogImage(){
	
	var req = null;
 
		if (window.XMLHttpRequest)
		{
 			req = new XMLHttpRequest();
			if (req.overrideMimeType) 
			{
				req.overrideMimeType('text/xml');
			}
		} 
		else if (window.ActiveXObject) 
		{
			try {
				req = new ActiveXObject("Msxml2.XMLHTTP");
			} catch (e)
			{
				try {
					req = new ActiveXObject("Microsoft.XMLHTTP");
				} catch (e) {}
			}
	        	}



		req.onreadystatechange = function()
		{ 
			
			if(req.readyState == 4)
			{
				if(req.status == 200)
				{
					
					var theObj = req.responseXML;
					
					document.getElementById("content_wrapper").innerHTML = "<img src='http://brainsonfire.com/data/uploads/"+theObj.getElementsByTagName('image')[0].childNodes[0].nodeValue+"' />"
					//alert("<img src='http://brainsonfire.com/data/uploads/"+theObj.getElementsByTagName('image')[0].childNodes[0].nodeValue+"' />")
				}	
				else	
				{
					//alert("Error: returned status code " + req.status + " " + req.statusText);
				}	
			} 
		}; 
		
		var theUrl = document.location.toString();
		if (theUrl.indexOf("www")>0){
			req.open("GET", "http://www.brainsonfire.com/data/xml/contentAJAX.aspx", true); 
			req.send(null); 
		}else{
			req.open("GET", "http://brainsonfire.com/data/xml/contentAJAX.aspx", true); 
			req.send(null); 
		}
	
	
}

function pull_movements_identities(){

	var url = "data/xml/contentAJAX.aspx"; 

	xml = new JKL.ParseXML( url );   
	data = xml.parse();
	//load movements
	var items = data["content"]["primary"]["menu"]["column"][0]["elements"]["item"];
	var _html="<ul>"
	for (var i = 0; i < items.length; i++) { 
		_html +="<li><a href=\""+items[i]["button"]+"\">"+items[i]["title"]+"</a></li>"
	}
	_html+="</ul>"
	document.getElementById("igniting-movements").innerHTML = _html;
	//load identities
	var items = data["content"]["primary"]["menu"]["column"][1]["elements"]["item"];
	var _html="<ul>"
	for (var i = 0; i < items.length; i++) { 
		_html +="<li><a href=\""+items[i]["button"]+"\">"+items[i]["title"]+"</a></li>"
	}
	_html+="</ul>"
	document.getElementById("building-identities").innerHTML = _html;
	setActiveNav()
}
function getBlogImage(){
	
	var url = "data/xml/blogimage.aspx"; 
	var xml = new JKL.ParseXML( url );   
	var data = xml.parse();
	//alert(data)
	//load movements
	var theimage = data["blogImage"]["image"];
	
	document.getElementById("content_wrapper").innerHTML = "<img src='http://brainsonfire.com/data/uploads/"+theimage+"' />"
	
}
var isBlog=0;
function setActiveNav(){
	
	var loc = document.location.toString().split("/");
	var cnt = loc.length-1;
	var item="home";
	var tmp = loc[cnt];
	if (tmp.indexOf("?")>0){
		tmp = tmp.split("?")[0];
	}
	switch(tmp){
		case "approach.aspx" :
			item = "top-nav-approach";
			break;
		case "work.aspx" :
			item = "top-nav-working";
			break;
		case "individualOverview.aspx" :
			item = "top-nav-individuals";
			break;
		case "people.aspx" :
			item = "top-nav-individuals";
			break;
		case "browseByTag.aspx" :
			item = "navTag";
			break;
		case "published.aspx" :
			item = "top-nav-published";
			break;
	}
	if(item=="home" && document.location.toString().indexOf("blog")<0){
		return
	}
	
	if (document.location.toString().indexOf("speaking")>0 || document.location.toString().indexOf("index.php?cat=262")>0 || document.location.toString().indexOf("?s=")>0){
		document.getElementById('top-nav-speak').className = "active";
		isBlog=1;
		ReqPage();
	}else if (document.location.toString().indexOf("blog")>0){
		isBlog=1;
		ReqPage();
		document.getElementById('top-nav-blog').className = "active";
	}else{
		//alert(item)
		document.getElementById(item).className = "active";
	}
}