/* Function:    fancy_form_field
 * Description: Hides the labels and adds a listener to clear field value 
 *              onfocus and returns the values onblur if the field is empty.
 * Parameters:  field_id, value
 * Returns:     null
 */
function fancy_form_field (field_id,default_value) {
  $(".hidden").hide();
  $(field_id).val(default_value);
  $(field_id).css('color','#999');
  $(field_id).focus(function(){
    if($(this).val() == default_value) {
      $(this).val('');
      $(this).css('color','#303030')
    }
  }).blur(function(){
    if($(this).val() == '') {
      $(this).val(default_value);
      $(this).css('color','#999')
    }
  })
}

/* Function:    ajax_form
 * Description: binds a FreeForm Form to ajax submit and does all 
 *              work to update the user of an error or to submit.
 * Parameters:  form_id, response_id
 * Returns:     null
 */
function ajax_form (form_id, response_id) {
  $(form_id).ajaxForm ({              //bind ajax submit to the form
    target:       response_id,       //response from submission
    // beforeSubmit: quick_validate, 
    success:      function(response){     //callback function for success  
      if(response == "success"){
        // $(form_id).slideUp();
        $(response_id).html("Your information and message was sent.  We'll be in touch soon.").addClass('success').fadeIn();
        $(form_id).resetForm()      //clear the form
      }else{
        $(response_id).html($(response_id+' #content ul').html()).addClass('error').fadeIn();
      }
    }
  });
}

/* Function:    ajax_form
 * Description: binds a FreeForm Form to ajax submit and does all 
 *              work to update the user of an error or to submit.
 * Parameters:  form_id
 * Returns:     null
 */
function ajax_comment (form_id) {
  $(form_id).ajaxForm ({              //bind ajax submit to the form
    target:       ".comment_response",       //response from submission
    // beforeSubmit: quick_validate, 
    success:      function(response) {     //callback function for success  
      if(response == "success"){
        // $(form_id).slideUp();
        $('.form_response').html("Your comment was sent and.  We'll be in touch soon.").addClass('success').fadeIn();
        $(form_id).resetForm()      //clear the form
      }else{
        $('.comment_response').html($('.comment_response #content ul').html()).addClass('error').fadeIn();
      }
    }
  });
}

/* Function:    contact_form_ini
 * Description: do some visual tweeking of the contact form 
 *              and make the from submit via XHR 
 * Parameters:  none
 * Returns:     null
 */
function contact_form_ini() {
  // make the form fields act all fancy
  fancy_form_field('#name', 'Name');
  fancy_form_field('#email', 'Email');
  fancy_form_field('#company', 'Company');
  fancy_form_field('#phone', 'Phone');
  fancy_form_field('#message', "Message"); 
  fancy_form_field('#contact_captcha', "Verify Your Humanity"); 

  // ajaxify the contact form
  ajax_form("#contact_us", ".form_response");
}

/* Function:    sidebar_forms_ini
 * Description: do some visual tweeking of the form 
 *              and make the from submit via XHR 
 * Parameters:  none
 * Returns:     null
 */
function sidebar_forms_ini() {
  // make the form fields act all fancy
  fancy_form_field('#newsletter_signup', 'Your Email');
  fancy_form_field('#keywords', 'Search');

  // ajaxify the newsletter form
  ajax_form("#singup", ".newsletter_response");

}

/* Function:    comment_form_ini
 * Description: do some visual tweeking of the form 
 *              and make the from submit via XHR 
 * Parameters:  none
 * Returns:     null
 */
function comment_form_ini() {
  // make the form fields act all fancy
  fancy_form_field('#comment_name', 'Name');
  fancy_form_field('#url', 'Website (optional)');
  fancy_form_field('#comment_email', 'Email (will be kept private)');
  fancy_form_field('#comment', 'Comment');
  fancy_form_field('#comment_captcha', 'Verify Your Humanity');

  // ajaxify the comment form
  // ajax_comment("#comment_form");

}


