/* ie a:hover fix
*/
function prepMenu() {
	var menu = document.getElementById("menu").getElementsByTagName("li");
	for(var x=0; x<menu.length; x++) {
		if(menu[x].className == "menuitem") {
			menu[x].onmouseover = function() {
				this.style.overflow = "visible";
			}
			menu[x].onmouseout = function() {
				this.style.overflow = "hidden";
			}
		}
	}
	return false;
}

/* side menu preparation
*/
function prepSide() {
	$("#menuControl").click(function(){
		
		if(document.getElementById("floatWrap").style.marginRight != "0px") {
			$("#floatWrap").animate({ 
				marginRight: "0px"
			}, 700 );
			$("#menuControl").animate({ 
				marginLeft: "0px"
			}, 700 );
			document.getElementById("menuControl").getElementsByTagName("img").item(0).src = "images/btn_close.png";
		} else {
			$("#floatWrap").animate({ 
				marginRight: "-165px"
			}, 700 );
			$("#menuControl").animate({ 
				marginLeft: "165px"
			}, 700 );
			document.getElementById("menuControl").getElementsByTagName("img").item(0).src = "images/btn_open.png";
		}
		
	});
}

/* swaps logos on leadership pages
*/

var logoJuggler = {
	imgUrlPrefix: 'images/logos/',
	counter: 0,
	divCounter: 0,
	maxImages: 4,
	logoSet: Array('samsung.jpg', 'voom.jpg', 'gillette.jpg', 'rogers.jpg', 'terra.jpg', 'philips.jpg', 'dex.jpg', 'mgd.jpg', 'telefonica.jpg', 'qwest.jpg', 'lenovo.jpg', 'nxp.jpg'),
	timeoutVar: null,
	init: function() {
		if(!document.getElementById('logoJuggler')) { return false; }
		divs = new Array();
		imgs = new Array();
		for(var x=0; x<logoJuggler.maxImages; x++) {
			divs[x] = document.createElement("div");
			divs[x].style.display = "block";
			divs[x].style.width = "152px";
			divs[x].style.height = "92px";
			divs[x].style.overflow = "hidden";
			divs[x].style.position = "relative";
			document.getElementById('logoJuggler').appendChild(divs[x]);
			
			imgs[x] = document.createElement("img");
			imgs[x].style.position = "absolute";
			imgs[x].style.left = "0px";
			imgs[x].src = logoJuggler.imgUrlPrefix + logoJuggler.logoSet[logoJuggler.counter];
			
			divs[x].appendChild(imgs[x]);
			
			logoJuggler.counter++;
		}
		
		logoJuggler.timeoutVar = setTimeout("logoJuggler.juggle()", 3000);
	},
	
	juggle: function() {
		myDiv = document.getElementById("logoJuggler").getElementsByTagName("div").item(logoJuggler.divCounter);
		//alert(myDiv.innerHTML);
		
		//if more than one image exists, delete first one
		//alert(myDiv.getElementsByTagName("img").length);
		if(myDiv.getElementsByTagName("img").length > 1) {
			myDiv.removeChild(myDiv.getElementsByTagName("img").item(0));
		}
		
		// add next image
		newImage = document.createElement("img");
		newImage.style.position = "absolute";
		newImage.style.left = "153px";
		newImage.src = logoJuggler.imgUrlPrefix + logoJuggler.logoSet[logoJuggler.counter];
		myDiv.appendChild(newImage);
		
		// set ID tags in prep for animation
		myDiv.getElementsByTagName("img").item(0).setAttribute("id", "logoJugglerImage1");
		myDiv.getElementsByTagName("img").item(1).setAttribute("id", "logoJugglerImage2");
		
		
		// animate
		$("#logoJugglerImage1").animate({ 
			left: "-153px"
		}, 600 );
		$("#logoJugglerImage2").animate({ 
			left: "0px"
		}, 600 );
		
		// unset ID tags to avoid conflict on next juggle
		myDiv.getElementsByTagName("img").item(0).setAttribute("id", "");
		myDiv.getElementsByTagName("img").item(1).setAttribute("id", "");
		
		// Prepare for the next juggle
		logoJuggler.counter++;
		if(logoJuggler.counter >= logoJuggler.logoSet.length) {
			logoJuggler.counter = 0;
		}
		logoJuggler.divCounter++;
		if(logoJuggler.divCounter >= logoJuggler.maxImages) {
			logoJuggler.divCounter = 0;
		}
		logoJuggler.timeoutVar = setTimeout("logoJuggler.juggle()", 3000);
	}
	
}

