var ie4=document.all;
var ns6=document.getElementById&&!document.all;

timer2 = 5000;
intOpacity2 = 100;
var left, top;
var currentObj;

var yearPoints = 0;
var formPoints = 0;

var screenHeight = document.documentElement.clientHeight;
var screenWidth = document.documentElement.clientWidth;

var RSSFeedArray = [];
var globalCounter = 0;

if (window.addEventListener)			
	window.addEventListener("load", preparePage, false);	
else if (document.addEventListener) 
	document.addEventListener("DOMContentLoaded", preparePage, false);  
else
	window.attachEvent("onload", preparePage);
	
function preparePage()
{
	getRSSContent();
	enableSubmitOver();
}	

function enableSubmitOver()
{
	submitButton = document.getElementById("submit");

	if (window.addEventListener)			
	{
		submitButton.addEventListener("mouseover", function(e) { submitButton.src="/images/submit-over.gif"; }, false);   
		submitButton.addEventListener("mouseout", function(e) { submitButton.src="/images/submit.gif"; }, false);
	}	
	else
	{
		submitButton.attachEvent("onmouseover", function(e) { submitButton.src="/images/submit-over.gif"; });
		submitButton.attachEvent("onmouseout", function(e) { submitButton.src="/images/submit.gif"; });
	}	
}


function getRSSContent()
{		
	if(globalCounter < RSSFeedArray.length)
	{
	   AJAXRequest(RSSFeedArray[globalCounter][0], function() 
	   { 
	   	if (xmlhttp.readyState==4) 
		{ 
			document.getElementById(RSSFeedArray[globalCounter][1]).innerHTML = xmlhttp.responseText; 
			globalCounter++; 
			getRSSContent(); 
		}
	});	   					
	}
}



function fnSwitchMenu(objSwitch, strMode, blnTimeout)
{
  if ((objDropDownItem!="")&&(objDropDownItem!=objSwitch))
    fnRemoveMenu();	
  
  fnFindBrowserStyle(document.getElementById(objSwitch)).display=strMode;
  if (objDropDownItem) changeOpacity(100, objDropDownItem);
  intOpacity = 100;
  
  if (blnTimeout)
    objDropDownItem="";
  else
    clearTimeout(timer);
  
  objDropDownItem = objSwitch;  
}

var DDSPEED = 3;
var DDTIMER = 15;

// main function to handle the mouse events //
function ddMenu(id,d){
  var h = document.getElementById('ddheader'+id);
  var c = document.getElementById('ddcontent'+id);
  c.style.display = "block";
  
  if (document.getElementById('leader-flash'))
  {
	c.style.height = 'auto';
	if (d==-1)
		h.timer = setTimeout(function() { c.style.display = "none"}, 100);
	else
		clearTimeout(h.timer);
  }
  else
  {  	  
	  clearInterval(c.timer);
	  if(d == 1){
		clearTimeout(h.timer);
		if(c.maxh && c.maxh <= c.offsetHeight){ return}
		else if(!c.maxh){
		  c.style.display = 'block';
		  c.style.height = 'auto';
		  c.maxh = c.offsetHeight;
		  c.style.height = '0px';
		}
		c.timer = setInterval(function(){ddSlide(c,1)},DDTIMER);
	  }else{
		h.timer = setTimeout(function(){ddCollapse(c)},50);
	  }
  }
}

// collapse the menu //
function ddCollapse(c){
  c.timer = setInterval(function(){ddSlide(c,-1)},DDTIMER);
}

// cancel the collapse if a user rolls over the dropdown //
function cancelHide(id){
  var h = document.getElementById('ddheader'+id);
  var c = document.getElementById('ddcontent'+id);
  clearTimeout(h.timer);
  clearInterval(c.timer);
  if((c.offsetHeight < c.maxh) && !(document.getElementById('leader-flash'))){
    c.timer = setInterval(function(){ddSlide(c,1)},DDTIMER);
  }
}

