Event.observe(window, 'load', function() {
    
    $$('a.morelink').each (function (moreLink) { //make all "morelinks" clickable, this also applies if the items to dispaly is set to 0 in p2p
        moreLink.observe ('click', openModal);
        
        function openModal(event) { //function to get the innerHTML of the table cell div, this has all the events in it
            var element = event.element();
            var listForWndow = element.up('td').innerHTML; //get the HTML in the same table cell as the more link
            var dayNum = element.up('td').nextSiblings();//get the number of cells to the right of that cell, this is used to deteremine the day of the week
            var scrollOffset = element.cumulativeOffset(); //get the total offset the moment the user clicks
            var viewPortOffset = element.viewportOffset(); //get the viewport offset the moment the user clicks
            var scrollOffsetTop = scrollOffset.top;
            var HRday; //initialize the Human readble day variable
            switch(dayNum.length)//set the human readable date by the number of cells to the right of the element clicked
                {
                case 0:
                  HRday = "Saturday";
                break;
                case null:
                  HRday = "Saturday";
                break;
                case 1:
                  HRday = "Friday";
                break;
                case 2:
                  HRday = "Thursday";
                break;
                case 3:
                  HRday = "Wednesday";
                break;
                case 4:
                  HRday = "Tuesday";
                break;
                case 5:
                  HRday = "Monday";
                break;
                case 6:
                  HRday = "Sunday";
                break;
                default:
                  HRday = " ";
                }
            
            var tableCell = element.up('td').identify();
            var tableCellSplit = tableCell.split("-");
            
            switch(tableCellSplit[1])//set the human readable month name
                {
                case "01":
                  HRmonth = "January";
                break;
                case "02":
                  HRmonth = "February";
                break;
                case "03":
                  HRmonth = "March";
                break;
                case "04":
                  HRmonth = "April";
                break;
                case "05":
                  HRmonth = "May";
                break;
                case "06":
                  HRmonth = "June";
                break;
                case "07":
                  HRmonth = "July";
                break;
                case "08":
                  HRmonth = "August";
                break;
                case "09":
                  HRmonth = "September";
                break;
                case "10":
                  HRmonth = "October";
                break;
                case "11":
                  HRmonth = "November";
                break;
                case "12":
                  HRmonth = "December";
                break;
                default:
                  HRmonth = " ";
                }
            
            var dayNumber = HRday+" "+HRmonth+" "+tableCellSplit[2]+", "+tableCellSplit[3]; //set up the human readable full day "September 16, 2009"
                
            
            constructLightBox(listForWndow,scrollOffsetTop,dayNumber,tableCell); //call the constructor function
        }
        
        function constructLightBox(listHTML,scrollOffsetTop,dayNumber,tableCell){ //function to construct the popup and overlay
            
            var viewport = document.viewport.getDimensions(); //get the viewport dimensions (visible screen)
            var containerId = null; "container";
            if ($('container')) {
                containerId = "container";
            } else if ($('page')) {
                containerId = "page";
            } else {
               return;
            }
            var containerH = $(containerId).getDimensions(); //get the container div dimensions
            var viewportOffset = document.viewport.getScrollOffsets(); //see how far the user has scrolled
            var width = viewport.width;//set the width variable, initially to the width of the viewport
            
            if(viewport.width < containerH.width){ //check to see if the user's resolution is so low that there is a horiontal scroll bar
                width = containerH.width; //if it is, use the width of the container div, so the overlay covers everything
            }
            
            var height = containerH.height; //set the height variable
            
            var roundedTop =    '<b class="roundedCorners">'+
                                '<b class="roundedCorners1"><b></b></b>'+
                                '<b class="roundedCorners2"><b></b></b>'+
                                '<b class="roundedCorners3"></b>'+
                                '<b class="roundedCorners4"></b>'+
                                '<b class="roundedCorners5"></b></b>';//construct the top rounded corners http://www.spiffycorners.com/index.php
            var roundedBottom = '<b class="roundedCorners">'+
                                '<b class="roundedCorners5"></b>'+
                                '<b class="roundedCorners4"></b>'+
                                '<b class="roundedCorners3"></b>'+
                                '<b class="roundedCorners2"><b></b></b>'+
                                '<b class="roundedCorners1"><b></b></b></b>';//construct the bottom rounded corners http://www.spiffycorners.com/index.php
            
            var closeButton = '<a href="javascript:closePopUp();" title="close" class="closeFace"><img title="close" src="/hive/images/components/closeButton.png" border="0" width="40" height="40" /></a>';//construct the close button
            var currPageURL = window.location;//get the window location, ad tracking
            var tableCellSpl = tableCell.split("-"); //split the tablecell variable, this is to allow targeting of the ad in the popup based on date
            
            var adHTML="";
            var adHTML2="";
            if(calPopUpSposored == "true"){
              adHTML = '<iframe id="refreshable_ad" class="ad"  src="http://ad.doubleclick.net/adi/trb.'+siteID+sectionID+'/'+tableCellSpl[1]+'-'+tableCellSpl[2]+'-'+tableCellSpl[3]+';rs=10009;rs=10011;rs=10017;rs=10018;rs=10024;rs=10026;rs=10030;rs=10031;rs=10033;rs=10035;rs=10038;rs=10042;rs=10043;rs=10046;rs=10051;rs=10055;rs=10062;rs=10074;rs=10079;rs=10086;rs=10092;rs=10094;rs=10103;rs=10104;rs=10115;ptype=pg;slug='+slugID+';rg=ur;ref=healthkeycom;pos=14;dcopt=ist;sz=300x250;tile=1;u='+currPageURL+';ord=71073343" width="300" height="250" vspace=0 hspace=0 scrolling="no" frameborder="0" marginheight="0" bordercolor="#000000" marginwidth="0"></iframe>'; //construct the ad HTML
              adHTML2 = '<div class="daySponsor"><iframe id="refreshable_ad_spon" class="ad"  src="http://ad.doubleclick.net/adi/trb.'+siteID+sectionID+'/day-sponsorship/'+tableCellSpl[1]+'-'+tableCellSpl[2]+'-'+tableCellSpl[3]+';rs=10009;rs=10011;rs=10017;rs=10018;rs=10024;rs=10026;rs=10030;rs=10031;rs=10033;rs=10035;rs=10038;rs=10042;rs=10043;rs=10046;rs=10051;rs=10055;rs=10062;rs=10074;rs=10079;rs=10086;rs=10092;rs=10094;rs=10103;rs=10104;rs=10115;ptype=pg;slug='+slugID+';rg=ur;ref=healthkeycom;pos=15;dcopt=ist;sz=234x60;tile=1;u='+currPageURL+';ord=71073343" width="234" height="60" vspace=0 hspace=0 scrolling="no" frameborder="0" marginheight="0" bordercolor="#000000" marginwidth="0"></iframe></div>'; //construct the ad HTML
            }
            var faceboxInnerHTML = '<div id="newPop" class="faceBoxHolder" style="display:none;">'+
                                    roundedTop+
                                    '<div class="faceMiddle roundedCornersfg">'+
                                        '<div class="facePadder">'+
                                            '<div class="popTop">'+closeButton+'<div style="float:left">'+dayNumber+'</div>'+adHTML2+'<div class="clearfix"></div></div>'+
                                            '<div class="popCenterHold clearfix">'+
                                                '<div class="popRight">'+adHTML+'</div>'+
                                                listHTML+
                                            '<div class="clearfix" style="clear:both;"></div></div>'+
                                        '</div>'+
                                    '<div class="clearfix"></div></div>'+
                                    roundedBottom+
                                   '</div>'; //construct the popup

            
            var overLay = '<div id="fadedBGOverlay" class="opacityBG" style="width:'+width+'px;height:'+height+'px;display:none;"></div>'; //construct the ovrlay
            
            Element.insert( $(containerId), {'after':overLay} );//write the overlay
            Element.insert( $(containerId), {'after':faceboxInnerHTML} );//write the popup
            var widthPos = parseInt( width / 2 ) - parseInt( ($('newPop').getWidth() / 2) )+'px';//figure out where to position the popup horizontally
            var realOffset = viewportOffset.top + parseInt((viewport.height/2)) - parseInt( ($('newPop').getHeight() / 2) )+'px' ;//figure out where to position the popup vertically
            
            $('newPop').setStyle({ left: widthPos, top: realOffset});//position the popup
            
            if(calPopUpSposored == "true"){
              var adFrame = document.getElementById('refreshable_ad');//get the new ad
              adFrame.src = adFrame.src;//apply the new ad
              var adFrame2 = document.getElementById('refreshable_ad_spon');//get the new ad
              adFrame2.src = adFrame2.src;//apply the new ad
            }

            
            $('fadedBGOverlay').appear({ duration: 0.3, from: 0, to: 0.7 }); //fade in the background overlay
            $('newPop').appear({ duration: 0.7, from: 0, to: 1.0 });   //fade in the pop-up
            if ($('headerAdCode1')) { $('headerAdCode1').hide(); }
            if ($('headerAdCode2')) { $('headerAdCode2').hide(); }
            if ($('headerAdCode3')) { $('headerAdCode3').hide(); }
            if ($('headerAdCode4')) { $('headerAdCode4').hide(); }
        }
    
    });
});
 
 function closePopUp(){
    $('fadedBGOverlay').remove();
    $('newPop').remove();
    if ($('headerAdCode1')) { $('headerAdCode1').show(); }
    if ($('headerAdCode2')) { $('headerAdCode2').show(); }
    if ($('headerAdCode3')) { $('headerAdCode3').show(); }
    if ($('headerAdCode4')) { $('headerAdCode4').show(); }
 }
