var menu_outs = [];
var MenuTimer = window.setInterval( doMenuOuts , 1500 );
function doMenuOuts(){
	for( var i = 0; i < menu_outs.length; i++ ){
		if( menu_outs[ i ].className == "item_over" ){
			menu_outs[ i ].className = "item_div" + menu_outs[ i ].title; 
			document.getElementById( "spacer" ).style.height = "213px";
		} else {
			menu_outs[ i ].style.overflow = 'hidden';		
		}
	}
	menu_outs = [];
}
var menu_links = [];
function applyMouseEvents(){
	 var top = $(".topLevel");
	 top.mouseover(function(){
		this.style.overflow = "visible";
		for( var i = 0; i < menu_outs.length; i++ ){
			if( menu_outs[ i ].id == this.id ){
				menu_outs.splice( i , 1 );
				//i--;
			}
		} 
		doMenuOuts();
		return false;
	 });
	 top.mouseout(function(){ menu_outs.push( this ); return false;});
	
	 top.bind('click', function () {
		//urchinTracker('/leftnav/' + getCatName( this ).replace(/&/gi, "and")  );
		urchinTracker('/leftnav/' + getCatName( this ) );
	 });
	 top.filter( function(){ 
		if( this.getAttribute( "hft" ) == "True" ){//Hide from top if we need it
			this.style.display = "none";
		}
		this.childNodes[ 0 ].title = "click here"; return false; 
	} );
	 
	 var subLevel = $(".subLevel");
	 subLevel.mouseover(function(){
		this.style.overflow = "visible";
		for( var i = 0; i < menu_outs.length; i++ ){
			if( menu_outs[ i ].id == this.id ){
				menu_outs.splice( i , 1 );
				//i--;
			}
		}
		doMenuOuts();
	 });
	 subLevel.mouseout(function(){ menu_outs.push( this ); });
	 subLevel.filter( function(){ 
		var title = this.childNodes[ 0 ];
		if( this.getAttribute( "hft" ) == "True" ){//Hide from top if we need it
			this.style.display = "none";
		}
		//if( title.href.indexOf( "jccmanhattan.org" ) == -1 && ( title.href.indexOf( "http://" ) != -1 || title.href.indexOf( "https://" ) != -1 ) ){
		if( title.href.indexOf( "jccmanhattan.org" ) == -1 || title.href.indexOf( ".pdf" ) != -1 || title.href.indexOf( ".php" ) != -1 ){
			menu_links.push( title.href );
			title.target = "_new";
		}
		title = title.innerHTML;
		if( title.length > 20 ){
			for( var i = 20; i > 10; i-- ){
				if( title.charCodeAt( i ) == 32 ){
					this.childNodes[ 0 ].innerHTML = title.substring( 0 , i ) + "<br />" + title.substring( i + 1 , title.length );
					break;
				}
			}
		}
		//return this.childNodes.length > 1;
		return hasKids( this );
	} ).append( getMarkerDiv() );
	
		
	 subLevel.bind('click', function () {
		//urchinTracker('/leftnav/' + getCatName( this.parentNode.parentNode ).replace(/&/gi, "and")  + '/' + getCatName( this ).replace(/&/gi, "and")  );
		urchinTracker('/leftnav/' + getCatName( this.parentNode.parentNode )  + '/' + getCatName( this ) );
	 });

	//$("div#cat_2_2572 > a").attr("href","javascript:void(0);");
	$("div#cat_2_2572 > a").mousedown(function(){this.href="javascript:void(0);";});
	$("div#cat_2_2572 > a").bind("click",doYouWantWellness);
	
	var bcs = document.getElementById( "BreadCrumb_div" );
	if( bcs == null ){ return; }
	else{
		bcs = bcs.getElementsByTagName( "A" );
		for( var i = 0; i< bcs.length; i++ ){
			//bcs[i].href = "/category.aspx?catid=" + getCatidFromUrl( bcs[i].href );
			bcs[i].onclick = "javascript:urchinTracker( '/breadcrumbs/' + this.innerHTML.replace(/&/gi, 'and') )";
		}
	}
}
function doYouWantWellness(){
	var confirmBox = $('<div id="dialog-confirm" title="Introducing the Marti Ann Meyerson Center for Health &amp; Wellness">\
	<p>\
	<a href="health-club" style="width: 190px; height: 103px; z-index: 1; position: absolute; top: 420px; left: 13px; opacity: 0;" onclick="location.href=\'health-club\'">\
	<img src="images/backgrounds/clear.gif" style="width: 190px; height: 103px; border:none;" /></a>\
	<a href="healthwellness" style="z-index:0; position:relative;"><img src="images/Meyerson_Interstitial_NEW.jpg" border="0" /></a>\
	</p>\
	</div>');
	$(document.body).append(confirmBox);
	confirmBox.dialog({
		position:"top",
		resizable: true,
		height:"auto",
		width:920,
		modal: true
	});
	confirmBox.css("overflow","hidden");
}
function getMarkerDiv(){
	var div = document.createElement("DIV");
	div.className = "marker_div";
	return div;
}
function hasKids( div ){
	if( div.childNodes.length < 2 ){ return false; }
	div = div.childNodes[ 1 ];
	if( div.className != "subholder" ){ return false; }
	for( var i = 0; i < div.childNodes.length; i++ ){
		if( div.childNodes[ i ].getAttribute( "hft" ) == "False" ){ return true; }
	}
	//div.parentNode.removeChild( div );
	div.style.display="none";
	return false;
}

