/***** MAIN MENU FUNCTIONS *****/

function mmLink() {
  this.id = arguments[0];
  this.list = arguments[1];
  this.hover = false;
  this.listHover = false;
  this.locked = false;
  this.initialize = mmInitialize;
  this.setPending = mmSetPending;
  this.clearPending = mmClearPending;
  this.setPendingExpire = mmSetPendingExpire;
  this.clearPendingExpire = mmClearPendingExpire;
}

function mmSetPending() {
  var jQid = "#" + this.id;
  var jQlistId = "#" + this.list;
  
  $("#mm a.pending").removeClass("pending");
  $(jQid).addClass("pending");
  meh = clearTimeout(window.mmPendingTimeout);
  window.mmPendingTimeout = setTimeout("mmSetActive('" + jQid + "', '" + jQlistId + "')", window.mmPendingInterval);
}

function mmClearPending() {
  var jQid = "#" + this.id;
  $(jQid).removeClass("pending");
}

function mmSetActive(x,y) {
  if ($(x).is(".pending")) {
    $("#mm a.active").removeClass("active");
    $("#mm2 a.active").removeClass("active");
    $("#mm-arrow").remove();
    $("#mm .sub-menu").removeClass("active");
    $("#mm2 .sub-menu").removeClass("active");
    $("#mm .sub-menu").hide();
    $("#mm2 .sub-menu").hide();
    $(x).removeClass("pending");
    $(x).addClass("active");

    if ($(y).is(".left")) {
      $(x).parent().append('<div id="mm-arrow" class="left"></div>');
    } else {
      $(x).parent().append('<div id="mm-arrow" class="right"></div>');
    }

    $(y).addClass("active");
    $(y).fadeIn();
  }
}

function mmSetPendingExpire() {
  var jQid = "#" + this.id;
  var jQlistId = "#" + this.list;
  
  $(jQid).addClass("pendingExpire");
  window.mmExpireTimeout = setTimeout("mmExpire('" + jQid + "', '" + jQlistId + "')", window.mmExpireInterval);
  
}

function mmClearPendingExpire() {
  var jQid = "#" + this.id;
  $(jQid).removeClass("pendingExpire");
  window.mmExpireTimeout = "";
}

function mmExpire(x,y) {
  if ($(x).is(".pendingExpire")) {
    if ($(x).is(".active")) {
      $(x).removeClass("active");
      $("#mm-arrow").remove();
    }
    $(x).removeClass("pendingExpire");
    $(y).hide();
    $(y).removeClass("active");
  }
}

function mmInitialize() {
  var jQid = "#" + this.id;
  var jQlistId = "#" + this.list;
  var jQref = this;

  if ($(jQid).is('.locking')) {           // Code for locking menus

    $(jQlistId).click( function (event) {
      jQref.locked = true;
    });

    $(jQlistId + " .mm-close").click( function (e) {
      if ($(jQid).is(".active")) {
        $(jQid).removeClass("active");
        $("#mm-arrow").remove();
      }
      jQref.locked = false;
      $(jQid).removeClass("pendingExpire");
      $(jQlistId).hide();
      $(jQlistId).removeClass("active");
      e.preventDefault();
      e.stopPropagation();
    });

  }
  
  if($(jQlistId).length > 0) {  // if the drop menu element even exists
	  
  $(jQid).mouseover( function () {
    jQref.hover = true;
	if ($(jQid).is('.pending') == false && $(jQid).is('.active') == false) {
	  jQref.setPending();
	}
	if ($(jQid).is('.pendingExpire') == true) {
	  jQref.clearPendingExpire();
	}
  });
  
  
  $(jQlistId).mouseover( function () {
    jQref.listhover = true;
	if ($(jQid).is('.pending') == false && $(jQid).is('.active') == false) {
	  jQref.setPending();
	}
	if ($(jQid).is('.pendingExpire') == true) {
	  jQref.clearPendingExpire();
	}
  });
  
  $(jQid).mouseout( function() {
    jQref.hover = false;
	if (jQref.listHover == false && jQref.locked == false) {
      if ($(jQid).is('.pending')) {
	    jQref.clearPending();
	  } else if ($(jQid).is('.active')) {
		jQref.setPendingExpire();
	  }
	}
  });
  
  $(jQlistId).mouseout( function() {
    jQref.listHover = false;
	if (jQref.hover == false && jQref.locked == false) {
      if ($(jQid).is('.pending')) {
	    jQref.clearPending();
	  } else if ($(jQid).is('.active')) {
		jQref.setPendingExpire();
	  }
	}
  });
  
  } // end IF jQlistID
}

/***** END MAIN MENU FUNCTIONS *****/