/* swaps images on case studies pages
*/

var caseJuggler = {
	counter: 0,
	currImage: 0,
	clickedImg: 0,
	maximgs: 0,
	init: function() {
		if(!document.getElementById('pictures')) { return false; }
		content = document.getElementById('pictures'); // width:390px;
		thumbs = document.getElementById('thumbs');
		
		content.style.float = "none";
		content.style.position = "absolute";
		mainImg = content.getElementsByTagName('img').item(0);
		mainImg.style.position = "absolute";
		mainImg.style.left = "0px";
		
		// remove original image
		content.removeChild(content.getElementsByTagName("img").item(0));
		
		// prep the stage
		newImage = new Array();
		for(var x=0; x<thumbs.getElementsByTagName("img").length; x++) {
			thumbs.getElementsByTagName("img")[x].onclick = function() {
				caseJuggler.changeImage(this);
			}
			thumbs.getElementsByTagName("img")[x].style.marginLeft = "8px";
			thumbs.getElementsByTagName("a")[x].onmouseover = function() {
				this.style.position = "relative";
				this.style.top = "-30px";
			}
			thumbs.getElementsByTagName("a")[x].onmouseout = function() {
				this.style.position = "relative";
				this.style.top = "0px";
			}
			
			thumbName = thumbs.getElementsByTagName("img")[x].src.split("/")[thumbs.getElementsByTagName("img")[x].src.split("/").length - 1];
			imgName = thumbName.replace("_thumb", "");

			// create new image
			newImage[x] = document.createElement("img");
			newImage[x].src = "images/case_studies/"+imgName;
			newImage[x].style.position = "absolute";
			newImage[x].style.left = parseInt(391*x)+"px";
			content.appendChild(newImage[x]);
		}

		caseJuggler.maximgs=thumbs.getElementsByTagName("img").length;
		
		// create the side arrow controls
		leftArrow = document.createElement("div");
		leftArrow.style.position = "absolute";
		leftArrow.style.left = "0px";
		leftArrow.style.width = "14px";
		leftArrow.style.height = "340px";
		leftArrow.style.background = "url(images/case_left.png) 0px 0px no-repeat";
		leftArrow.style.zIndex = "999999";
		leftArrow.style.cursor = "pointer";
		leftArrow.onclick = function() {
			caseJuggler.shiftImage(caseJuggler.currImage - 1);
		}
		content.appendChild(leftArrow);
		
		rightArrow = document.createElement("div");
		rightArrow.style.position = "absolute";
		rightArrow.style.right = "0px";
		rightArrow.style.width = "14px";
		rightArrow.style.height = "340px";
		rightArrow.style.background = "url(images/case_right.png) 0px 0px no-repeat";
		rightArrow.style.cursor = "pointer";
		rightArrow.onclick = function() {
			caseJuggler.shiftImage(caseJuggler.currImage + 1);
		}
		content.appendChild(rightArrow);
		
		
		caseJuggler.currImage = 0;
	},
	
	shiftImage: function(num) {
		//console.log("num: "+num+", caseJuggler.currImage: "+caseJuggler.currImage+" caseJuggler.clickedImg: "+caseJuggler.clickedImg+", visibleimg: "+caseJuggler.visibleImg+", maximgs: "+caseJuggler.maximgs);
		if(num < 0 || num >= caseJuggler.maximgs) {
			//console.log("scrolloff");
			return;
		}
		//caseJuggler.visibleImg+=num;
		if(num != caseJuggler.currImage) {
			diff = num - caseJuggler.currImage;
			if(diff > 0) {
				movingLeft = true;
			} else {
				diff = parseInt(diff*-1);
				movingLeft = false;
			}
			
			// apply id's in prep
			for(var x=0; x<document.getElementById('pictures').getElementsByTagName('img').length; x++) {
				document.getElementById('pictures').getElementsByTagName('img')[x].setAttribute("id", "caseJugglerImage"+x);
			}
			
			sandbox = document.getElementById('pictures').getElementsByTagName('img');
			for(x=0; x<sandbox.length; x++) {
				if(movingLeft == true) {
					currLeft = sandbox[x].style.left;
					//alert(currLeft + "\n" + parseInt(parseInt(currLeft) - diff*391)+"px");
					$("#caseJugglerImage"+x).animate({ 
						left: parseInt(parseInt(currLeft) - diff*391)+"px"
					}, 600 );
				} else {
					currLeft = sandbox[x].style.left;
					$("#caseJugglerImage"+x).animate({ 
						left: parseInt(parseInt(currLeft) + diff*391)+"px"
					}, 600 );
				}
			}
			
			// unset id's
			for(var x=0; x<document.getElementById('pictures').getElementsByTagName('img').length; x++) {
				document.getElementById('pictures').getElementsByTagName('img')[x].setAttribute("id", "");
			}
		}
		
		
		
		// set new currImage
		//caseJuggler.currImage = caseJuggler.clickedImg;
		caseJuggler.currImage = num;
	},
	
	changeImage: function(thumbObj) {
		
		thumbName = thumbObj.src.split("/")[thumbObj.src.split("/").length - 1];
		imgName = thumbName.replace("_thumb", "");
		
		content = document.getElementById('pictures');
		
		// determine which number thumbnail was clicked
		sandbox = document.getElementById('thumbs').getElementsByTagName('img');
		for(var x=0; x<sandbox.length; x++) {
			if(thumbObj.src == sandbox[x].src) {
				caseJuggler.clickedImg = x;
			}
		}
		
		if(caseJuggler.clickedImg != caseJuggler.currImage) {
			diff = caseJuggler.clickedImg - caseJuggler.currImage;
			if(diff > 0) {
				movingLeft = true;
			} else {
				diff = parseInt(diff*-1);
				movingLeft = false;
			}
			
			// apply id's in prep
			for(var x=0; x<document.getElementById('pictures').getElementsByTagName('img').length; x++) {
				document.getElementById('pictures').getElementsByTagName('img')[x].setAttribute("id", "caseJugglerImage"+x);
			}
			
			sandbox = document.getElementById('pictures').getElementsByTagName('img');
			for(x=0; x<sandbox.length; x++) {
				if(movingLeft == true) {
					currLeft = sandbox[x].style.left;
					//alert(currLeft + "\n" + parseInt(parseInt(currLeft) - diff*391)+"px");
					$("#caseJugglerImage"+x).animate({ 
						left: parseInt(parseInt(currLeft) - diff*391)+"px"
					}, 600 );
				} else {
					currLeft = sandbox[x].style.left;
					$("#caseJugglerImage"+x).animate({ 
						left: parseInt(parseInt(currLeft) + diff*391)+"px"
					}, 600 );
				}
			}
			
			// unset id's
			for(var x=0; x<document.getElementById('pictures').getElementsByTagName('img').length; x++) {
				document.getElementById('pictures').getElementsByTagName('img')[x].setAttribute("id", "");
			}
		}
		
		
		
		// set new currImage
		caseJuggler.currImage = caseJuggler.clickedImg;
	}
}

