var mImg = new Array();

var curID = 0;
var ssTimeout = false;
var stopFade = false;
var isFading = false;
var ssLinks = new Array('http://agenda.greenerevanston.org/community-choice-aggregation-for-electricity',
			'#',
			'#',
			'#',
			'http://agenda.greenerevanston.org/community-choice-aggregation-for-electricity');
var ssTargets = new Array('_blank',
            '',
            '',
            '',
            '_blank');

function preloadSS() {
 for (i=0;i<ssLinks.length;i++) {
  mImg[i] = new Image();
  mImg[i].src = 'images/main.'+i+'.jpg';
 }

 arrowLOver = new Image();
 arrowLOver.src = 'images/arrowLOver.gif';
 arrowROver = new Image();
 arrowROver.src = 'images/arrowROver.gif';

 ssTimeout = setTimeout('startFade()', 2000);
}

function startFade() {
 clearTimeout(ssTimeout);

 if (!document.getElementById('SSHolder'))
  return;

 curID++;
 if (curID > ssLinks.length - 1)
  curID = 0;
 document.getElementById('SSHolder').style.background = "URL('images/main."+curID+".jpg') no-repeat";
 ssTimeout = setTimeout('doFade(100)', 50);
}

function doFade(nm) {
 isFading = true;
 clearTimeout(ssTimeout);
 nm -= 10;
 if (nm < 0)
  nm == 0;

 o = document.getElementById('SSImg');
 o.style['filter'] = 'alpha(opacity='+nm+')';
 tmpOpac = nm/100;
 o.style['-moz-opacity'] = tmpOpac;
 o.style['-khtml-opacity'] = tmpOpac;
 o.style['opacity'] = tmpOpac;

 if (nm == 0) {
  o = document.getElementById('SSImg');
  o.src = 'images/main.'+curID+'.jpg';
  o.style['filter'] = 'alpha(opacity=100)';
  o.style['-moz-opacity'] = 1;
  o.style['-khtml-opacity'] = 1;
  o.style['opacity'] = 1;
  document.getElementById('SSLink').href = ssLinks[curID];
  document.getElementById('SSLink').target = ssTargets[curID];
  isFading = false;
  if (!stopFade)
   ssTimeout = setTimeout('startFade()', 2000);
  else
   clearTimeout(ssTimeout);
 } else {
 ssTimeout = setTimeout('doFade('+nm+')', 50);
 }
}

function ssPrev() {
 clearTimeout(ssTimeout);
 stopFade = true;
 curID--;
 if (curID < 0)
  curID = ssLinks.length - 1;

 if (isFading) {
  o = document.getElementById('SSImg');
  o.src = 'images/main.'+curID+'.jpg';
  o.style['filter'] = 'alpha(opacity=100)';
  o.style['-moz-opacity'] = 1;
  o.style['-khtml-opacity'] = 1;
  o.style['opacity'] = 1;
  document.getElementById('SSLink').href = ssLinks[curID];
  isFading = false;
 }

 document.getElementById('SSHolder').style.background = "URL('images/main."+curID+".jpg') no-repeat";
 ssTimeout = setTimeout('doFade(100)', 50);
}

function ssNext() {
 clearTimeout(ssTimeout);
 stopFade = true;
 curID++;
 if (curID > ssLinks.length - 1)
  curID = 0;

 if (isFading) {
  o = document.getElementById('SSImg');
  o.src = 'images/main.'+curID+'.jpg';
  o.style['filter'] = 'alpha(opacity=100)';
  o.style['-moz-opacity'] = 1;
  o.style['-khtml-opacity'] = 1;
  o.style['opacity'] = 1;
  document.getElementById('SSLink').href = ssLinks[curID];
  isFading = false;
 }

 document.getElementById('SSHolder').style.background = "URL('images/main."+curID+".jpg') no-repeat";
 ssTimeout = setTimeout('doFade(100)', 50);
}




var totalMenus = 9;
var subVisible = false;

function toggleSubMenu(nm, p) {
 o = document.getElementById('subMenu'+nm);
 if (o.style.visibility == 'visible') {
  o.style.visibility = 'hidden';
  document.getElementById('menu'+nm).className = 'menu';
  subVisible = false;
 } else {
  for (i=0;i<totalMenus;i++) {
   if (document.getElementById('subMenu'+i) && i != nm) {
    document.getElementById('subMenu'+i).style.visibility = 'hidden';
    document.getElementById('menu'+i).className = 'menu';
   }
  }
  pos = getPOS(p);
  o.style.top = pos[1] + 5;
  o.style.left = pos[0] - 18;
  o.style.visibility = 'visible';
  subVisible = true;
  document.getElementById('menu'+nm).className = 'menuActive';
 }
}

function getPOS(p) {
 tmpX = p.offsetLeft;
 tmpY = p.offsetTop + p.offsetHeight;
 while (p.offsetParent) {
  p = p.offsetParent;
  tmpX += p.offsetLeft;
  tmpY += p.offsetTop;
 }
 outArr = new Array(tmpX, tmpY);
 return outArr;
}





//----- FEEDBACK
var fadeTimeout = false;
function showFeedback() {
 clearTimeout(fadeTimeout);
 tmpH = getDocHeight() + 20;
 if (document.getElementById('theCover').offsetHeight < tmpH)
  document.getElementById('theCover').style.height = tmpH + 'px';
 document.getElementById('theCover').style.visibility = 'visible';
 document.getElementById('theCover').style['filter'] = 'alpha(opacity=0)';
 document.getElementById('theCover').style['-moz-opacity'] = 0; 
 document.getElementById('theCover').style['-khtml-opacity'] = 0;
 document.getElementById('theCover').style['opacity'] = 0;
 fadeTimeout = setTimeout('fadeyIn(0)', 50);

 if (navigator.userAgent.indexOf('MSIE') > -1) {
  tmpH = document.body.scrollHeight - 600;
  document.getElementById('theWindow').style.padding = tmpH+'px 0px 0px 0px';
 } else {
  document.getElementById('theWindow').style.position = 'fixed';
 }
 document.getElementById('theWindow').style.visibility = 'visible';
 document.getElementById('theFeedback').style.width = '500px';
 document.getElementById('theFeedback').style.height = '400px';
}

function fadeyIn(nm) {
 clearTimeout(fadeTimeout);
 nm += 10;
 if (nm > 80)
  nm = 80;

 document.getElementById('theCover').style['filter'] = 'alpha(opacity='+nm+')';
 tmpOpac = nm / 100;
 document.getElementById('theCover').style['-moz-opacity'] = tmpOpac; 
 document.getElementById('theCover').style['-khtml-opacity'] = tmpOpac;
 document.getElementById('theCover').style['opacity'] = tmpOpac;

 if (nm < 80)
  fadeTimeout = setTimeout('fadeyIn('+nm+')', 50);
}

function hideFeedback() {
 document.getElementById('theWindow').style.visibility = 'hidden';
 document.getElementById('theCover').style.visibility = 'hidden';
}

function getDocHeight() {
    var D = document;
    return Math.max(
        Math.max(D.body.scrollHeight, D.documentElement.scrollHeight),
        Math.max(D.body.offsetHeight, D.documentElement.offsetHeight),
        Math.max(D.body.clientHeight, D.documentElement.clientHeight)
    );
}