/***** FORM FUNCTIONS *****/

function cep_getRadioVal(radioObj) {
	if(!radioObj)
		return "";
	var radioLength = radioObj.length;
	if(radioLength == undefined)
		if(radioObj.checked)
			return radioObj.value;
		else
			return "";
	for(var i = 0; i < radioLength; i++) {
		if(radioObj[i].checked) {
			return radioObj[i].value;
		}
	}
	return "";
}

function formatCurrencyVar(num, showCents) {
  showCents = typeof(showCents) != 'undefined' ? showCents : 1;
  num = num.toString().replace(/\$|\,/g, '');
  if (isNaN(num)) num = "0";
  sign = (num == (num = Math.abs(num)));
  num = Math.floor(num * 100 + 0.50000000001);
  cents = num % 100;
  num = Math.floor(num / 100).toString();
  if (cents < 10) cents = "0" + cents;
  for (var i = 0; i < Math.floor((num.length - (1 + i)) / 3); i++)
  num = num.substring(0, num.length - (4 * i + 3)) + ',' + num.substring(num.length - (4 * i + 3));
  if (showCents) {
    return (((sign) ? '' : '-') + '$' + num + '.' + cents);
  } else {
	return (((sign) ? '' : '-') + '$' + num);
  }
}


/***** PAGE INITIALIZATION *****/

$(document).ready( function() {

  /*** MENU INITIALIZATION ***/					   
  window.mmExpireInterval = 500;
  window.mmPendingInterval = 250;
  
  window.mmPendingTimeout = '';
  window.mmExpireTimeout = '';
  
  mmArray = new Array();
  
  mmPartners = new mmLink("mm-partners", "sm-partners");
  mmVision = new mmLink("mm-vision", "sm-vision");
  mmRetire = new mmLink("mm-retire", "sm-retire");
  mmCC = new mmLink("mm-cc", "sm-cc");
  mmGiving = new mmLink("mm-giving", "sm-giving");
  mmResources = new mmLink("mm-resources", "sm-resources");
  mmContact = new mmLink("mm2-contact", "sm2-contact");
  
  mmArray[0] = mmPartners;
  mmArray[1] = mmVision;
  mmArray[2] = mmRetire;
  mmArray[3] = mmCC;
  mmArray[4] = mmGiving;
  mmArray[5] = mmResources;
  mmArray[6] = mmContact;
  
  for (i = 0; i < mmArray.length; i++) {
    mmArray[i].initialize();
  }

  /*** FORM INITIALIZATION ***/
  $(".infield-label").inFieldLabels();
  $(".infield-br").remove();
  
  /*** COLORBOX INITIALIZATION ***/
  $(".colorbox").colorbox();
  $(".investment-rates").colorbox({href:"/files/includes/rates/rates_IN.cfm", height:"460px",width:"720px"});
  $(".inversion-rates").colorbox({href:"/files/includes/rates/rates_IN_es.cfm", height:"460px",width:"720px"});
  $(".lr-rates").colorbox({href:"/files/includes/rates/rates_LR.cfm"});
  $(".sitemap").colorbox({href:"/support/sitemap_include.html"});
 
  /*** SEARCH INITIALIZATION ***/
  $("#kw_entered").focus( function () {
     $("#search-container").addClass("active");
	 if ($(this).val() == "Search...") {
	   $(this).val("");
	 } else if ($(this).val() == "Busca...") {
	   $(this).val("");
	 }
  });
  
  $("#kw_entered").blur( function () {
     $("#search-container").removeClass("active");
	 if ($(this).val() == "" && window.lang == "en") {
	   $(this).val("Search...");
	 } else if ($(this).val() == "" && window.lang == "es") {
	   $(this).val("Busca...");
	 }
  });
  
  //Search submit JS is adjacent to the actual HTML elements.
  
  /*** NEXTUP INITIALIZATION ***/
  try {
  $("#nextup-box").hide();

  $("#body-col").waypoint(function(event, direction) {
    if(direction === "down") {
	  $("#nextup-box").show("slide", { direction: "right" }, 500);
    } else {
	  $("#nextup-box").hide("slide", { direction: "right" }, 500);
	}
  }, {offset: 'bottom-in-view'});
  
  $(".nextup").click(function(){		// makes nextup div clickable
     window.location=$(this).find("a").attr("href");
     return false;
  });
  
  $(".nextup-sub").click(function(){		// makes nextup div clickable
     window.location=$(this).find("a").attr("href");
     return false;
  });
  
  $(".nextup").css("cursor", "pointer");
  $(".nextup-sub").css("cursor", "pointer");
  } catch(err) { }
  
});