/* Function:    flash_builder
 * Description: build the flash and insert it into the  
 *              page to get around IE being dumb and annoying  
 * Parameters:  w,h,bc,id,swf
 * Returns:     null
 */
function flash_builder(w,h,bc,id,swf) {
  var new_html =  '<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" '
                + '  codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0" width="'+ w +'" height="'+ h +'">'
                + '  <param name="allowScriptAccess" value="sameDomain" />'
                + '  <param name="quality" value="high" />'
                + '  <param name="allowFullScreen" value="false" />'
                + '  <param name="scale" value="noscale" />'
                + '  <param name="wmode" value="transparent" />'
                + '  <param name="bgcolor" value="'+ bc +'" />'
                + '  <param name="movie" value="http://beta.tocquigny.com/public/flash/'+ swf +'" />'
                + '  <embed src="http://beta.tocquigny.com/public/flash/'+ swf +'"quality="high" bgcolor="'+ bc +'" width="'+ w +'" height="'+ h +'"'
                + 'allowScriptAccess="sameDomain" allowFullScreen="false" type="application/x-shockwave-flash" '
                + 'pluginspage="http://www.macromedia.com/go/getflashplayer" wmode="transparent">'
                + '</embed></object>';

  $(id).html(new_html);		
}

// Adds onclick events for Google page tracking to all of the dom id's specified in [n][0] of trackableElements
function googlePageTracker(){
	// trackableElements -> 2D array of form ['tracking_id', 'path/for/google']
	var trackableElements = [
		['#btn_contact', '/header/contact-us/'], 
		['#spell-tocquigny', '/footer/how-do-you-spell-tocquigny'], ['#privacy-policy', '/footer/privacy-policy'],
		['a.email-peter-moossy-header', '/header/contact-us/email-peter-moossy']
	];
	for(x in trackableElements){
		$(trackableElements[x][0]).click(function(){
			pageTracker._trackPageview(trackableElements[x][1]);
		});
	}
}

//on page load functions
$(document).ready(function() {

  /* build the flash baner
  *****************************************/
  //flash_builder("100%","57","#FFFFFF","#banner","banner.swf")

  /* tweek the forms
  *****************************************/
  contact_form_ini();
  sidebar_forms_ini();
  comment_form_ini();
  
  /* contact us panel
  *****************************************/
  $("#btn_contact").click(function(){
    //see if the panel is open
    if ($(".email_form").hasClass("shown")) {
      $(".email_form").slideUp(600,"easeOutExpo").removeClass("shown");
      setTimeout(function() {
          $("#btn_contact").css("background-position","left -67px");
          $("#uber_links li a").css("z-index","1005");
      }, 250);
    }else{
      $(".email_form").slideDown("slow","easeInExpo").addClass("shown");
      $("#uber_links li a").css("z-index","500");
      $(this).css("background-position","left -25px");
    };
  });
  //close button
  $("#btn_contact_close").click(function(){
    if ($(".email_form").hasClass("shown")) {
      $(".email_form").slideUp(600,"easeOutExpo").removeClass("shown");
      setTimeout(function() {
          $("#btn_contact").css("background-position","left -67px");
          $("#uber_links li a").css("z-index","1005");
      }, 250);
    }
    return false;
  });
  
  /* sidebar lists
  *****************************************/
  $('.category_listing li:even').css('clear','left')  

  /* pretty scrolls
  *****************************************/
  $.localScroll({
    duration: 1200,
  	hash: true,
  	easing: "easeOutExpo"
  });
  
  /* modals
  *****************************************/
  $('a.modal_text').fancybox({
    'padding': 20,
    'overlayShow': true,
    'autoDimensions': false,
    'autoScale': false,
    'width': 800,
    'height': 550,
    'overlayOpacity': 0.75,
    'overlayColor': '#fff',
    'hideOnOverlayClick': true,
    'hideOnContentClick': false,
    'showNavArrows': false,
    'speedIn': 500,
    'titleShow': false,
    'type': 'iframe'
  });
});