var headline_count;
var headline_interval;
var old_headline = 0;
var current_headline=0;

$(document).ready(function(){
		initpage();
});

function XHR(){
	try{ return new XMLHttpRequest(); }catch(e){}
	try{ return new ActiveXObject("Msxml2.XMLHTTP"); }catch(e){}
	try{ return new ActiveXObject("Microsoft.XMLHTTP"); }catch(e){}
	alert("XMLHttpRequest not supported");
	return null;
}

function getNews(){
	var xhr = XHR();
	if(!xhr){ return; }
	xhr.open("GET", "./includes/newsticker.xml", true);

	xhr.onreadystatechange = function(){
		if(xhr.readyState != 4){ return; }
		
		var response = xhr.responseXML.documentElement;
		
		var news = response.getElementsByTagName('news');
		
		var nHeadline = "";
		var nLink = "";
		var ticker = "<div id='tickerheader'>HIGHLIGHTS <span>&gt;</span></div><ul id='scrollup'>";
		
		for(var i = 0; i < news.length; i++){
			nHeadline = news[i].getElementsByTagName('headline')[0].firstChild.nodeValue;
			nLink = news[i].getElementsByTagName('link')[0].firstChild.nodeValue;
			
			ticker += "<li><a href='"+nLink+"'>"+nHeadline+"</a></li>";
		}
		
		ticker += "</ul>";
		
		document.getElementById('newsticker').innerHTML = ticker;
		
	};

	xhr.send(null);
}


function initpage(){
	
	$(" map#smartlinkcluster area ").hover(function(e) {
		var sm_num = $(this).attr("id");
		var thecoord = $(this).attr("coords");
		var hoverbox = $("a#hoverstate");
		var hoverimg = $("a#hoverstate img");
		
		var coordarray = thecoord.split(",");
		
		hoverimg.attr("src","../images/smartlink_ill/sm_"+sm_num+"-hover.png");
		
		// NUMBERED CIRCLE HOVER STATE
		hoverbox.attr("rel","../images/smartlink_ill/sm_panel_"+sm_num+".png");
		hoverbox.css("left",""+coordarray[0]-coordarray[2]+"px");
		hoverbox.css("top",""+coordarray[1]-coordarray[2]+"px");
		
		var leftoff = -10;
		if(sm_num == "four" || sm_num == "five" || sm_num == "six"){
			leftoff = -285;
		}
		
		$('a.jt').cluetip({
		  width: 256,
		  height: 124,
		  dropShadow: false,
		  sticky: true,
		  mouseOutClose: true,
		  closeText: '',
		  topOffset: 0,  
		  leftOffset: leftoff,
		  showTitle: false
		});
		
		//document.getElementById("floatertext").href = "../docs/Smartlinkscreen_"+sm_num+".pdf";
		var poplink = $("a#floatertext");
		poplink.attr("href","../docs/Smartlinkscreen_"+sm_num+".pdf");
		
		hoverbox.show();
		
		e.preventDefault();
	});
	
	$(" a#hoverstate ").hover(function() {
		// THIS FIRES POP UP
		
	},function(){
		var hoverimg = $("a#hoverstate img");
		hoverimg.attr("src","../images/smartlink_ill/1x1clr.gif");
		$(this).attr("rel","../images/smartlink_ill/1x1clr.gif");
		$(this).hide();
	});

	var pageheight = $("div#wrapper").height();
	/*if($("div#rightcontent").height() <= 200){
		alert($("div#rightcontent").height());
		$("div#rightcontent").height(300);
	}
	else if( ($("div#rightcontent").height() + 270) < pageheight){
		alert($("div#rightcontent").height());
		$("div#rightcontent").height(pageheight - 270);
	}
	else{}*/
	/*if($("div#rightcontent").height() <= 270){
	$("div#rightcontent").height(pageheight - 270);
	}*/
		
	// MENU SECTION
	var _active = null;
	var _active2 = null;
	
	var _hmin = 0;
	//@ loop through matches selector
	$("ul#nav>li").each(function(){
		//@ in/out handler
		$(this).hover(
	  
			//@ hover
			function(){
				var _hmax = $(this).find('ul.navcontent').height();
				$(this).find('ul.navcontent').show();
				$(this).find('div.navslider').animate({height: _hmax+"px"}, {queue:false, duration:600});
				//@ set active to current hovered
				_active = $(this);
				
				if ( $(this).find('ul.navcontent>li ul.subnav').length > 0 ){
					$(this).find('ul.navcontent>li').each(function(){
						$(this).hover(
							function(){
								var _hmax2 = $(this).find('ul.subnav').height();
								$(this).find('ul.subnav').show();
								$(this).find('div.subslider').animate({height: _hmax2+"px"}, {queue:false, duration:600});
								_active2 = $(this);
							},
							function() {									
									$(_active2).find('div.subslider').animate({height: _hmin+"px"},
																		  {queue:false, duration:600,easing:'easeInBack'});	
							}
						);
					});
				}	
			},
			//@ out
			function() {
				//@ slide back
				$(_active).find('div.navslider').animate({height: _hmin+"px"},
													  {queue:false, duration:600,easing:'easeInBack'});
			}
		);
	});
	
	//getNews();

	// TEXT SCROLLER
	headline_count = $("ul#scrollup li").size();
	$("ul#scrollup li:eq("+current_headline+")").css('top','5px');
	
	headline_interval = setInterval(headline_rotate,5000); //time in milliseconds
	
	$('ul#scrollup').hover(function() {
		clearInterval(headline_interval);
	}, function() {
		headline_interval = setInterval(headline_rotate,5000); //time in milliseconds
		headline_rotate();
	});
	
	$("a.zoomer").fancybox({'frameWidth':500,'frameHeight':50});
	
	if(document.getElementById('sitetree') != null){
	$("#sitetree").treeview({
		collapsed: true,
		animated: "medium",
		control:"#sitetreecontrol",
		persist: "location"
	});
}
}

function headline_rotate() {
  current_headline = (old_headline + 1) % headline_count; //remainder will always equal old_headline until it reaches headline_count - at which point it becomes zero. clock arithmetic
  $("ul#scrollup li:eq(" + old_headline + ")").animate({top: -31},"slow", function() {
    $(this).css('top','36px');
    });
  $("ul#scrollup li:eq(" + current_headline + ")").show().animate({top: 5},"slow");  
  old_headline = current_headline;
}