/* handles AJAX transitions and loading
*/

var pageJuggler = {
	pageGrid: Array(),
	currPage: '',
	clickedPage: '',
	
	init: function() {
		if(!document.getElementById("container")) { return false; }
		
		// hook all links
		sandbox = document.getElementsByTagName("a");
		for(var x=0; x<sandbox.length; x++) {
			if(sandbox[x].href !="") {
				sandbox[x].onclick = function() {
					pageJuggler.loadPage(this.href);
					return false;
				}
			}
		}
		
		sandbox = document.getElementById("container").getElementsByTagName("div");
		for(var x=0; x<sandbox.length; x++) {
			if(sandbox[x].className.match(/content/)) {
				container = sandbox[x];
			}
		}
		
		
		// create a wrapper div within the content div
		contInWrap = document.createElement("div");
		contInWrap.style.position = "relative";
		contInWrap.style.styleFloat = "left";
		contInWrap.style.cssFloat = "left";
		contInWrap.className = "containerInWrap";
		
		
		
		nodeA = document.createElement("div");
		nodeA.className = "pageJugglerNode";
		nodeA.innerHTML = container.innerHTML;
		contInWrap.appendChild(nodeA);
		
		container.innerHTML = "";
		container.appendChild(contInWrap);
		
		container.style.overflow = "hidden";
		
		
		
		menuItems = document.getElementById("menu").getElementsByTagName("li");
		for(var x=0; x<menuItems.length; x++) {
			parent = menuItems[x].getElementsByTagName("a").item(0);
			pageJuggler.pageGrid[parent.href] = Array();
			pageJuggler.pageGrid[parent.href]["x"] = x;
			pageJuggler.pageGrid[parent.href]["y"] = 0;
			
			sandbox = parent.getElementsByTagName("li");
			for(var y=1; y<=sandbox.length; y++) {
				myHref = sandbox[y].getElementsByTagName("a").item(0); 
				pageJuggler.pageGrid[myHref] = Array();
				pageJuggler.pageGrid[myHref]["x"] = x;
				pageJuggler.pageGrid[myHref]["y"] = y;
			}
		}
		
		sandbox = window.location.href.split("/");
		currPage = sandbox[sandbox.length - 1];
		if(currPage == "") {
			currPage = "index.php";
		}
		
		pageJuggler.currPage = currPage;
	},
	
	loadPage: function(page) {
		// ajax call to page to grab and return contents
		sandbox = page.split("/");
		pageJuggler.clickedPage = sandbox[sandbox.length - 1];
		$.get(page+"?q=ajax", function(data){
			pageJuggler.juggle(data);
		});
	},
	
	juggle: function(contents) {
		
		// animation prep and handler
		sandbox = document.getElementById("container").getElementsByTagName("div");
		for(var x=0; x<sandbox.length; x++) {
			if(sandbox[x].className.match(/containerInWrap/)) {
				container = sandbox[x];
			}
		}
		alert(contents);
		
		
		// create new content div
		newDiv = document.createElement("div");
		newDiv.style.styleFloat = "left";
		newDiv.style.cssFloat = "left";
		newDiv.style.marginLeft = parseInt(container.width) + "px";
		newDiv.innerHTML = contents;
		container.appendChild(newDiv);
		
		
		// set new ID's
		counter = 0;
		sandbox = container.getElementsByTagName("div");
		for(var x=0; x<sandbox.length; x++) {
			if(sandbox[x].className.match(/contInWrap/)) {
		//		sandbox[x].id = "contInWrap"+counter;
				counter++;
			}
		}
		
		/*
		** DETERMINE WHICH DIRECTION TO ANIMATE
		** spider through the navigation in this.init(), assign
		** x and y values to each item. if not found, assume
		** animation to slide divs up
		*/
		animDir = "up";
		
		
		// animate
		/* NEEDS UPDATE FOR DIRECTIONAL MOTION */
		width = $(".content").width();
		height = $(".content").height();;
		firstLeft = '';
		firstTop = '';
		secondBeginLeft = '';
		secondBeginTop = '';
		
		alert("height:"+height+"; width:"+width);
		
		if(animDir = "left") {
			firstLeft = parseInt(0 - width)+"px";
			secondBeginLeft = parseInt(width)+"px";
			firstTop = "0px";
			secondBeginTop = "0px";
		} else if(animDir = "right") {
			firstLeft = parseInt(width)+"px";
			secondBeginLeft = parseInt(0 - width)+"px";
			firstTop = "0px";
			secondBeginTop = "0px";
		} else if(animDir = "up") {
			firstLeft = "0px";
			secondBeginLeft = "0px";
			firstTop = parseInt(0 - height) + "px";
			secondBeginTop = "0px";
		} else if(animDir = "down") {
			firstLeft = parseInt(width)+"px";
			secondBeginLeft = parseInt(0 - width)+"px";
			firstTop = "0px";
			secondBeginTop = "0px";
		} else {
			
		}
		alert("primary slides from top:0px, left:0px to top:"+firstTop+", left:"+firstLeft);
		alert("secondary slides from top:"+secondBeginTop+", left:"+secondBeginLeft+" to top:0px, left:0px");
		
		// position
		document.getElementById("contInWrap1").style.left = secondBeginLeft;
		document.getElementById("contInWrap1").style.top = secondBeginTop;
		
		// go
		/*
		$("#contInWrap0").animate({ 
			left: firstLeft
		}, 700 );
		$("#contInWrap1").animate({ 
			left: secondLeft
		}, 700 );
		*/
		
		// reset ID's to blank
		for(var x=0; x<sandbox.length; x++) {
			if(sandbox[x].className.match(/contInWrap/)) {
				sandbox[x].id = "";
			}
		}
	}
}

