var timeout	= 500;
var closetimer	= 0;
var ddmenuitem	= 0;

// open hidden layer
function mopen(id)
{	
	// cancel close timer
	mcancelclosetime();

	// close old layer
	if(ddmenuitem) ddmenuitem.style.visibility = 'hidden';

	// get new layer and show it
	ddmenuitem = document.getElementById(id);
	var menueval;
	menueval = document.getElementById('menueval');
	menucoord = findPos(menueval);
	ddmenuitem.style.left = menucoord[0]+'px';
	ddmenuitem.style.top = menucoord[1]+menueval.offsetHeight+'px';
	ddmenuitem.style.position = 'absolute';
	ddmenuitem.style.visibility = 'visible';

}
// close showed layer
function mclose()
{
	if(ddmenuitem) ddmenuitem.style.visibility = 'hidden';
}

// go close timer
function mclosetime()
{
	closetimer = window.setTimeout(mclose, timeout);
}

// cancel close timer
function mcancelclosetime()
{
	if(closetimer)
	{
		window.clearTimeout(closetimer);
		closetimer = null;
	}
}

// close layer when click-out
document.onclick = mclose; 

function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		do {
			curleft += obj.offsetLeft;
			curtop += obj.offsetTop;
		} while (obj = obj.offsetParent);
	return [curleft,curtop];
    }
}






function init()
{
  Effect.Appear('wraper', { duration: 1.0 });
  setTimeout( init2, 1000);
}
function init2()
{
  new Effect.Morph('main', { style: 'height:400px;', duration: 0.5 });
  setTimeout( init3, 500);
}
function init3()
{
  if(document.getElementById("left")) Effect.Appear('left', { duration: 0.5 });
  if(document.getElementById("left")) setTimeout( init4, 500);
  else init4();
}
function init4()
{
  if(document.getElementById("right")) Effect.Appear('right', { duration: 0.5 });
  if(document.getElementById("wide")) Effect.Appear('wide', { duration: 0.5 });
  setTimeout( init5, 500);
}
function init5()
{
  var mainheight = 0;
  if(document.getElementById("left") && document.getElementById("right"))
  {
    leftheight = $('left').offsetHeight;
    rightheight = $('right').offsetHeight;
	
    if(leftheight > rightheight)
    {
      mainheight = leftheight;
    }
    else
    {
      mainheight = rightheight;
    }
  }
  if(document.getElementById("wide"))
  {
    wideheight = $('wide').offsetHeight;
    mainheight = mainheight+wideheight;
  }
 new Effect.Morph('main', { style: 'height:'+mainheight+'px;', duration: 0.5});  
}


var section;
function gotolink(getsection)
{
  section = getsection;
  new Effect.Morph('main', { style: 'height:400px;', duration: 0.5 });
  if(document.getElementById("left")) { Effect.Fade('left', { duration: 0.5}); }
  if(document.getElementById("wide")) { Effect.Fade('wide', { duration: 0.5}); }
  setTimeout(goto1, 500);
}
function goto1()
{
  if(document.getElementById("right")) { Effect.Fade('right', { duration: 0.5}); }
  setTimeout(goto2, 500);
}
function goto2()
{
  new Ajax.Updater('main', '/pages/'+section+'.php?ajax=1', { method: 'get' });
  setTimeout(goto3, 500);
}
function goto3()
{
  if(document.getElementById("left")) { Effect.Appear('left', { duration: 0.5 }); }
  if(document.getElementById("wide")) { Effect.Appear('wide', { duration: 0.5 }); }
  setTimeout(goto4, 500);
}
function goto4()
{
  if(document.getElementById("right")) Effect.Appear('right', { duration: 0.5 });
  var mainheight = 0;
  if(document.getElementById("left") && document.getElementById("right"))
  {
    leftheight = $('left').offsetHeight;
    rightheight = $('right').offsetHeight;
	
    if(leftheight > rightheight)
    {
      mainheight = leftheight;
    }
    else
    {
      mainheight = rightheight;
    }
  }
  if(document.getElementById("wide"))
  {
    wideheight = $('wide').offsetHeight;
    mainheight = mainheight+wideheight;
  }
 new Effect.Morph('main', { style: 'height:'+mainheight+'px;', duration: 0.5});
}


function popup(section)
{
  href = 'http://www.sma-technology.com/pages/evalpopup.php?section='+section
  window.open(href, section, 'scrollbars=yes');
}

function popupsmb(section)
{
  href = 'http://www.sma-technology.com/pages/evalpopup.php?section='+section+'&smb=1'
  window.open(href, section, 'scrollbars=yes');
}

function tallysurvey()
{
  var sections = ["culturescore", "compliancescore", "isdeptscore", "stakeholderscore", "operationsscore", "isinfrascore", "isnetscore", "securityscore"];
  var countsects = 0;
  var allscores = [];
  for(var i in sections)
  {
    var thissection = document.getElementById(sections[i]);
	if(thissection != null)
	{
	  // Check Each Section to See if it has a score, if yes, calculate score and get graph
	  if(thissection.value != 0)
	  {
	    document.getElementById("graphs").style.visibility="visible";
		var asfraction = thissection.value/5;
		var aspercent = asfraction * 100;
		aspercent = Math.round(aspercent);
		
        new Ajax.Updater(sections[i]+'a', '/pages/evalsubmission.php?score='+aspercent+'&section='+sections[i], { method: 'get' });
        countsects++;
		allscores[allscores.length] = aspercent;
	  }
	}
  }
  
  var totalscore = 0;
  var temp = 0;
  
  for(i=0;i< countsects; i++)
  {
    totalscore = totalscore + allscores[i];
  }
  totalscore = totalscore/countsects;
  totalscore = Math.round(totalscore);

  new Ajax.Updater('totalscore', '/pages/evalsubmission.php?score='+totalscore+'&section=total', { method: 'get' });
	
  if(countsects == 8)
  {
    new Ajax.Updater('left', '/pages/evaldone.php?totalscore='+totalscore, { method: 'get' });
  }
}

// BELOW IS THE IMAGE FLIP FOR PARTNERS SECTION
function flipem(a,b)
{
  new Effect.Move(a, { x: -300, y: 0, mode: 'relative' });
  setTimeout((function(){flipem2(a,b);}), 1000);
}
function flipem2(a,b)
{
  new Effect.Parallel([
    new Effect.Move(b, { sync: true, x: 300, y: 0, mode: 'relative' }), 
    new Effect.Fade(b, { sync: true }),
  ], {
    duration: 1
  });
  setTimeout((function(){flipem3(a,b);}), 1000);
}
function flipem3(a,b)
{
  new Effect.Parallel([
    new Effect.Move(a, { sync: true, x: 300, y: 0, mode: 'relative' }), 
    new Effect.Appear(a, { sync: true }),
  ], {
    duration: 1
  });
  new Effect.Move(b, { x: -300, y: 0, mode: 'relative' });
  setTimeout((function(){thinkagain(a);}), 1000);
}

var partners = ['cdw', 'comptia', 'microsoft', 'mindshare', 'untangle'];

function think()
{
  var a = partners.shift();
  partners.push(a);
  var b = partners.shift();
  partners.push(b);
  flipem(a,b);
}
function thinkagain(a)
{
  var b = partners.shift();
  partners.push(b);
  setTimeout((function(){flipem(b,a);}), 1000);
}
// END IMAGE FLIP


window.onload = init;
