// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults
// updates a field to reflect the number of characters remaining in another field
function _updateCounter(field, counter_name, limit) {

	counter = document.getElementById(counter_name);
	counter.innerHTML = limit - field.value.length;
}

function _truncateAt(field, limit) {
	if(field.value.length > limit)
		field.value = field.value.truncate(limit, '');
}

// these functions are from http://www.quirksmode.org/js/cookies.html
// they're not exactly efficient, but how often are you getting/setting these?
var Cookie = {
  get : function (name) {
    var nameEQ = name + "="
    var ca = document.cookie.split (";")

    for (var i = 0; i < ca.length; ++i) {
      var c = ca[i]
      while (c.charAt (0) == " ") c = c.substring (1, c.length)

      if (c.indexOf (nameEQ) == 0)
      return c.substring (nameEQ.length, c.length)
    }

    return null
  },

  millisecondsInADay : 24 * 60 * 60 * 1000,

  set : function (name, value, days) {
    if (days) {
      var date = new Date ()
      date.setTime (date.getTime () + (days * Cookie.millisecondsInADay))

      var expires = "; expires=" + date.toGMTString ()
    }

    else var expires = ""

    // write the cookie
    document.cookie = name + "=" + value + expires + "; path=/"
  },

  erase : function (name) {
    Cookie.set (name, "", -1)
  }
}

var CustomLeaders = {
	private_val : null,

	custom_leaders : function() {
		if (null == this.private_val) {
			this.private_val = this.get();
		}
		return this.private_val;
	},
	
	clear : function(my_val) {
		this.private_val = my_val;
	},
	
	get : function() {
		var my_val = Cookie.get('custom_leaders');
		if (null != my_val) { return my_val.split(','); }
		else { return []; }
	},
	
	add : function(slot_id, complete_callback) {
		if(this.custom_leaders().include(slot_id)) { return; }
		var my_val = [this.custom_leaders(), slot_id].flatten();
		Cookie.set('custom_leaders', my_val.join(','), 365);
		this.clear(my_val);
		if (null != complete_callback) { complete_callback(slot_id); }
	},
	
	remove : function(slot_id, complete_callback) {
		var my_val = this.custom_leaders().select(function(item) { return (slot_id != item); });
		Cookie.set('custom_leaders', my_val.join(','), 365);
		this.clear(my_val);
		if (null != complete_callback) { complete_callback(slot_id); }
	},
	
	plus : function(slot_id, hide) {
		$('+' + slot_id).toggle();
	},
	
	minus : function(slot_id, hide){
		$('x' + slot_id).toggle();
	},
	
	swap_icons : function(slot_id) {
		CustomLeaders.minus(slot_id);
		CustomLeaders.plus(slot_id);
	},
	
	swap : function(slot_id) {
		if (this.custom_leaders().include(slot_id)) {
			this.remove(slot_id, this.swap_icons);
		} else {
			this.add(slot_id, this.swap_icons);
		}
	},
	
	smoke_em_if_you_got_em : function(array_of_slots) {
		array_of_slots.collect(function(slot_id) {
			if (CustomLeaders.custom_leaders().include(slot_id)) {
				CustomLeaders.plus(slot_id);
				CustomLeaders.minus(slot_id);
			}
		});
	}
}

var BoardSwitcher = {
	swap : function(target){

		var oldTarget = Cookie.get('_konami_tf_score_target');
		if( oldTarget == null || oldTarget == '' ) {
			oldTarget = 'global';
		}
				
		if( target != oldTarget ) {
			this.activate('h3-' + target);
			this.deactivate('h3-' + oldTarget);
			Cookie.set('_konami_tf_score_target', target, 1);
		}
		
		window.location.reload();
	},
	
	team : function() {
		BoardSwitcher.swap('team');
	},
	
	custom : function() {
		BoardSwitcher.swap('custom');
	},
	
	global : function() {
		Cookie.erase('_konami_tf_score_target');
		window.location.reload();
	},
	
	deactivate : function(id) {
		element = $(id);
		if(null == element) return;
		element.removeClassName('active');
		element.addClassName('inactive');
	},
	
	activate : function(id) {
		element = $(id);
		if(null == element) return;
		element.removeClassName('inactive');
		element.addClassName('active');
		element.onclick = '';
	},
		
	active : function() {

		href = Cookie.get('_konami_tf_score_target');
		if( href == null ) {
			href = '';
		}
		
		if (href.match(/(custom|team)/)) {
			this.deactivate('h3-global');
			if (href.match(/team/)) { this.activate('h3-team'); }
			if (href.match(/custom/)) { this.activate('h3-custom'); }
		} else {
			this.activate('h3-global');
		}
	}
}