/* load JS routines when page loads
*/


$(document).ready(function(){
	prepMenu();
	initExpander();
	prepSide();
	logoJuggler.init();
	caseJuggler.init();
	//pageJuggler.init();
	//linkEffects();
});


function ajaxme(num)
{
	$('#release').hide();
	$('#release').load('new_reports.php?article='+num+' #release > *',null,function() { $('#release').effect('slide',{},'slow'); });
	return false;
}

function ajaxpr(article)
{
	$('#release').hide();
	$('#release').load('press_releases.php?article='+article+' #release > *',null,function() { $('#release').effect('slide',{},'slow'); });
	return false;
}

backgrounds=[
/*	{url: 'index.php',img: ''}, */
	{url: 'our_story.php',img: 'our_story.jpg'},
	{url: 'leadership.php',img: 'leadership.jpg'},
	{url: 'co_creation.php',img: 'co_creation.jpg'},
	{url: 'incredibly_capable.php',img: 'capable.jpg'},
	{url: 'whats_new.php',img: 'whats_new.jpg'}
];

//describe navigations in COLUMN (not row) order (because that's how the HTML is).

//if I *really* need it in row order, I can just transform the array.

/* FULL MATRX 
htmlmatrix=[
	["index.php"],
        ["our_story.php","leadership.php","leadership_mt.php","leadership_rr.php","leadership_es.php","leadership_bs.php","leadership_ah.php","leadership_sc.php","leadership_md.php"],
        ["expertise.php","co_creation.php","multi_platform.php","our_method.php","the_five_elements.php","incredibly_capable.php"],
	["whats_new.php","press_releases.php","new_reports.php","in_the_news.php","publications.php","register.php"],
	["work.php","case_studies.php","clients.php","clients_by_category.php", "case_alere.php",
  "case_ama.php",
  "case_coldwell.php",
  "case_covenant.php",
  "case_days_inn.php",
  "case_dex.php",
  "case_lexisnexis.php",
  "case_marriott.php",
  "case_nxp.php",
  "case_samsung.php",
  "case_travelport.php"],
        ["contact.php","contact.php","contact_eindoven.php","jobs.php","internships.php"]
];
*/

