/* ---------------------------------------------- */
/* Create Registration module namespace           */
/* if it does not exist                           */
/* ---------------------------------------------- */
var CM_Reg; 
if(!CM_Reg) CM_Reg = {};

/* ---------------------------------------------- */
/* UTILITIES
/* 
 * format numbers to include commas               
 */
CM_Reg.formatNumber = function (number) {
  if(number < 1000) return number;
  
  var strNumber = new String(number); 
  var index = strNumber.length;

	index -= 3;
	while (index >= 1) {
		strNumber = strNumber.substring(0,index) + "," + strNumber.substring(index,strNumber.length)
		index -= 3;
	}		
	return strNumber;
}
/*
 * correct transparent png for IE6                
 */
CM_Reg.pngFix = function (element) {
  var image = $(element).css('backgroundImage');
  var start = 4;
  var end   = image.indexOf(')');
  image = image.substring(start,end);
  $(element).css({
	  'background': 'none',
	  'filter': 'progid:DXImageTransform.Microsoft.AlphaImageLoader(src="' + image + '",sizingMethod="scale")'
  });
}

/*
 * bind popup links, overrides default browser behavior for anchors
 *	 to enable set the rel attribute in anchor tags to 'external'
 *	 optional: set width and height 'external_<width>_<height>'
 *	 optional: set title attribute in anchor sets the name of the popup window
 *	 example: <a href="sample.html" rel="external_600_600" title="popupName">popup link</a>
 */
CM_Reg.bindPopUps = function() {
  $("a[rel^='external']").click(function(){
    var href = $(this).attr("href");
    var name = $(this).attr("title");
    if(!name) name ="classmates";
    var relArray = $(this).attr("rel").split("_",3);
    
    // default dimensions
    var width = "500";
    var height= "500";
    
    // override defaults
    if(relArray.length == 3) {
      width = relArray[1];
      height = relArray[2];
    }
    
    var options = "toolbar=1,menubar=0,scrollbars=yes,scrolling=yes,resizable=yes,width=" + width + ",height=" + height;
    window.open(href,name,options);
    return false; // cancels default browser action
  });  
}
/* END UTILITIES 
/* ---------------------------------------------- */
   




/* ---------------------------------------------------------------------------- */
// ToolTip (must be instantiated after page has completely loaded (onLoad))                                  
// Displays a tool tip with state details.       
// param stateList = optional, array of states                                  
// param toolTipElement = optional, reference to tooltip div, #<yourElement>
// param toolTipBackground = optional, url background image, default is blue
// param linkClass =  optional, class name for your state links
CM_Reg.ToolTip = function (stateList,toolTipElement,toolTipBackground,linkClass) {
  if (!stateList) stateList = CM_Reg.stateList;
  CM_Reg.stateList = stateList;
  
  CM_Reg.ToolTip.h3Style = "";
  CM_Reg.ToolTip.ulStyle = "";
  CM_Reg.ToolTip.liStyle = "";
  
  if(!toolTipElement) {
    var tipHTML = "<div id='dhtmltooltip'></div>";
    $("body").append(tipHTML);
    toolTipElement = "#dhtmltooltip";
    // add default styles
    CM_Reg.ToolTip.tipStyle = {
      display: "none",
      position: "absolute",
      top: "-100px",
      left: "-100px",
      width: "161px",
      height: "74px",
      padding: "4px 2px 2px 4px",
      background: "transparent url(/graphics/reg_test/greenReginfo_bubble.png) no-repeat",
      zIndex: 10000
    };
    $("#dhtmltooltip").css(CM_Reg.ToolTip.tipStyle);
    CM_Reg.ToolTip.h3Style = {
      margin: "0",
      padding: "0",
      fontSize: "12px",
      color: "#000",
      textAlign: "left"
    };
    CM_Reg.ToolTip.ulStyle = {
      margin: "3px 0 0 3px",
      padding: "3px 0 0 0",
      listStyle: "none"
    };
    CM_Reg.ToolTip.liStyle = {
      padding: "3px 0 0 0",
      margin: "0",
      fontSize: "11px",
      lineHeight: "100%",
      fontWeight: "bold",
      textAlign: "left"
    };
  } 
  CM_Reg.ToolTip.toolTipElement = $(toolTipElement);
  CM_Reg.ToolTip.toolTipElementString = toolTipElement;
  
  if(!toolTipBackground) toolTipBackground = "/graphics/reg_test/greenReginfo_bubble.png";
  CM_Reg.ToolTip.m_toolTipBackground = toolTipBackground;

  // bind tool tip function to state link hover
  if(!linkClass) linkClass = ".stateLink";
  this.m_linkClass = $(linkClass);
  this.m_linkClass.hover(function(){
      CM_Reg.ToolTip.toggleTip('show',this)
    },function(){
      CM_Reg.ToolTip.toggleTip('hide',this)
    });
  this.m_linkClass.click(function(){
     CM_Reg.ToolTip.toggleTip('hide',this)
  });

  // IE6 hacks
  //var IE = $.browser.msie && /MSIE\s(5\.5|6\.|7\.|8\.)/.test(navigator.userAgent);
  var browser=navigator.appName;
  if (browser=='Microsoft Internet Explorer') {
   CM_Reg.pngFix(toolTipElement);
  }
};


