// Landing page object 
var landing = {};


landing.sendReset = function() { 
   var email = YAHOO.util.Dom.get('inpEmail').value; 
   var sURL = '/Newsroom/Events/rest.php?action=resetPassword&email=' + email; 
   
   YAHOO.util.Connect.asyncRequest('GET', sURL, {success: function (o) { 
                                                      YAHOO.log(o.responseText); 
                                                      landing.diaReset.hide();
                                                      landing.infoBox.show(); 
                                                      },  
                                                 failure: {
                                                   } }); 
} 

landing.cancelReset = function() { 
   this.hide(); 
}; 

landing.closeInfo = function () { 
   this.hide();  
} 


var init = function () {
   // Toggle Logging
   //var myLogReader = new YAHOO.widget.LogReader('log');

   landing.showEvent = new YAHOO.lb.eventPanel('showEvent');
   landing.showEvent.render('eventPanel'); 
   
   var infoEls = YAHOO.util.Dom.getElementsByClassName('oh_info', 'a'); 
   
   YAHOO.util.Event.addListener(infoEls, 'click', function() {
         var reEvent = /^\w*_(\d{1,6})$/i; 
         var match = reEvent.exec(this.id);
         
         landing.showEvent.loadEvent(match[1]); 
         //landing.showEvent.show();
         //YAHOO.log('show');
      }); 
   
   pageTracker._trackPageview('/Landings/Goal/PageLoaded');  
   /** Get all of our "tabs" by looking up elements by classname **/
   var tabs = YAHOO.util.Dom.getElementsByClassName('tabLink');
   
   var panels = YAHOO.util.Dom.getElementsByClassName('tab_panel'); 
   landing.panels = []; 
   
   panelConfig = {width: "400px", 
                 height: "232px",
                 visible: false, 
                 draggable: false, 
                 close: true,
                 scrollable: true, 
                 constraintoviewport: false,
                 underlay: 'none',
                 effect: {effect:YAHOO.widget.ContainerEffect.FADE,duration: 0.20}}; 
   
   for (var i = 0; i < panels.length; i++) { 
      var panelName = panels[i].id.substr(6); 
      landing.panels[panelName] = new YAHOO.widget.Panel(panels[i].id, panelConfig); 
      landing.panels[panelName].render();
   } 
  
   /** Keep track of the panel that is currently being shown so we can hide it when another 
   tab is clicked **/ 
   landing.activePanel = false; 
   
   landing.loc = {}; 
   
   /** Listener for tab clicks **/ 
   YAHOO.util.Event.addListener(tabs, 'click', function () { 
      var tabName = this.id.substr(4);
      landing.panels[tabName].show();
      if (landing.activePanel && landing.activePanel !== landing.panels[tabName]) {
         landing.activePanel.hide(); 
         landing.activePanel = false;
      } 
      landing.activePanel = landing.panels[tabName]; 
      pageTracker._trackPageview('/Landings/tabs/' + tabName);
      
   });
  
   /**
    * Overwrite the place holder function that is called when an 
    * list is created. 
    */
   YAHOO.lb.forms.listCreated = function (ref) { 
      YAHOO.util.Event.addListener(ref, 'change', function (e) { 
         scv = YAHOO.util.Dom.get('SourceCodeVal'); 
         scv.value = this.value; 
      });    
   } 
   
   /**
    * Overwrite the placeholder function that is called when an "other" 
    * text box is created. 
    */
   YAHOO.lb.forms.otherCreated = function (ref) { 
      // if the value is set (posted) update the other box
      ref.value = YAHOO.util.Dom.get('SourceCodeVal').value; 
   
      // add event handler to update the hidden field when the 
      // value of this is changed. 
      YAHOO.util.Event.addListener(ref, 'change', function (e) { 
        scv = YAHOO.util.Dom.get('SourceCodeVal'); 
        scv.value = this.value;  
      }); 
   } 
   
   /**
    * When the Source Type is changed we need to load the secondary list based 
    * on the selected value of this list 
    */
   YAHOO.util.Event.addListener('SourceType', 'change', function (e) { 
      var sourceType = this[this.selectedIndex].value; 
      var json_url = '/shared/rest/inforeq.php?action=secondarySourceCodes&SourceType=' + sourceType; 
      YAHOO.lb.forms.listSelect(this, json_url, 'sourceCode', 'IntCodeID', 'Label'); 
      YAHOO.util.Dom.get('SourceCodeVal').value = ''; 
   }); 
   
   
   /** 
    * If the SourceType is already set when the page is loaded--because of refresh or post--
    * we are going to load the secondary list 
    */ 
   var sourceTypeVal = YAHOO.util.Dom.get('SourceType') || false;  
   if (sourceTypeVal && sourceTypeVal.selectedIndex > 0) { 
      // get the value from the hidden field 
      var svid = sourceTypeVal.value; 
      var json_url = '/shared/rest/inforeq.php?action=secondarySourceCodes&SourceType=' + svid; 
      dflt = YAHOO.util.Dom.get('SourceCodeVal').value || false;
      YAHOO.lb.forms.listSelect(sourceTypeVal, json_url, 'sourceCode', 'IntCodeID', 'Label', dflt);
   } 
   
   // Tru Ambitions and most interested buttons 
   landing.taButtons = new YAHOO.widget.ButtonGroup("taButtons"); 
   landing.progButtons = new YAHOO.widget.ButtonGroup("progButtons");
   
   // Create our password reset dialog 
   landing.diaReset = new YAHOO.widget.SimpleDialog('resetPass', 
                                    { width: "300px", 
                                      fixedcenter: true, 
                                      visible: false, 
                                      draggable: false, 
                                      close: false,
                                      modal: true,
                                      icon: YAHOO.widget.SimpleDialog.ICON_WARN,
                                      text: "Click \"Reset Password\" to have an email sent to you with instructions on reseting your password.", 
                                      contraintoviewport: true,
                                      effect: {effect:YAHOO.widget.ContainerEffect.FADE,duration: 0.20}, 
                                      buttons: [
                                             { text: "Reset Password", handler: landing.sendReset, isDefault: true}, 
                                             { text: "Cancel", handler: landing.cancelReset}
                                          ]});
   
   landing.diaReset.render('reset'); 
   
   landing.infoBox = new YAHOO.widget.SimpleDialog('info', 
                                                {width: "300px", 
                                                 fixedcenter: true, 
                                                 constraintoviewport:true, 
                                                 underlay:"shadow",
                                                 close: false, 
                                                 visible: false,
                                                 effect: {effect:YAHOO.widget.ContainerEffect.FADE,duration: 0.20},
                                                 modal: true,  
                                                 draggable:false, 
                                                 text: 'An email has been sent to you which contains instructions for reseting your password. Once you have reset your password you can register below.',
                                                 buttons: [{text: "Okay", handler: landing.closeInfo, isDefault: true}]}); 
   landing.infoBox.render('infoBox');                                                
                                                 

   YAHOO.util.Event.onAvailable('resetPassword', function() {
      YAHOO.util.Event.addListener('resetPassword', 'click', landing.diaReset.show, landing.diaReset, true);  
   }); 
}