htmlmatrix=[
	["index.php"],
        ["our_story.php"],
        ["expertise.php"],
	["whats_new.php"],
	["work.php"],
        ["contact.php"]
];

start={x: 0,y: 0};

starturlbit=window.location.pathname.split("/")[window.location.pathname.split("/").length -1];

for(i=0;i<htmlmatrix.length;i++) {
	for(j=0;j<htmlmatrix[i].length;j++) {
		if(htmlmatrix[i][j]==starturlbit) {
			//console.log("Found at: "+i+", "+j);
			start.x=i;
			start.y=j;
		}
	}
}

function post_ajax()
{
	// console.log("Hey a thing length you: "+$('#background').length+", "+$('#background')[0].style);
	if(bkgnd!='') {
		document.body.style.background=bkgnd;
	} else {
		backre=/url\(.*\)/;
		// console.log("Backre: "+backre+" results: "+backre.exec($('#background')[0].style.background));
		backurl=backre.exec($('#background')[0].style.background)[0];
		// console.log(backurl);
		document.body.style.background=backurl+" top center no-repeat #fff";
	}
	caseJuggler.init();
	logoJuggler.init();
	// linkEffects(); 
	ny_m="info";
	ny_m+="@";
	ny_m+="versegroup";
	ny_m+=".";
	ny_m+="com";

	ei_m="esteeghs";
	ei_m+="@";
	ei_m+="versegroup";
	ei_m+=".";
	ei_m+="com";
	
	i_m="intern";
	i_m+="@";
	i_m+="versegroup";
	i_m+=".";
	i_m+="com";
	
	i_m2="intern";
	i_m2+="@";
	i_m2+="versegroup";
	i_m2+=".";
	i_m2+="com";
	
	c_m="casting";
	c_m+="@";
	c_m+="versegroup";
	c_m+=".";
	c_m+="com";
	
	$("#contact_eindoven").html("<a href='ma"+"ilto:"+ei_m+"'>"+ei_m+"</a>");
	$('#contact_newyork').html("<a href='ma"+"ilto:"+ny_m+"'>"+ny_m+"</a>");
	$('#contact_intern').html("<a href='ma"+"ilto:"+i_m+"'>"+i_m+"</a>");
	$('#contact_intern2').html("<a href='ma"+"ilto:"+i_m2+"'>"+i_m2+"</a>");
	$('#contact_casting').html("<a href='ma"+"ilto:"+c_m+"'>"+c_m+"</a>");
}

