
function pluralize(count, singular, plural) {
  plural = plural || singular + 's';

  return count == 1 ? singular : plural;
}

location.querystring = (function() {
  var queryStringDictionary = {};

  var querystring = decodeURI(location.search);

  if (!querystring) {
    return {};
  }

  // Remove the '?' via substring(1)
  querystring = querystring.substring(1);

  // '&' seperates key/value pairs
  var pairs = querystring.split("&");

  // Load the key/values of the return collection
  for (var i = 0; i < pairs.length; i++) {
    var keyValuePair = pairs[i].split("=");
    queryStringDictionary[keyValuePair[0]] = keyValuePair[1];
  }

  return queryStringDictionary;
})();

activator_class = function(klass) {
  for (key in klass) {
    if (key.match(/^activate/) && typeof klass[key] == 'function') {
      jQuery(document).ready(klass[key]);
    }
  }
};

var Application = {
  activate_browser_targeting: function(){
    var target_classes = [];
    if(jQuery.browser.msie){
      target_classes.push("msie");
      if(jQuery.browser.version == 6.0){
        target_classes.push("ie6");
      }
      jQuery(document.body).addClass(target_classes.join(' '));
    }
  },
  activate_feedback_tracking: function() {
    jQuery('#uservoice-feedback-tab').click(function() {
       pageTracker._trackEvent('Support', 'Feedback', location.pathname);
    });
  },
  activate_select_all_rates_check_box: function() {
    jQuery('#selectAllCheckBox').click(function() {
      var checked = jQuery(this).attr('checked');
      jQuery("#rate_edit_preview input:checkbox").attr('checked', checked);
    });
  },
  activate_hide_textarea_invite: function() {
    jQuery('.question_box #title').focus(function() {
      var title = jQuery(this);
      if(title.val() == t('questions._ask_a_question_box.invite')) {
        title.val("");
        title.css("color", "black");
        jQuery('.question_box .chars_info').show();
        jQuery('.question_box #title').trigger('keyup');
      }
    });
  },
  activate_show_textarea_invite: function() {
    jQuery('.question_box #title').blur(function() {
      var title = jQuery(this);
      if(jQuery.trim(title.val()) === "") {
        title.css("color", "#8B8B8B");
        title.val(t('questions._ask_a_question_box.invite'));
        jQuery('.question_box .chars_info').hide();
      }
    });
  },
  activate_check_invite_on_submit: function() {
    jQuery('.question_box').submit(function() {
      var title = jQuery(this).find('#title');
      if(title.val() == t('questions._ask_a_question_box.invite')) {
        window.location = this.action; // bypass submit, just go there
        return false;
      }
    });
  },
  activate_question_box_check_chars_left: function() {
    textarea_limiter('.question_box #title', 200, function(count) { jQuery('.question_box .chars_count').text(count) });
  },
  activate_check_chars_left: function() {
    textarea_limiter('#new_question #question_title', 200, function(count) { jQuery('.new_question .chars_count').text(count) });
  },
  activate_activity_feed_bubbles_rollover: function() {
    var bubbles = jQuery('#activity_feed .content a');
    bubbles.mouseover(function() {
      jQuery(this).addClass("hover");
      jQuery(this).parents(".item").find(".bubble_tip").addClass("image_hover");
    });
    bubbles.mouseout(function() {
      jQuery(this).removeClass("hover");
      jQuery(this).parents(".item").find(".bubble_tip").removeClass("image_hover");
    });
   },
  activate_rounded_corners: function() {
    jQuery('.rounded_corners').corner();
  },
  activate_zebra: function() {
    jQuery('.zebra tr:even').addClass('stripe');
  }
};

activator_class(Application);

jQuery.uniq = function(list) {
  var obj = {};
  jQuery.each(list, function(i, item) {
    obj[item] = null;
  });

  var result = [];
  jQuery.each(obj, function(k,v) {
    result.push(k);
  });

  return result;
};

function number_to_currency(num) {
  var reverse = function(str) {
    return str.split("").reverse().join("");
  };

  var add_thousand_separators = function(str) {
    str = reverse(str);
    str = str.match(/.{1,3}/g).join(",");

    return reverse(str);
  }

  return '$' + add_thousand_separators(num.toString());
}

function textarea_limiter(sel, maxsize, callback) {
  jQuery(sel).keyup(function() {
    var text = jQuery(this).val();
    var chars = text.length;

    if(chars > maxsize) {
      jQuery(this).val(text.slice(0, maxsize));
      callback(0)
    } else {
      callback(maxsize - chars)
    }
  });
}

