// JavaScript Document

var FlashMe = new Class({

 initialize: function(menuElement) {

  this.menu = menuElement;
  this.id = menuElement.id;
  this.duration = 400;
  this.buttons = [];

  $A(this.menu.getElementsByTagName('li')).each(
   function(li) {
    if(li.parentNode == menuElement) { this.buttons.push($(li)); }
   }.bind(this)
  );
  this.submenus = $A(this.menu.getElementsByTagName('div'));
  
  this.submenus.each(
   function(submenu) {
    submenu = $(submenu);
    submenu.setStyle('display','block');
    submenu.originalHeight = submenu.offsetHeight;
    submenu.effect = new Fx.Style(submenu, 'height',{ duration: this.duration, transition:Fx.Transitions.quartOut });
    submenu.effect.set(0);
   }.bind(this)
  );
   
  

  this.buttons.each(
   function(button) {
    button.addEvent('mouseover',this.expand.bindAsEventListener(this));
    button.addEvent('mouseout', this.collapse.bindAsEventListener(this));
   }.bind(this)
  );

 },

 findButton: function(element) {
  var button = false;
  while(element.parentNode) {
   if(this.buttons.test(element)) { button = element; }
   element = element.parentNode;
  }
  return button;
 },

 findSubmenu: function(element) {
  var button = this.findButton(element);
  var submenu = button.getElementsByTagName('div')[0];
  return submenu;
 },

 expand: function(event) {
  var submenu = this.findSubmenu(event.target || event.srcElement);
  if(submenu) {
   submenu.effect.clearTimer();
   submenu.effect.start(submenu.originalHeight);

  }
 },

 collapse: function(event) {
  var submenu = this.findSubmenu(event.target || event.srcElement);
  if(submenu) {
   submenu.effect.clearTimer();
   submenu.effect.start(0);
  }
 }

});


window.addEvent('domready', cloak_init);

contentHover_width = 0;
contentHover_height = 0;

function cloak_init(){$$("a.contentOver").each(function(el){el.onclick=cloak_bind});}

function cloak_bind(event) { var event = new Event(event); event.preventDefault(); this.blur(); this.onclick=cloak_bind; cloak_Mass(this.href);	return false; }