// incrementally expand/contract the dropdown and change the opacity //
function ddSlide(c,d){
  var currh = c.offsetHeight;
  var dist;
  if(d == 1){
    dist = (Math.round((c.maxh - currh) / DDSPEED));
  }else{
    dist = (Math.round(currh / DDSPEED));
  }
  if(dist <= 1 && d == 1){
    dist = 1;	
  }
  c.style.height = currh + (dist * d) + 'px';
  if (!ie4)
  {
	c.style.opacity = currh / c.maxh;
	c.style.filter = 'alpha(opacity=' + (currh * 100 / c.maxh) + ')';
  }
  
  //alert(currh);
  
  if((currh <= 2 && d != 1) || (currh > (c.maxh - 2) && d == 1)){
    if (currh==2) c.style.display = "none";
    clearInterval(c.timer);
  }
}

function fadeMenu()
{
  
  intOpacity-=10;
  
  changeOpacity(intOpacity, objDropDownItem);
  
  if (intOpacity <= 0)
    fnRemoveMenu();    
  else
    timer=setTimeout('fadeMenu()', 50);
}

function fnRemoveMenu()
{
  fnFindBrowserStyle(document.getElementById(objDropDownItem)).display="none";
  changeOpacity(100, objDropDownItem);
  intOpacity = 100;
}



function changeOpacity(newOpacity, objObject)
{
    if (ie4)
        fnFindBrowserStyle(document.getElementById(objObject)).filter = "progid:DXImageTransform.Microsoft.Alpha(opacity="+newOpacity+")"
    else
    {
        fnFindBrowserStyle(document.getElementById(objObject)).KhtmlOpacity = (newOpacity/100);
    	fnFindBrowserStyle(document.getElementById(objObject)).MozOpacity = (newOpacity/100);
    }  
}


function showMyPoints(width)
{
	fnFindBrowserStyle(document.getElementById("house-point-form")).display = "none";
	
	mode = "?show=me";
	
	changeHousepoints(mode, 'My house points', width);
}


function changeYearpoints(year, message, width)
{
	yearPoints = year;
	
	if (!year) 
	{
		formPoints = "";
	}
	else
	{
		if (formPoints)
			message += ", form "+formPoints;
		else
			message += " total";
	}		
	
	if (year)	
	{
		fnFindBrowserStyle(document.getElementById("house-point-form")).display = "block";		
		document.getElementById("house-point-form").maxh = "";
	}	
	else
	{
		fnFindBrowserStyle(document.getElementById("house-point-form")).display = "none";
		document.getElementById("house-point-form").maxh = "";
	}
	
	//ddMenu('-1',1);
	showHousePointMenu();
		      
	mode = "?hpy="+year+((formPoints) ? "&fo="+formPoints : "");
	
	changeHousepoints(mode, message, width);
}

function changeFormpoints(form, message, width)
{
	formPoints = form;
	
	if (yearPoints)
	{
		message = "Year "+yearPoints;
		if (formPoints) 
			message += ", form "+formPoints;
		else
			message += " total";
	}
	
	mode = "?fo="+form+((yearPoints) ? "&hpy="+yearPoints : "");
	
	changeHousepoints(mode, message, width)
}


function changeHousepoints(mode, message, width)
{
	fnFindBrowserStyle(document.getElementById("ddcontent-1")).display = "none";
	var so = new SWFObject("amcolumn.swf", "amcolumn", width, "106", "8", "#FFFFFF");
	so.addVariable("path", "");		
	so.addVariable("settings_file", encodeURIComponent("housepoint-stream.php"+mode+"&bounce=1"));
	//so.addVariable("data_file", "requestfilter.php?m=hp");
	so.addVariable("preloader_color", "#000000");
	so.write("house-point-chart");	
	
	document.getElementById("ddheader-1").innerHTML = "Showing: <strong>"+message+"</strong>";	
	//ddMenu('-1',-1);
}