function ajax_menu(urlbit)
{
	bkgnd='';
	for(i=0;i<backgrounds.length;i++) {
		if(urlbit==backgrounds[i].url) {
			bkgnd="url(images/backgrounds/"+backgrounds[i].img+") top center no-repeat #fff";
			break;
		}
	}
	destination={x: 0,y:0};
	for(i=0;i<htmlmatrix.length;i++) {
		for(j=0;j<htmlmatrix[i].length;j++) {
			if(htmlmatrix[i][j]==urlbit) {
				//console.log("Found at: "+i+", "+j);
				destination.x=i;
				destination.y=j;
			}
		}
	}
	
	//console.log("OK! starting from: "+start.x+", "+start.y+" and going to: "+destination.x+", "+destination.y);

	xdiff=destination.x-start.x;
	ydiff=destination.y-start.y;

	//console.log("Xdiff: "+xdiff+", ydiff: "+ydiff);
	directions=[];
	opposites=[];

	if(ydiff<0) {
		directions.push('up');
		opposites.push('down');
	}
	if(ydiff>0) {
		directions.push('down');
		opposites.push('up');
	}

	if(xdiff>0) {
		directions.push('right');
		opposites.push('left');
	}
	if(xdiff<0) {
		directions.push('left');
		opposites.push('right');
	}

	//console.log("Selected BAckground is: "+bkgnd);
	
	if(directions.length==2) {
		document.body.style.background='white';
		$('#wrapper').effect('slide',{direction: opposites[0],mode: 'hide'},'medium',function() {
			$('#wrapper').load(urlbit+' #wrapper > *',null,function() {$('#wrapper').effect('slide',{direction: directions[1],mode: 'show'},'medium',post_ajax)});
		});
	} else if(directions.length==1) {
		document.body.style.background='white';
		/* $('#wrapper').hide();
		$('#wrapper').load(urlbit+' #wrapper > *',null,function() { $('#wrapper').effect('slide',{direction: directions[0]},'slow',post_ajax); }); */

		$('#wrapper').effect('slide',{direction: opposites[0],mode: 'hide'},'medium',function() {
			$('#wrapper').load(urlbit+' #wrapper > *',null,function() {$('#wrapper').effect('slide',{direction: directions[0],mode: 'show'},'medium',post_ajax)});
		});
	} else {
		return true; //allows page reload to reset start, etc. 
	}
	start=destination;

	
	return false;
}