function cloak_Mass(url) {
	var params = cloak_parseQuery( url );
	var contentHover_width = (params['width']*1);
	var contentHover_height = (params['height']*1);
	var contentHover_linksto = (params['linksto']);
	var contentHover_content = (params['content']);
	var contentHover_type = (params['type']);

	if ( !$("cloakColor") ) {
		new Element('iframe').setProperty('id', 'contentCloak').injectInside(document.body);
		$('contentCloak').setOpacity(0.6);
		new Element('div').setProperty('id', 'cloakColor').injectInside(document.body);
		$('cloakColor').setOpacity(0.6);
		cloak_Size();
		new Element('div').setProperty('id', 'contentHover').injectInside(document.body);
		contentHover_xy(url);
	}
	else { window.onresize=null; window.onscroll=null;}
	//use for forgot password
	if (url.indexOf('internal') != -1) {
		$("contentHover").innerHTML += "<iframe name='SSgA_inner' frameborder='0' framespacing='0' border='0' hspace='0' src='"+contentHover_content+"' style='width:"+(contentHover_width-2)+"px;height:"+(contentHover_height)+"px;overflow-x:hidden;' id='SSgA_inner'></iframe>";		
	}
	//use when leaving site and opening a new window
	if (url.indexOf('external') != -1) {
		$("contentHover").innerHTML += "<iframe name='SSgA_outer' frameborder='0' framespacing='0' border='0' hspace='0' src='"+contentHover_content+"' style='width:"+(contentHover_width-2)+"px;height:"+(contentHover_height-40)+"px;' id='SSgA_outer'></iframe><div id='buttons'><a href='"+contentHover_linksto+"' target='blank' id='btnOk' class='confirmed'/><a href='#' id='btnCancel' class='declined' /></div>";	
	}
	//use when leaving site and opening a new window
	if (url.indexOf('leavingUS') != -1) {
		$("contentHover").innerHTML += "<iframe name='SSgA_outer' frameborder='0' framespacing='0' border='0' hspace='0' src='"+contentHover_content+"' style='width:"+(contentHover_width-2)+"px;height:"+(contentHover_height-40)+"px;' id='SSgA_outer'></iframe><div id='buttons'><a href='"+contentHover_linksto+"' id='btnOk' class='confirmed'/><a href='#' id='btnCancel' class='declined' /></div>";	
	}
	if (url.indexOf('cornerLogin') != -1) {
		$("contentHover").innerHTML += "<iframe name='SSgA_over' frameborder='0' framespacing='0' border='0' hspace='0' src='"+contentHover_content+"' style='width:"+(contentHover_width-2)+"px;height:"+(contentHover_height-40)+"px;' id='SSgA_over'></iframe><div id='buttons'><a href='#' id='btnOk' class='cornerLog'/><a href='#' id='btnCancel' class='loginDeclined' /></div>";	
	}

	if (url.indexOf('innerSystem') != -1) {
		$("contentHover").innerHTML += "<iframe name='SSgA_clients' frameborder='0' framespacing='0' border='0' hspace='0' src='"+contentHover_content+"' style='width:"+(contentHover_width-2)+"px;height:"+(contentHover_height-40)+"px;' id='SSgA_over'></iframe><div id='buttons'><a id='btnAccept' name='acceptable' class='ssgaLog' /><a name='declined' id='btnCancel' class='declined'/></div>";	
	}
	if (url.indexOf('globalProducts') != -1) {
		$("contentHover").innerHTML += "<iframe name='SSgA_global' frameborder='0' framespacing='0' border='0' hspace='0' src='"+contentHover_content+"' style='width:"+(contentHover_width-2)+"px;height:"+(contentHover_height-40)+"px;' id='SSgA_over'></iframe><div id='buttons'><a name='accept' id='btnYes' class='globalFund'/><a name='cancel' id='btnNo' class='declined' /></div>";	
	}

	if ( $("cloakColor") ) {
		window.onresize=function(){cloak_Size();contentHover_xy(url);}
		window.onscroll=function(){contentHover_xy(url);}
		}
		else { window.onresize=null; window.onscroll=null;}
	
	var cloakVeil = $('cloakColor'); 
	var cancelLink = $('btnCancel'); 
	var colorVeil = $('contentCloak');
	var contentVeil = $('contentHover');
	
	
	var cloakButtons = $$('#contentHover div#buttons a.declined'); // seperated from confirmation because of DOM propagation
	cloakButtons.addEvent('click', function(e){e = new Event(e).stop(); overlayOff(); });
	

	var loginCancel = $$('#contentHover div#buttons a.loginDeclined'); 
	loginCancel.addEvent('click', function(e){e = new Event(e).stop();
				if($('ButtonLogin')){
					document.loginForm.display_pwd.style.display='none'; 
					$('passwordX').style.display='block';
				} 
				$('passwordX').focus(); 
				overlayOff(); 
				});


	var cloakButtonOK = $$('#contentHover div#buttons a.confirmed'); 
	cloakButtonOK.addEvent('click', function(e){e = new Event(e); overlayOff(); });

	var cloakButtonAccept = $$('#contentHover div#buttons a.ssgaLog'); 
	cloakButtonAccept.addEvent('click', function(e){e = new Event(e); overlayOff(); });

	var cloakGlobalProducts = $$('#contentHover div#buttons a.globalFund'); 
	cloakGlobalProducts.addEvent('click', function(e){e = new Event(e); setFlashAction(); overlayOff();});

	var cloakButtonCorner = $$('#contentHover div#buttons a.cornerLog'); 
	cloakButtonCorner.addEvent('click', function(e){e = new Event(e); clientLogin(); overlayOff(); });

	cloakVeil.addEvent('click', function(e){e = new Event(e).stop();overlayOff();window.onresize=null;window.onscroll=null;});
	fixMacOpacity();
	

}