YAHOO.util.Event.onAvailable('taText', function() { 
   var taTextDefault = 'Please share with us your "True Ambition"'; 
   this.value = taTextDefault;
   YAHOO.util.Event.addListener('taText', 'focus', function () { 
       if (this.value == taTextDefault) 
         this.value = '';      
   }, taTextDefault);    
   
   YAHOO.util.Event.addListener('taText', 'blur', function() { 
      if (this.value == '') 
         this.value = taTextDefault;
      }, taTextDefault);
});  


// Initialize
YAHOO.util.Event.onDOMReady(init); 

   /**
   If their is a btnOHRegister element on the page this event will fire. 
   */
YAHOO.util.Event.onAvailable('btnOHRegister', function() { 
            //@todo need to make sure that pressing enter will fire this event and not submit the form 
   
            // And we are going to listen for someone to submit the form
            YAHOO.util.Event.addListener('btnOHRegister', 'click', landing.btnOHRegister); 
         }); 
        
// See if we need to lookup zip codes  
YAHOO.util.Event.onAvailable('ZipCode', function () { 
   if (this.value.length == 5) 
      landing.doZipLookup(this.value);  
   
   //Look up zipcode
   YAHOO.util.Event.addListener('ZipCode', 'keyup', function (e) { 
         landing.doZipLookup(this.value); 
         
         YAHOO.util.Event.addListener('ZipCode', 'keyup', function (e) {YAHOO.log('hi');}); 
         
      });
   }); 
         
/**
   Handler for when a button clicked to register for an open house...
**/
landing.cbOHRegister = { 
   /**
    * Fired when the request is sent. We are just going to give the use some indication that their request is being processed.
    */ 
   start: function (o) { 
      var OHBody = YAHOO.util.Dom.get('OHBody'); 
      OHBody.innerHTML = 'Registering for event....'; 
                
   }, 
   /**
    * On Success we are going to let the user know that their request was received. 
    */ 
   success: function(o) { 
               try { 
                  var resp = YAHOO.lang.JSON.parse(o.responseText);     
               } 
               catch (e) {
                  YAHOO.log('Invalid Response');   
               }
               
               if (resp.bSuccess) { 
                  var OHBody = YAHOO.util.Dom.get('OHBody');     
                  OHBody.innerHTML = resp.message;
                  pageTracker._trackPageview('/Landings/Goal/EventRegistered'); 
               }
               else { 
                 var msg = YAHOO.util.Dom.get('respMessage');
                  msg.innerHTML = '<p>' + resp.message + '</p>';
               } 
               
            }, 
   /**
    * If there is an error, handle accordingly.
    */          
   failure: function (o) { 
               YAHOO.log('failed'); 
            }
} 

landing.setCityState = function (City, State) { 
   var CityState = YAHOO.util.Dom.get('City_State'); 
   CityState.innerHTML = City + ', ' + State; 
   YAHOO.util.Dom.get('inp_City').value = City; 
   YAHOO.util.Dom.get('inp_State').value = State;
} 
  
// call our zipcode lookup function 
landing.doZipLookup = function (Zip) { 
   var reZip = /^[0-9]{5}$/;  // Validate zip code 
   var CityState = YAHOO.util.Dom.get('City_State'); 
   
   if (reZip.test(Zip)) { 
      SI_SERVICE.onReturnLoc = function (City, State) { 
         YAHOO.util.Dom.get('inp_City').value = City; 
         YAHOO.util.Dom.get('inp_State').value = State; 
         CityState.innerHTML = City + ', ' + State; 
      } 
      var loc = SI_SERVICE.zipCodeLookup(Zip); 
   }
   else { 
      CityState.innerHTML = "Enter Valid Zip Code...";   
   }
} 

/**
 * Handle function for when the submit button is pressed
 */
landing.btnOHRegister  = function () { 
   landing.frm = YAHOO.util.Dom.get('frmOHRegister'); 
   // Link Connection object to the form
   YAHOO.util.Connect.setForm(landing.frm);
   YAHOO.util.Connect.asyncRequest('POST', document.location, landing.cbOHRegister);  
} 