/****************ALERTS***********************/
var xmlAlerts;
function loadAlerts( ){
	GetAlertXML();
	if (xmlAlerts==null)
	{
	  confirm ("Your browser does not support AJAX!");
	  return;
	}
	xmlAlerts.onreadystatechange = makeAlerts;
	xmlAlerts.open("GET","Alerts.xml",true);
	xmlAlerts.send(null);
}

function GetAlertXML()
{
	xmlAlerts=null;
try
  {
  // Firefox, Opera 8.0+, Safari
  xmlAlerts=new XMLHttpRequest();
  }
catch (e)
  {
  // Internet Explorer
  try
    {
    xmlAlerts=new ActiveXObject("Msxml2.XMLHTTP");
    }
  catch (e)
    {
    xmlAlerts=new ActiveXObject("Microsoft.XMLHTTP");
    }
  } 
return xmlAlerts;
}
function makeAlerts(){
	if( xmlAlerts.readyState == 4 ){
		var alerts = xmlAlerts.responseXML.documentElement.childNodes, code = "", alertBoxCode, hasHL;
		for( var i = 0; i < alerts.length; i++ ){
			hasHL = -1;
			alertBoxCode = "<a href='#' onclick='this.parentNode.parentNode.style.overflow = \"hidden\"' class='alert_close'>x</a>";
			for( var j = 0; j < alerts[ i ].childNodes.length; j++ ){
				if( alerts[ i ].childNodes[ j ] && hasHL != 1){
					hasHL = 0;
				}
				//if alert is new, set hasHL to 1
				//put a breaker if it's not the first
				if( j != 0 ){
					alertBoxCode += "<hr />";
				}
				alertBoxCode += ( alerts[ i ].childNodes[ j ].text != null ) ? alerts[ i ].childNodes[ j ].text : alerts[ i ].childNodes[ j ].textContent;
			}
			code += "<div class='alert_slot'>";
			if( hasHL == 0 ){
				code += "<img src='#' onclick='this.parentNode.style.overflow = \"visible\"'>";
				code += "<div class='alert_box'>";
				code += alertBoxCode;
				code += "</div>";
			}
			code += "</div>";
		}
		document.getElementById( "alerts_div" ).innerHTML = code;
	}
}
function putBackArrowIn( ID , name ){
	document.getElementById( ID ).innerHTML = '<img src="eddy/042709/jGlideMenu_0/img/arrow_bk2.gif" alt="<" border="0" /> ' + constrain( name , 23 );
}