/* use pageswap to replace HREFs if internal
*/

function effectClick()
{
	piece=this.href.split("/")[this.href.split("/").length -1];
	//console.log("EffectClick has discerned itself to be: "+piece);
	//return false;
	return ajax_menu(piece);
}

/* side menu display
*/

function linkEffects()
{
	links=$('a');
	for(i=0;i<links.length;i++ ) {
		//console.dir(links[i]);
		//console.log("target: "+links[i].target+", onclick: "+links[i].onclick+", href: "+links[i].href);
		if(links[i].onclick==undefined && links[i].target=="" &&links[i].protocol!="javascript:" && links[i].protocol!="mailto:") {
			piece=links[i].href.split("/")[links[i].href.split("/").length -1];
			//console.log("Adding onclick!!! for piece: "+piece);
			//links[i].onclick="return false";
			//links[i].onclick="return ajax_menu('"+piece+"');";
			links[i].onclick=effectClick;
			//console.log("onclick is now: "+links[i].onclick);
			//links[i].onclick=function() {window.alert("hi"); return false;};
			piece='';
		}
	}
}


/*
** Example HTML
**
** <div class="familyResources expandable">
**	<div class="expandController headerMessage">[+]</div>
**	<h4>Childcare</h4>
**	<ul>
**		<li><a href="http://gocitykids.parentsconnect.com/category/new-york-ny-usa/resources/childcare#babysitters">Babysitters</a></li>
**		<li><a href="http://gocitykids.parentsconnect.com/category/new-york-ny-usa/resources/childcare#before-afterschool-care">Before / Afterschool Care</a></li>
**	</ul>
** </div>
**
**
*/

menulist=[];

// Expandable Elements
// Customizable variables below
var beginCollapsed = false;
var expandableLimitOne = true;
var headerTag = 'h3';
var expandableTag = 'div';
var objClassName = "expandable";

/*
** Do not edit below this line
*/

function reinitExpander() {
	collapseAll();
	expanderToggle($('.expA > '+headerTag)[0]);
	expanderToggle($('.expB > '+headerTag)[0]);
	expanderToggle($('.expC > '+headerTag)[0]);
}

function initExpander() {
	// When the document is finished loading, prepare the appropriate elements
	sandbox = document.getElementsByTagName("*");
	for(var x=0; x<sandbox.length; x++) {
		if(sandbox[x].className != null && sandbox[x].className != "") {
			thisObjClass = sandbox[x].className;
			if(thisObjClass.match(objClassName)) {
				handler = sandbox[x].getElementsByTagName(headerTag);
				if(handler[0] != "" && handler[0] != null) {
					handler[0].onmouseover = function() { expandMouseover(this); };
					handler[0].style.cursor = "pointer";
					if(beginCollapsed == true) {
						handler[0].parentNode.getElementsByTagName(expandableTag)[0].style.display = "none";
					}
				}
			}
		}
	}
	//activationArray= [$('.expA')[0],$('.expB')[0],$('.expC')[0]];
	//console.dir(activationArray);
	menulist=$('.expandable > '+headerTag); 
	//now we have a nice array of DOM elements corresonding to each <div>, which contains a trigger (h3), and a thing to expand (a div)
	fm=$('#float_menu');
	//console.dir(fm);
	fm.mouseleave(function() {/* console.log('mouseleave!');  */reinitExpander(); });
	//fm.mouseout(function() {console.log('mouseout!');});
}