CM_Reg.ToolTip.toolTipEnabled = true; 
CM_Reg.ToolTip.mouseTracking = false;
CM_Reg.ToolTip.toolTipElement = "";
CM_Reg.ToolTip.toolTipElementString = "";
CM_Reg.ToolTip.toolTipBackground = "";
/* ---------------------------------------------- */
/* State List for registration home page          */
/* Array index represents the sId (state id)      */
/* ---------------------------------------------- */
CM_Reg.stateList = new Array(); 
CM_Reg.stateList[15] = {name:'Alabama'             ,schools:11000 ,members:1700000};
CM_Reg.stateList[34] = {name:'Alaska'              ,schools:2300  ,members:270000};
CM_Reg.stateList[54] = {name:'Arizona'             ,schools:8900 ,members:1500000};
CM_Reg.stateList[18] = {name:'Arkansas'            ,schools:5700 ,members:1000000};
CM_Reg.stateList[17] = {name:'California'          ,schools:68000 ,members:12000000};
CM_Reg.stateList[36] = {name:'Colorado'            ,schools:8800 ,members:1400000};
CM_Reg.stateList[16] = {name:'Connecticut'         ,schools:7300 ,members:1400000};
CM_Reg.stateList[38] = {name:'Delaware'            ,schools:2200 ,members:300000};
CM_Reg.stateList[56] = {name:'District of Columbia',schools:2200  ,members:220000};
CM_Reg.stateList[3]  = {name:'Florida'             ,schools:30000 ,members:4600000};
CM_Reg.stateList[44] = {name:'Georgia'             ,schools:17000 ,members:2700000};
CM_Reg.stateList[30] = {name:'Hawaii'              ,schools:2600 ,members:420000};
CM_Reg.stateList[45] = {name:'Idaho'               ,schools:2300 ,members:520000};
CM_Reg.stateList[11] = {name:'Illinois'            ,schools:30000 ,members:5300000};
CM_Reg.stateList[23] = {name:'Indiana'             ,schools:15000 ,members:2600000};
CM_Reg.stateList[10] = {name:'Iowa'                ,schools:7500 ,members:1300000};
CM_Reg.stateList[19] = {name:'Kansas'              ,schools:7300 ,members:1100000};
CM_Reg.stateList[22] = {name:'Kentucky'            ,schools:10000 ,members:1600000};
CM_Reg.stateList[5]  = {name:'Louisiana'           ,schools:11000 ,members:1700000};
CM_Reg.stateList[41] = {name:'Maine'               ,schools:3100 ,members:600000};
CM_Reg.stateList[37] = {name:'Maryland'            ,schools:14000 ,members:2000000};
CM_Reg.stateList[2]  = {name:'Massachusetts'       ,schools:16000 ,members:2600000};
CM_Reg.stateList[25] = {name:'Michigan'            ,schools:23000 ,members:4600000};
CM_Reg.stateList[32] = {name:'Minnesota'           ,schools:9000 ,members:1800000};
CM_Reg.stateList[27] = {name:'Mississippi'         ,schools:6000 ,members:1100000};
CM_Reg.stateList[6]  = {name:'Missouri'            ,schools:13000 ,members:2200000};
CM_Reg.stateList[42] = {name:'Montana'             ,schools:2100 ,members:420000};
CM_Reg.stateList[58] = {name:'Nebraska'            ,schools:4500 ,members:720000};
CM_Reg.stateList[52] = {name:'Nevada'              ,schools:3300  ,members:480000};
CM_Reg.stateList[43] = {name:'New Hampshire'       ,schools:2400 ,members:490000};
CM_Reg.stateList[4]  = {name:'New Jersey'          ,schools:17000 ,members:3900000};
CM_Reg.stateList[21] = {name:'New Mexico'          ,schools:3100 ,members:700000};
CM_Reg.stateList[13] = {name:'New York'            ,schools:72000,members:8000000};
CM_Reg.stateList[33] = {name:'North Carolina'      ,schools:14000 ,members:2500000};
CM_Reg.stateList[39] = {name:'North Dakota'        ,schools:2000  ,members:300000};
CM_Reg.stateList[1]  = {name:'Ohio'                ,schools:22000 ,members:5100000};
CM_Reg.stateList[9]  = {name:'Oklahoma'            ,schools:7600 ,members:1500000};
CM_Reg.stateList[28] = {name:'Oregon'              ,schools:6400 ,members:1300000};
CM_Reg.stateList[8]  = {name:'Pennsylvania'        ,schools:29000 ,members:5100000};
CM_Reg.stateList[24] = {name:'Rhode Island'        ,schools:2900  ,members:440000};
CM_Reg.stateList[7]  = {name:'South Carolina'      ,schools:7800 ,members:1300000};
CM_Reg.stateList[57] = {name:'South Dakota'        ,schools:2200 ,members:330000};
CM_Reg.stateList[29] = {name:'Tennesse'            ,schools:9800 ,members:1900000};
CM_Reg.stateList[12] = {name:'Texas'               ,schools:37000,members:7500000};
CM_Reg.stateList[20] = {name:'Utah'                ,schools:3200 ,members:810000};
CM_Reg.stateList[50] = {name:'Vermont'             ,schools:1300  ,members:240000};
CM_Reg.stateList[35] = {name:'Virginia'            ,schools:15000 ,members:2400000};
CM_Reg.stateList[14] = {name:'Washington'          ,schools:11000 ,members:2200000};
CM_Reg.stateList[31] = {name:'West Virginia'       ,schools:5400 ,members:950000};
CM_Reg.stateList[26] = {name:'Wisconsin'           ,schools:10000 ,members:2000000};
CM_Reg.stateList[59] = {name:'Wyoming'             ,schools:1100  ,members:250000};  
// for 'find friends from' box
CM_Reg.stateList[99] =  {name:'Schools'            ,schools:690000  ,members:110000000};  
CM_Reg.stateList[100] = {name:'Colleges'           ,colleges:88000  ,members:8700000};  
CM_Reg.stateList[101] = {name:'Work'               ,companies:2100000  ,members:4000000};  
CM_Reg.stateList[102] = {name:'Military'           ,units:74000  ,members:2700000};
  