function hideHousePointMenu()
{  fnFindBrowserStyle(document.getElementById("ddcontent-1")).display = "none";  }

function showHousePointMenu()
{  fnFindBrowserStyle(document.getElementById("ddcontent-1")).display = "block";  }

function xmlInit()
{
  xmlhttp=false;
  
/*@cc_on @*/
/*@if (@_jscript_version >= 5)
// JScript gives us Conditional compilation, we can cope with old IE versions.
// and security blocked creation of the objects.
 try 
 {
   xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
 } 
 catch (e) 
 {
  try 
  {
    xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
  } 
  catch (E) 
  {
    xmlhttp = false;
  }
 }
@end @*/
	if (!xmlhttp && typeof XMLHttpRequest!='undefined')
	  xmlhttp = new XMLHttpRequest();
}


function AJAXRequest(Filename, CallbackFunction)
{
  xmlInit();
  xmlhttp.open("GET", Filename, true);
  xmlhttp.onreadystatechange=CallbackFunction;
  xmlhttp.send(null)
}


function cb_DisplayCalendar()
{
	if (xmlhttp.readyState==4) // Ensure it only attempts to populate details on success.
	{
	  Results = xmlhttp.responseText;
	  
	  document.getElementById("calendar-of-events").innerHTML = Results;
	}
}


function keepEvent()
{
  changeOpacity(100, "popupCalendar");
  intOpacity = 100;
  
  clearTimeout(timer2);
}

function popupEvent(thisObj, message, event)
{	
	if (currentObj != thisObj)
	{
		var curLeft = thisObj.offsetWidth;
		var curTop = 0;
		currentObj = thisObj;
		
		if (thisObj.offsetParent) 
		{
			do {
						curLeft += thisObj.offsetLeft;
						curTop += thisObj.offsetTop;

			} while (thisObj = thisObj.offsetParent);		
		}

		left = curLeft;
		top = curTop+5;
		
		fnFindBrowserStyle(document.getElementById("popupCalendar")).left = (left) + "px";
		fnFindBrowserStyle(document.getElementById("popupCalendar")).top = (top) + "px";							
	}	
	
	document.getElementById("popupCalendar").innerHTML = "<a href=\"javascript:fnRemovePopup();void(0);\"><img src=\"images/close.gif\" alt=\"Close\" id=\"close\" /></a>"+message;
	fnFindBrowserStyle(document.getElementById("popupCalendar")).display = "block";
	
	keepEvent();	
}


function popupDownEvent()
{
	timer2=setTimeout('fadePopup()', 1000);
}


function fadePopup()
{
  
  intOpacity2-=10;   
  
  changeOpacity(intOpacity2, "popupCalendar");
  
  if (intOpacity2 <= 0)
    fnRemovePopup();    
  else
    timer2=setTimeout('fadePopup()', 50);
}

function changeOpacity(newOpacity, elementId)
{
    //if (ie4)
        //fnFindBrowserStyle(document.getElementById(elementId)).filter = "progid:DXImageTransform.Microsoft.Alpha(opacity="+newOpacity+");"
    //else
    //{
        fnFindBrowserStyle(document.getElementById(elementId)).KhtmlOpacity = (newOpacity/100);
    	fnFindBrowserStyle(document.getElementById(elementId)).MozOpacity = (newOpacity/100);
    //}  
}

function fnRemovePopup()
{
  fnFindBrowserStyle(document.getElementById("popupCalendar")).display="none";
  changeOpacity(100, "popupCalendar");
  intOpacity2 = 100;
}


function highlightMenu(menuId)
{  document.getElementById(menuId).className="menOver";	 }

function unHighlightMenu(menuId)
{  document.getElementById(menuId).className="";  }

function fnFindBrowserStyle(objIdent)
{ return ((ie4||ns6) ? objIdent.style : objIdent); }