function collapseAll() {
	sandbox = document.getElementsByTagName("*");
	for(var x=0; x<sandbox.length; x++) {
		if(sandbox[x].className != null && sandbox[x].className != "") {
			thisObjClass = sandbox[x].className;
			if(thisObjClass.match(objClassName)) {
				handler = sandbox[x].getElementsByTagName(headerTag);
				if(handler[0] != "" && handler[0] != null) {
					handler[0].className = handler[0].className.replace(/OnState/, "");
					handler[0].parentNode.getElementsByTagName(expandableTag)[0].style.display = "none";
				}
			}
		}
	}
}

/*function collapseSome() {
	arinfo='';
	//console.dir(activationArray);
	for(e in activationArray) {
		arinfo+=(e+': '+activationArray[e].className+' ');
	}
	//console.log("collapseSome called, arinfo is: "+arinfo)
	if(activationArray.length > 3) {
		tmp = activationArray.shift();
		//console.log("I want to HIDE: "+tmp.className);
		tmp.getElementsByTagName(expandableTag)[0].style.display = "none";
		hdr=tmp.getElementsByTagName(headerTag)[0];
		hdr.className = hdr.className.replace(/OnState/,"");
	} else {
		//console.log("nothing to collapse!");
	}
} */

function expandMouseover(thisObj) {
	//this gets invoked on the header tag (h3?), and says to expand the associated DIV
	moused=-1;
	for(e in menulist) {
		elem=menulist[e];
		//console.log("comparing "+elem.className+" to "+a.className);
		if(elem==thisObj) {
			moused=parseInt(e);
			break;
		}
	}
	selectionArray=[];
	switch(moused) {
		case -1:
		//you didn't mouse over anything we track!
		return;
		break;
		
		case 0:
		selectionArray=[0,1,2];
		break;
		
		case menulist.length-1:
		selectionArray=[menulist.length-1,menulist.length-2,menulist.length-3];
		break;
		
		default:
		selectionArray=[moused-1,moused,moused+1];
	}
	//console.log("Selection array is: "+selectionArray.join(", "));
	for(a=0;a<menulist.length;a++) {
		isvisible=(menulist[a].parentNode.getElementsByTagName(expandableTag)[0].style.display != "none");
		shouldbevisible=false;
		for(b=0;b<selectionArray.length;b++) {
			if(selectionArray[b]==a) {
				shouldbevisible=true;
				break;
			}
		}
		//console.log("Item: "+a+" is it visible?: '"+isvisible+"' - SHOULD it be visible? "+shouldbevisible);
		if((shouldbevisible && !isvisible) || (!shouldbevisible && isvisible)) {
			//console.log("Going to toggle for "+a);
			expanderToggle(menulist[a]);
		} else {
			//console.log("Leaving "+a+" alone");
		}
	}
}

function expanderToggle(thisObj) {
	workingObj = thisObj.parentNode.getElementsByTagName(expandableTag)[0];
	if(workingObj.style.display == "none") {
		oldState = "none";
	} else {
		oldState = "block";
	}
	if(oldState == "none") {
		workingObj.style.display = "block";
		thisObj.className += " OnState";
	} else {
		thisObj.className = thisObj.className.replace(/OnState/, "");
		workingObj.style.display = "none";
	}
}

