//global js only
$(document).ready(function(){
  
  $.default_search_text = 'Search for a business to give feedback to...';
  $(".g-search #q").val($.default_search_text);
  $(".g-search #q").removeClass('active');
  $("#aid").val("");
  $("#region").val("");

  $("#global-search-submit").click(
    function() {
      if ($(".g-search #q").val() == $.default_search_text || $("#q").val().trim() == "") {
        $("#q").effect("highlight", {}, 3000);
        return false;
      }
    }
  );
   
  $(".g-search #q").focus( function() {
      if ($(".g-search #q").val() == $.default_search_text || $(".g-search #q").val().length == 0) {
        $(this).val('');
      }
  });
  
  $(".g-search #q").blur( function() {
      if ($(this).val().length == 0) {
        $(this).val($.default_search_text);
      }
      $(this).removeClass('active');
  });

  $(".formel:not(#give-feedback .formel)").live('focus', 
    function() {
      $(this).addClass('active');
      $(this).parent("div").find("span.error").html('');
  });
  
  $(".formel:not(#give-feedback .formel)").live('blur',
    function() {
      $(this).removeClass('active');      
  });
  
  //set field focus on form pages
  if ($("#page-signin").length) { $("#user_session_email").focus(); }
   
  jQuery.fn.exists = function(){
    return jQuery(this).length > 0;
  };
      
  jQuery.ajaxSetup({ 
    'beforeSend': function(xhr) { xhr.setRequestHeader("Accept", "text/javascript"); }
  });
  
  jQuery.fn.submitWithAjax = function(callback) {
    this.submit(function() {
      $.post(this.action, $(this).serialize(), callback, "script");
      return false;
    });
    return this;
  };
  
  //member top nav drop down
  $("a#g-account-link").click(function() {
    $("#g-account-menu").toggle();
    $("#g-manage-menu").hide();
    return false;
  });
  
  $("a#g-manage-link").click(function() {
    if ($("#g-manage-menu").length) {
      $("#g-manage-menu").toggle();
      $("#g-account-menu").hide();
      return false;
    }
  });
  
  //hiding account and manage feedback dropdowns if they click anywhere else
  $("body").click(function() {
    $("#g-account-menu, #g-manage-menu").hide();
  });
  
  //for IE we need the li clickable to extend the href inside...this grabs that href and assigns it
  jQuery.each($("li.menu-item"), function() {
    $(this).click(function() {
      location.href = $(this).children("a").attr('href');  
    });
  });

  $('.facebook_login_connect').bind('click', function() {
    FB.login(
			function(){
				$.post('/facebook_user_session.js', {}, null, 'script');
			}, 
			{perms:'publish_stream,email'}
		);
		return false;
  });
  
});

// Track an action as an event and as a pageview
function trackEvent(category, action, label) {
  var url_path_elements = [parameterize(category), parameterize(action)];
  if (label != undefined) {
    url_path_elements.push(parameterize(label));
  }
  var url = '/' + url_path_elements.join('/');

  _gaq.push(
    ['_trackEvent', category, action, label],
    ['_trackPageview', url]
  );
  return true;
}

// This logic is pretty much borrowed from the rails implementation
function parameterize(string){
  parameterized_string = string.replace(/[^a-z0-9\-_\+]+/gi, '-').replace(/-{2,}/, '-').replace(/^-|-$/gi, '');
  return parameterized_string.toLowerCase();
}