var GSA = {};
jQuery(document).ready(function($)
{
    //  NDA Form
    $('#nda_wrap').each(function(){
       var first = $('#nda_first_name');
       var last = $('#nda_last_name');
       var first_target = $('#nda_first_name_target');
       var last_target = $('#nda_last_name_target');
              
      var updateFields = function() {
          first_target.html(first.val());
          last_target.html(last.val());   
      }
              
       $('input').keyup(function(){
           updateFields();
       });
    });
    
    
    //  Home page mini moneyshots
   var home_tabs = jQuery('#property_spinner').tabs({ fx: { opacity: 'toggle' } });
   home_tabs.tabs('rotate', 3000, false);
   
   jQuery('.accordion dd').hide();
   jQuery('.accordion dt').click(function()
   {
      jQuery(this).next().slideToggle(); 
   });

   // make the navigation tabs on the property spinner link to the property rather than spin the spinner
   jQuery('#property_spinner ul li a').click(function()
   {
       var linkUrl = jQuery(this).find('.url').html()
       window.location = linkUrl;
   });

   // make the navigation tabs on the property spinner link to the property rather than spin the spinner
   jQuery('.property_listing .property').click(function()
   {
       var linkUrl = jQuery(this).find('a').attr('href')
       window.location = linkUrl;
   });
   
   jQuery("#mce-EMAIL, .clicky").each(function() {
	   var fieldName = jQuery(this).attr("name");
	   GSA[fieldName] = new GSA.formEraserHelper(jQuery(this));
   });
   
   jQuery(".rack_price_tables #tampa_table").each(function() {
	   jQuery(this).show();
	   jQuery(".nav_rack_prices li:first").attr('class', 'active');
   });
   
   jQuery(".nav_rack_prices a").click(function() {
	   parent = jQuery(this).parent();
	   
	   jQuery(".rack_price_tables div").each(function() {
		   jQuery(this).hide();
	   });
	   
	   parent.attr('class', 'active');
	   parent.siblings().each(function() {
		   jQuery(this).removeAttr('class');
	   });
	   
   	   jQuery(jQuery(this).attr('href') + "_table").show();
   });
   
   GSA.enableInfoFormSubmit = true;
   
   var options = { 
       target:        '#form_feedback',   // target element(s) to be updated with server response 
       beforeSubmit:  showRequest,  // pre-submit callback 
       success:       showResponse,  // post-submit callback 

       // other available options: 
       url:       '/index.php/properties/more-info'      // override for form's 'action' attribute 
       //type:      type        // 'get' or 'post', override for form's 'method' attribute 
       //dataType:  null        // 'xml', 'script', or 'json' (expected server response type) 
       //clearForm: true        // clear all form fields after successful submit 
       //resetForm: true        // reset the form after successful submit 

       // jQuery.ajax options can be used here too, for example: 
       //timeout:   3000 
   };
   
   
   // pre-submit callback 
   function showRequest(formData, jqForm, options) { 
       // formData is an array; here we use jQuery.param to convert it to a string to display it 
       // but the form plugin does this for you automatically when it submits the data 
       var queryString = jQuery.param(formData); 

       // alert('About to submit: \n\n' + queryString); 

       // here we could return false to prevent the form from being submitted; 
       // returning anything other than false will allow the form submit to continue 

       if(GSA.enableInfoFormSubmit == true ){
           GSA.enableInfoFormSubmit = false;
           return true; 
       } 
       else {
           return false;
       }
   } 

   // post-submit callback 
   function showResponse(responseText, statusText, xhr, jQueryform)  { 
   
       // if the form was sumbitted succesfully, disable it.
       if (responseText.search('success') == -1) {
           GSA.enableInfoFormSubmit = true;
       } else{
           GSA.enableInfoFormSubmit = false;
       };
        
       // fire off page view when requesting more info for analytic purposes
       pageTracker._trackPageview("/index.php/more-info-inquiry"); 
   
       // for normal html responses, the first argument to the success callback 
       // is the XMLHttpRequest object's responseText property 

       // if the ajaxSubmit method was passed an Options Object with the dataType 
       // property set to 'xml' then the first argument to the success callback 
       // is the XMLHttpRequest object's responseXML property 

       // if the ajaxSubmit method was passed an Options Object with the dataType 
       // property set to 'json' then the first argument to the success callback 
       // is the json data object returned by the server 

        // alert('status: ' + statusText + '\n\nresponseText: \n' + responseText + 
        //     '\n\nThe output div should have already been updated with the responseText.'); 
            
   }
    
    jQuery('.property_page .details #freeform').ajaxForm(options)
   
   // fire off page view when subscribing to newsletter for analytic purposes
   jQuery('#mc-embedded-subscribe-form').submit(function(){
       pageTracker._trackPageview("/index.php/newsletter-success"); 
   });
   
});

GSA.formEraserHelper = function (fieldObj)
{
	var obj = this;
	this.fieldObj = fieldObj;
	this.initialValue = this.fieldObj.val();
	
	this.fieldObj.focus(function() {
		if (jQuery(this).val() == obj.initialValue) {
			jQuery(this).val("");
		};
	});
	this.fieldObj.blur(function() {
		if ("" == jQuery(this).val()) {
			jQuery(this).val(obj.initialValue);
		};
	}); 
}