/******************RIGHT NAV******************************/
function findCurrentMenuItem(){
	var menu_divs = document.getElementById( "Menu_div" );
	if( menu_divs != null ){
		menu_divs = menu_divs.getElementsByTagName( "DIV" );
	} else { window.setTimeout( buildRightNav , 300 ); }
	for( var i = 0; i < menu_divs.length; i++ ){
		if( menu_divs[ i ].id.indexOf( current_category ) != -1 ){
			return menu_divs[ i ];
		}
	}
}
function findMenuItem( ID ){
	var divs = document.getElementsByTagName( "DIV" );
	var possible = [], choose = null;
	for( var i = 0; i < divs.length; i++ ){
		if( divs[ i ].id.indexOf( "cat_" ) != -1 && divs[ i ].id.indexOf( ID ) != -1 ){
			possible.push( divs[ i ] );
			//return divs[ i ];
		}
	}	
	if( possible.length == 0 ){ return null; }
	else{
		choose = possible[ 0 ];
		for( var i = 0; i<possible.length; i++ ){
			if( getLevel( possible[ i ] ) < getLevel( choose ) ){
				choose = possible[ i ];
			}
		}
	}
	return choose;
}
var mainParent_tries = 0;
function mainParentOf( obj ){
	//try{
		var BCs = document.getElementById( "BreadCrumbNav_2_01" );
		if( BCs == null ){ BCs = document.getElementById( "BreadCrumbNav" ); }
	BCs = BCs.getElementsByTagName("A");
	if( BCs.length > 0 ){ 
		var main = BCs[ 0 ].href;
		if( main == "javascript:void(0)" ){
			main = BCs[ 0 ].res;
		}
	}else{
		return obj;
		//var main = location.href;
	}
	//main = main.substring( main.indexOf( "catid=" ) + 6 , main.indexOf( "catid=" ) + 10 );
	var menu = document.getElementById( "Menu_div" );
	var menu_divs = menu.getElementsByTagName( "A" );
	
	for( var i = 0; i < menu_divs.length; i++ ){
		if( menu_divs[ i ].href == main ){
			return menu_divs[ i ].parentNode;
		}
	}
	if( mainParent_tries < 10 ){
		mainParent_tries++;
		window.setTimeout( buildRightNav , 300 );
	}
	/*}catch(e){ 
		if( mainParent_tries < 10 ){
			mainParent_tries++;
			window.setTimeout( buildRightNav , 300 );
		}
	}*/
}
function getLevel( obj ){
	var level = -1;
	if( obj == null || obj.id == null ){ return level; }
	for( var i = 0; i < obj.id.length; i++ ){
		if( obj.id.charAt( i ) == '_' ){
			level++;
		}
	}
	//alert( level );
	return level; //return document.getElementById( "BreadCrumbNav_2_01" ).childNodes.length - 1;
}
function parentOf( ID ){
	var divs = document.getElementsByTagName( "DIV" ), thisDiv;
	for( var i = 0; i < divs.length; i++ ){
		if( divs[ i ].id.indexOf( "cat_" ) != -1 && divs[ i ].id.indexOf( ID ) != -1 ){
			thisDiv = divs[ i ];
			break;
		}
	}
	if( getLevel( thisDiv ) == 1 ){ return 0; }
	else{
		return getCatID( (thisDiv != null) ? thisDiv.parentNode.parentNode : null );	
	}
}
function hasSubs( div ){
	return childByClass( div , "subholder" ) != null;
}
function getCatName( div ){
	if( div == null ){ return ""; }
	if( div.tagName == "C" ){ return $(div).attr("T"); }
	/*for( var i = 0; i < div.childNodes.length; i++ ){
		if( div.childNodes[ i ].tagName == "A" ){
			return div.childNodes[ i ].innerHTML;
		}
	}*/
	return $(div).find("a").html();
}
function getNameFromId( ID ){
	var divs = document.getElementsByTagName( "DIV" );
	for( var i = 0; i < divs.length; i++ ){
		if( divs[ i ].id.indexOf( "cat_" ) != -1 && divs[ i ].id.indexOf( ID ) != -1 ){
			return getCatName( divs[ i ] );
		}
	}
}
function getCatID( div ){
	if( div == null ){ return -1; }
	/*for( var i = 0; i < div.childNodes.length; i++ ){
		if( div.childNodes[ i ].tagName == "A" ){
			return div.childNodes[ i ].href.substring( div.childNodes[ i ].href.length - 4 , div.childNodes[ i ].href.length );
		}
	}*/
	return div.id.substring( div.id.length - 4 , div.id.length );
}
function getCatHREF( div ){
	for( var i = 0; i < div.childNodes.length; i++ ){
		if( div.childNodes[ i ].tagName == "A" ){
			return div.childNodes[ i ].href;
		}
	}
}
function getShortHREF( div ){
	for( var i = 0; i < div.childNodes.length; i++ ){
		if( div.childNodes[ i ].tagName == "A" ){
			var href = div.childNodes[ i ].href;
			return ( href.indexOf( "jccmanhattan.org/" ) != -1 ) ? href.substring( href.indexOf( "jccmanhattan.org/" ) + 17 , href.length ) : href;
		}
	}
}
function findUserFriendlyURL( url ){
	return getCatHREFfromID( getCatidFromUrl( url ) );
}
function getCatHREFfromID( ID ){
	var divs = document.getElementsByTagName( "DIV" );
	for( var i = 0; i < divs.length; i++ ){
		if( divs[ i ].id.indexOf( "cat_" ) != -1 && divs[ i ].id.indexOf( ID ) != -1 ){
			return getCatHREF( divs[ i ] );
		}
	}
	return ID;
}
function rNavViaXML(){
    if (xmlHttp.readyState == 4) {
		buildRightNav();
		//buildInstructorList( getCategory( current_category ) );
	}
}
var mainID, currentCat, currentLvl;
function buildRightNav(){
	data = xmlHttp.responseXML.documentElement; 
	programIndex = data.getElementsByTagName("P");
	categoryIndex = data.getElementsByTagName("C");
	seriesSaleIndex = data.getElementsByTagName("SSO");
	
	currentCat = findCurrentMenuItem();
	
	var main = mainParentOf( currentCat );
	
	mainID = getCatID( main ); 
	
	var code = "<table style='position:relative;'><tr><td style='width: 200px; height: auto; border: 1px #000000;'></td><td style='width: 300px; height: auto; border: 1px #000000;'></td><td><div class='jGM_box' id='jGlide_001'>";
	//print the main category menu that starts as the display
	code += printCodeFor( main , "at the jcc in manhattan" , "001" );
	var subs = childrenByTag( childByClass( main , "subholder" ) , "div" );
	var subSubs;
	for( var i = 0; i < subs.length; i++ ){
		if( hasKids( subs[ i ] ) ){
			code += printCodeFor( subs[ i ] , getCatName( main ) );
			document.getElementById( "right_nav" ).innerHTML = code;
		}
	}
	code += "</div></td></tr></table>";
	try{document.getElementById( "right_nav" ).innerHTML = code;}catch(e){}

	
		$('#jGlide_001').jGlideMenu( { tileSource      : '.jGlide_001_tiles' , demoMode        : false } ).show();
	

                        // Connect "Toggle" Link        
                        $('#switch').click(function(){  $(this).jGlideMenuToggle();});
                        $('li').click(function(){  $(this).jGlideMenuToggle();});
						var int = window.setTimeout( function(){
							currentLvl = getLevel( currentCat );//document.getElementById( "BreadCrumbNav_2_01" ).childNodes.length;
							deepLinkBreadCrumbs();
							InitialSlide();
						}, 2000 );
						//}catch(e){alert(e + " in buildRightNav");}
}
function InitialSlide(){
	if( currentLvl > 1 ){
			//try{alert( currentCat.id + " , " + currentCat.parentNode + " , " + document.getElementById( currentCat.id ).parentNode.parentNode );}catch(e){alert(e);}
			currentCat = document.getElementById( currentCat.id );
		while( currentLvl > 2 || currentCat.childNodes.length < 2 ){
			currentCat = currentCat.parentNode.parentNode; 
			currentLvl--;
		}
		scrollToTile( getCatID( currentCat ) );
		//jQuery.jGlideMenu.loadTile("tile_" + getCatID( currentCat ),jQuery.jGlideMenu.URLParams);//this line creates IE7 left nav error
		//jQuery.jGlideMenu.scrollToTile(currentLvl,jQuery.jGlideMenu.defaultScrollSpeed);
	}
}
function printCodeFor( div , parentName , title ){
	var catName = ( title != null ) ? title : getShortHREF( div );
	catName = catName.replace( ' ' , '_' );
	var code = '<ul id="tile_' + catName +'" class="jGlide_001_tiles" title="' + stripTags( getCatName( div ) ) + '" alt="' + parentName + '">';
	var subs = childrenByTag( childByClass( div , "subholder" ) , "div" );
	for( var i = 0; i < subs.length; i++ ){
		code += "<li";
		if( title != null && hasKids( subs[ i ] ) ){//if should have subs
			code += " rel='tile_" + getShortHREF( subs[ i ] ) + "'>";
			code += stripTags( getCatName( subs[ i ] ) );
		} else if( getCatHREF( subs[ i ] ).indexOf( "jccmanhattan.org" ) != -1 && subs[ i ].childNodes[ 0 ].target != "_new" &&  getCatHREF( subs[ i ] ).indexOf( "?page=" ) == -1 &&  getCatHREF( subs[ i ] ).indexOf( "programs.aspx" ) == -1){//if category is not a redirect
			code += " rel='load_" + getShortHREF( subs[ i ] ) + "'>";
			code += stripTags( getCatName( subs[ i ] ) );
		} else {//outside link
			code += "><a href='" + getShortHREF( subs[ i ] ) + "' >";
			code += stripTags( getCatName( subs[ i ] ) );
			code += "</a>";
		}		
		code += "</li>";
	}
	code += "</ul>";
	/*start do subs
	var subs = childrenByTag( childByClass( main , "subholder" ) , "div" );
	var subSubs;
	for( var i = 0; i < subs.length; i++ ){
		if( hasSubs( subs[ i ] ) ){
			code += printCodeFor( subs[ i ] , getCatName( main ) );
			document.getElementById( "right_nav" ).innerHTML = code;
		}
	}
	end*/
	return code;
}
function deepLinkBreadCrumbs(){
	var BC = document.getElementById( 'BreadCrumb_div' ).getElementsByTagName( 'a' ); 
	for( var i = 0; i < BC.length; i++ ){
		BC[ i ].onclick = function(){
			SWFAddress.setValue(this.rel);
			urchinTracker( '/breadcrumbs/' + this.innerHTML.replace(/&/gi, "and") );
			return false;
		}
		BC[ i ].rel = ( i == 0 ) ? "tile_001" : "tile_" + getShortHREF( BC[ i ].parentNode );
		BC[ i ].res = BC[ i ].href;
		BC[ i ].href = "javascript:void(0)";
	}
}