function moveThis(what, down, over) { 
	var answerOf = $(what);
	answerOf.setStyles({ "top": (window.getScrollTop() + (window.getHeight() - down)/2)+'px', "left": 		(window.getScrollLeft() + (window.getWidth() - over)/2)+'px' });

}

if($$("a.contentClose")){

	function closeLink(){
		var cloakVeil = window.parent.document.getElementById('cloakColor'); 
		var colorVeil = window.parent.document.getElementById('contentCloak'); 
		var contentVeil = window.parent.document.getElementById('contentHover'); 
		cloakVeil.remove(); 
		colorVeil.remove(); 
		contentVeil.remove();
		
								
	}
}

function contentHover_xy(url) {
	if ( $("cloakColor") ){
	var params = cloak_parseQuery( url );
	var contentHover_width = (params['width']*1);
	var contentHover_height = (params['height']*1);
	var contentHover_linksto = (params['linksto']);
	var contentHover_content = (params['content']);
	$("contentHover").setStyles({"width": (contentHover_width-2)+'px', "height": contentHover_height+'px', "left": (window.getScrollLeft() + (window.getWidth() - contentHover_width)/2)+'px', "top": (window.getScrollTop() + (window.getHeight() - contentHover_height)/2)+'px'});
	}
}

function overlayOff() {
	var cloakVeil = $('cloakColor');
	var colorVeil = $('contentCloak');
	var contentVeil = $('contentHover');
	cloakVeil.remove();
	colorVeil.remove(); 
	contentVeil.remove();
}

function cloak_Size(){
	$("contentCloak").setStyles({"height": '0px', "width": '0px'});
	$("contentCloak").setStyles({"height": window.getScrollHeight()+'px', "width": window.getScrollWidth()+'px'});
	$("cloakColor").setStyles({"height": '0px', "width": '0px'});
	$("cloakColor").setStyles({"height": window.getScrollHeight()+'px', "width": window.getScrollWidth()+'px'});
}

function cloak_parseQuery ( query ) {
	// return empty object
	if( !query )
		return {};
	var params = {};
	// parse query
	var pairs = query.split(/[;&]/);
	for ( var i = 0; i < pairs.length; i++ ) {
		var pair = pairs[i].split('==');
		if ( !pair || pair.length != 2 )
			continue;
		// unescape both key and value, replace "+" with spaces in value
		params[unescape(pair[0])] = unescape(pair[1]).replace(/\+/g, ' ');
   }
   return params;
}

function fixMacOpacity() {
   var userAgent = navigator.userAgent.toLowerCase();
   if (userAgent.indexOf('mac') != -1 && userAgent.indexOf('firefox')!=-1) {
     var yourShade = document.getElementById("cloakColor");
	 var notherShade = document.getElementById("contentCloak");
     notherShade.setOpacity(1);
     yourShade.setOpacity(1);
	 yourShade.style.backgroundImage= "url(/images/contentCloak.png)";
     yourShade.style.backgroundRepeat="repeat";
   }
 }

//------------------functions for Login

var hostess = document.location.host;

function clientLogin() {
	
	document.forms['loginForm'].action='https://' + hostess + '/siteminderagent/forms/InternetRetry.fcc';
	document.forms['loginForm'].TARGET.value='$SM$https://' + hostess + '/weblogic/private/privateHome';
	document.forms['loginForm'].submit();
		
}


function loginSSgA() {
	cloak_Mass('&type==cornerLogin&content==/disclaimers/SSgA_disclaimer3.html&height==315&width==550&linksto==X')
}


function enterSender(event) {
	if (window.event && window.event.keyCode == 13) { 
		loginSSgA();
		document.getElementById('passwordX').blur();
		document.getElementById('userX').blur();
	}
	if (event && event.which == 13) {
		loginSSgA();
		document.getElementById('passwordX').blur();
		document.getElementById('userX').blur();
	}
	else {return true;}
}