/* ---------------------------------------------- */
// mousemove event handler
// updates position of tool tip 
/* ---------------------------------------------- */
CM_Reg.ToolTip.updateMouse = function (event) {
  var y = event.pageY;
  var x = event.pageX;
  var toolTipTop  = y - CM_Reg.ToolTip.toolTipElement.height() - 10 ;
  var toolTipLeft = x + 5 ; 
  CM_Reg.ToolTip.toolTipElement.css('top' ,toolTipTop);
  CM_Reg.ToolTip.toolTipElement.css('left',toolTipLeft);
}

/* ---------------------------------------------- */
// show/hide state info tool tip                  
/* ---------------------------------------------- */
CM_Reg.ToolTip.toggleTip = function (state,obj) {
  if(!CM_Reg.ToolTip.toolTipEnabled) return;
  if(state == 'show') {
    if(!CM_Reg.ToolTip.mouseTracking) {
      $(document.body).bind('mousemove', CM_Reg.ToolTip.updateMouse);
      CM_Reg.ToolTip.mouseTracking = true;
    }
    
    var stateId = $(obj).attr('id').substring(1);
    var content = '';
    for(var property in CM_Reg.stateList[stateId]) {
      if(property == "name") {
        content += '<h3>'+CM_Reg.stateList[stateId].name+'</h3><ul>';
      } else {
        content += '<li>' + CM_Reg.formatNumber(CM_Reg.stateList[stateId][property]) + ' + ' + property + '</li>';
      }
    }
    content += '</ul>';
    
    CM_Reg.ToolTip.toolTipElement.html(content);
    if (CM_Reg.ToolTip.h3Style != "" && CM_Reg.ToolTip.ulStyle != "" && CM_Reg.ToolTip.liStyle != "") {
      // reapplying base styles, bug where tooltip loses height,width and/or background image alignement
      //$("#dhtmltooltip").css(CM_Reg.ToolTip.tipStyle);
      // apply styles to new content
      $("#dhtmltooltip h3").css(CM_Reg.ToolTip.h3Style);
      $("#dhtmltooltip ul").css(CM_Reg.ToolTip.ulStyle);
      $("#dhtmltooltip li").css(CM_Reg.ToolTip.liStyle);
    }
    CM_Reg.ToolTip.toolTipElement.show();
  } else {
    CM_Reg.ToolTip.mouseTracking = false;
    $(document.body).unbind('mousemove', CM_Reg.ToolTip.updateMouse);
    CM_Reg.ToolTip.toolTipElement.hide();
  }
}
//  END ToolTip Class                                        
/* ---------------------------------------------------------------------------- */

