/*
 * This prevents console.log, etc. from causing JS erros in non-Firebugged browsers
 */
if (!("console" in window) || !("firebug" in console))
{
  var names = ["log", "debug", "info", "warn", "error", "assert", "dir", "dirxml", "group", "groupEnd", "time", "timeEnd", "count", "trace", "profile", "profileEnd"];

  window.console = {};
  for (var i = 0; i < names.length; ++i)
    window.console[names[i]] = function() {}
}


/* Firebug stuff */
console.alert = function(str) {
  if (!window.names) {
    // Firebug is available!
    //console.log(str);
  } else {
    alert(str);
  }
}



var xMousePos = 0; // Horizontal position of the mouse on the screen
var yMousePos = 0; // Vertical position of the mouse on the screen

var curr_motd;
var curr_motd_num = 1;
var max_motd_num;

var check_for_motd_close_interval;

// Element.prototype.mouse_within_auto = function() {
//   return Position.within(this, window.xMousePos, window.yMousePos);
// }
//
// Element.prototype.mouse_within = function(event) {
//   mouseX = Event.pointerX(event);
//   mouseY = Event.pointerY(event);
//   return Position.within(this, mouseX, mouseY);
// }

Element.Methods.mouse_within = function(element, event) {
  mouseX = Event.pointerX(event);
  mouseY = Event.pointerY(event);
  return Position.within(element, mouseX, mouseY);
}

Element.Methods.mouse_within_auto = function(element) {
  return Position.within(element, window.xMousePos, window.yMousePos);
}

// mouse_within_auto = function() {
//   return Position.within(this, window.xMousePos, window.yMousePos);
// }

Element.addMethods();

mouse_within = function (event, element) {
  mouseX = Event.pointerX(event);
  mouseY = Event.pointerY(event);
  return Position.within(element, mouseX, mouseY);
}

record_mouse_position = function (event) {
  xMousePos = event.pointerX();
  yMousePos = event.pointerY();
  // console.log(window.xMousePos);
  // console.log(window.yMousePos);
}

report_mouse_position = function(event) {
  //console.log("Mouse at: " + Event.pointerX(event) + ', ' + Event.pointerY(event));
}

open_current_motd_body = function () {

  check_for_motd_close_interval = setInterval('doMouseCheck()', 1000);

  curr_motd.down('.motd_body_overlay_wrapper').overlay.show();
  // console.log(curr_motd.down('.motd_body_overlay_wrapper').overlay);
  motd_open = true;
}

close_current_motd_body = function (close_title) {
  close_title = close_title ? close_title : true
  //document.stopObserving('mousemove', record_mouse_position);
  clearInterval(check_for_motd_close_interval);

  curr_motd.down('.motd_body_overlay_wrapper').overlay.hide();
  motd_open = false;
}

change_to_previous_motd = function() {
  //console.log("Changing? " + curr_motd_num + " : " + max_motd_num);
  if (curr_motd_num >= max_motd_num) {
    return;
  }

  close_current_motd_body();
  curr_motd.hide();
  curr_motd.down('.feed_text').hide();

  curr_motd_num++;
  curr_motd = $('motd_' + curr_motd_num);

  open_current_motd_body();
  curr_motd.show();
  curr_motd.down('.feed_text').show();

  // Hack
  curr_motd.down('.motd_body_overlay_wrapper').overlay.cfg.setProperty("context", ['motd_nav_feed', "tl", "bl"]);
}

change_to_next_motd = function() {
  if (curr_motd_num <= 1) {
    return;
  }
  close_current_motd_body();
  curr_motd.hide();
  curr_motd.down('.feed_text').hide();

  curr_motd_num--;
  curr_motd = $('motd_' + curr_motd_num);

  open_current_motd_body();
  curr_motd.show();
  curr_motd.down('.feed_text').show();
}

function open_dna_panel_start() {
  new Ajax.Updater('profileBuilder', '/rails/dna_panel/show_panel', {
    asynchronous: true,
    evalScripts: true,
    onComplete: function(request) {
      $('profileBuilder').show(); $('profileBuilderLoading').hide();
    }
  });
}

toggle_login = function () {
  if ($('login_panel').visible()) {
    close_login();
  } else {
    open_login();
  }
}

open_login = function () {
  window.scroll(0, 0);
  $('login_panel').show();
  $('navbar_username').focus();
}

close_login = function () {
  $('login_panel').hide();
}

show_password_recovery = function() {
  $('login').hide(); $('password_recovery').show();
}

close_login = function () {
  $('login_panel').hide();
}

open_registration = function () {
  signup_panel.show();
  $('signup_panel').show();
  $('new_username').focus();
}

close_registration = function () {
  signup_panel.hide();
  $('signup_panel').hide();
}

switch_to_login = function() {
  signup_panel.hide();
  $('signup_panel').hide();
  login_panel.show();
  $('login_panel').show();
  $('navbar_username').focus();
}

switch_to_registration = function() {
  login_panel.hide();
  $('login_panel').hide();
  signup_panel.show();
  $('signup_panel').show();
  $('new_username').focus();
}

// From http://www.universalwebservices.net/web-programming-resources/javascript/change-input-element-type-using-javascript
// e.g. <input type='text' value='type password here' onfocus="if (this.value == 'type password here') { changeInputType(this, 'password'); }; "/>
function changeInputType(oldObject, oType) {
  var newObject = document.createElement('input');
  newObject.type = oType;
  if(oldObject.size) newObject.size = oldObject.size;
  if(oldObject.value) newObject.value = oldObject.value;
  if(oldObject.name) newObject.name = oldObject.name;
  if(oldObject.id) newObject.id = oldObject.id;
  if(oldObject.className) newObject.className = oldObject.className;
  oldObject.parentNode.replaceChild(newObject,oldObject);
  return newObject;
}


function move(object_to_move, based_on, offset_left, offset_top) {
  //challenge_box = $('forums_main_item');
  left = based_on.cumulativeOffset()['left'] + offset_left;
  top = based_on.cumulativeOffset()['top'] + offset_top;
  //console.log(object_to_move);
  //console.log(based_on);
  //console.log('Location: ' + left + ', ' + top);

  object_to_move.setStyle({ top: top + 'px', left: left + 'px'});
}


// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults
function textCounter(field, countfield, maxlimit)
{
	if (field.value.length > maxlimit)
	{
		field.value = field.value.substring(0, maxlimit);
	}
	else
	{
		e = document.getElementById(countfield);
		val = e.innerHTML;
		e.innerHTML = (maxlimit - field.value.length);
	}
}


function closeDNAPanel(){
	e = document.getElementById('profileBuilder');
	e.hide();
}


function disableForm(formname){
	elements = Form.getElements(formname);
	//This doesnt work for some reason
	//Form.disable(formname);

	 elements.each(function (anElement){
		//alert(anElement.type);
		var newClass = "";

		if (Element.hasClassName(anElement, 'game_pulldown')){
			newClass = "game_pulldown_off";
		}

		if (Element.hasClassName(anElement, 'game_textarea')){
			newClass = "game_textarea_off";
		}

		if (Element.hasClassName(anElement, 'button')){
			newClass = "button_off";
		}

		if (Element.hasClassName(anElement, 'avatar_pulldown')){
			newClass = "avatar_pulldown_off";
		}

		if (anElement.type == 'submit'){
			//anElement.hide();
			anElement.disable();
		}

		Element.addClassName(anElement, newClass);

	});

}

function enableElements(elements) {
  elements.each(function (anElement){
    // console.log("Element ID: " + anElement.id + ", Type: " + anElement.type);
		var newClass = "";
		var oldClass = "";
		if (Element.hasClassName(anElement, 'game_pulldown_off')){
			newClass = "game_pulldown";
			oldClass = "game_pulldown_off";
		}

		if (Element.hasClassName(anElement, 'game_textarea_off')){
			newClass = "game_textarea";
			oldClass = "game_textarea_off";
		}

		if (anElement.id == 'button_off') {
		  anElement.id = 'button';
		}

		if (Element.hasClassName(anElement, 'button_off')){
			newClass = "button";
			oldClass = "button_off";
		}

		if (Element.hasClassName(anElement, 'avatar_pulldown_off')){
			newClass = "avatar_pulldown";
			oldClass = "avatar_pulldown_off";
		}

		anElement.enable();

		Element.addClassName(anElement, newClass);
		Element.toggleClassName(anElement, oldClass);

	});
}

function enableElementsInForm(form) {
  elements = Form.getElements(form);
  enableElements(elements);
  $A(theUserImageForm.elements).each(function (item) {
    if (item.disabled) {
      item.disabled = false;
    }
  });
}

function enableForm(formname){
	elements = Form.getElements(formname);
	enableElements(elements);
}

function disable_form_elements(form) {
  form = $(form);
  elements = form.getElements();
  elements.each(function (element) {
    element.disable();
    element.addClassName('disabled');
  });
}

function enable_form_elements(form) {
  form = $(form);
  elements = form.getElements();
  elements.each(function (element) {
    element.enable();
    element.removeClassName('disabled');
  });
}






/* this method will munge the values of several form elements of a gamedata_item and munge them into one form element.
it will then remove the other elements, so that they are never submitted.
*/
mangleIndividualItem = function(element){
	//alert(element);
	proposed_name = element.down('.item_proposed_name');
	game_id = element.down('.item_gameid');
	the_id = element.down('.item_id');
	should_destroy = element.down('.item_should_destroy');
	mangledData = element.down('.item_mangled_data');

	/*alert("proposedName " + proposedName.value + "  " +
	"game_id " + game_id.value + "  " +
	"the_id " + the_id.value + "  " +
	"should_destroy " + should_destroy.value + "  " +
	"mangledData " + mangledData.value + "  " );*/

	//if the string was empty, just remove the element altogether.
	if (proposed_name.value == null || proposed_name.value == ''){
		mangledData.remove();
	}
	else{
		mangledString = proposed_name.value + '_' + game_id.value + '_' + the_id.value + '_' + should_destroy.value;
		mangledData.value = mangledString;
	}

	//alert(mangledData.value);
	//alert(mangledData.value.split('_'));
	proposed_name.remove();
	game_id.remove();
	the_id.remove();
	should_destroy.remove();
}

/** this method is a means to work around the rails problem of complex forms and ajax not allowing more than one field.
	it will munge the contents of the form elements for each gamedata_item, and make them into one form element.
	Rails can then handle the unpacking of this information.
*/
mangleItemData = function(formname){

	element = $(formname);

	//get the first one.
	element = element.down('.gamedata_item_fields');
	mangleIndividualItem(element);
	element.hide();

	//now proceed along with them.
	while (element = element.next('.gamedata_item_fields')){
		mangleIndividualItem(element);
		element.hide();
	}
	//snag the add item div, adn hide it so it looks less weird
	$('gamedata_items_add').hide();
	$('gamedata_items').hide();
	$('gamedata_items_saving').show();

}


markForDestroy = function(element){
	$(element).next('.item_should_destroy').value = 1;
	$(element).up('.gamedata_item_fields').hide();
}

function getFlashVarsString(url_base, path){
	var flashvarsString = '';
	// This is a user string if you want ot load just one user.
  flashvarsString += "xmlpath=" + url_base + path;
	// flashvarsString += "xmlpath=/rails/user_images/public_gallery";
	flashvarsString += "&";
	flashvarsString += "prePath=" + url_base;
	flashvarsString += "&";
	flashvarsString += "debugMode=false";
	//console.log("Params for filmstrip: " + flashvarsString)
	return flashvarsString
}

display_filmstrip = function(element_id, url_base, feed_path) {
  var fo = new FlashObject(url_base + '/flash/galleryWide_1.swf', "main", "373", "135", "8", "#FFFFCC");
  fo.addParam("allowScriptAccess", "sameDomain");
  fo.addParam("wmode", "transparent");

  // Comment the next line for testing, to force it to load from the live site.
  fo.addParam("flashVars", getFlashVarsString(url_base, feed_path));

  fo.write(element_id);
}

tickDownActionItemsCounter = function() {
  action_items_count--;
  $('action_items_count').update('' + action_items_count)
}

tickDownBoardCounter  = function() {
  overall_board_count--;
  $('overall_board_count').update('' + overall_board_count)
}

function getAllChildren(e) {
  // Returns all children of element. Workaround required for IE5/Windows. Ugh.
  return e.all ? e.all : e.getElementsByTagName('*');
}

function uncheckAll(set) {
  set.each(function(el) { el.checked = false; })
}

function checkAll(set) {
  set.each(function(el) { el.checked = true; })
}

function init_notice(notice_element) {
  notice_element = $(notice_element);

  notice_panel = new YAHOO.widget.Panel(notice_element.id, {
  	constraintoviewport: true,
  	underlay: 'shadow',
  	y: 0,
  	zIndex: 10001,
  	close: true,
  	visible: false,
    iframe: true,
  	draggable: false,
  	fixedcenter: true,
  	modal: true
  	});
  notice_element.panel = notice_panel;
  notice_panel.cfg.setProperty('underlay', 'matte');
  notice_panel.render();
}

// Notices system
function show_notice(notice_element) {
  notice_element = $(notice_element)
  notice_panel = notice_element.panel;

  notice_element.show();
  notice_panel.show();
}



/* Form Helpers */

function setMaxLength() {
	var x = document.getElementsByTagName('textarea');
	var counter = document.createElement('div');
	counter.className = 'counter';
	for (var i=0;i<x.length;i++) {
		if (x[i].getAttribute('maxlength')) {
			var counterClone = counter.cloneNode(true);
			counterClone.relatedElement = x[i];
      // counterClone.innerHTML = '<span>0</span>/'+x[i].getAttribute('maxlength');
			counterClone.innerHTML = '<span>0</span> out of '+x[i].getAttribute('maxlength') + ' characters';
			x[i].parentNode.insertBefore(counterClone,x[i].nextSibling);
			x[i].relatedElement = counterClone.getElementsByTagName('span')[0];

			x[i].onkeyup = x[i].onchange = checkMaxLength;
			x[i].onkeyup();
		}
	}
}

function checkMaxLength() {
	var maxLength = this.getAttribute('maxlength');
	var currentLength = this.value.length;
	if (currentLength > maxLength)
		this.relatedElement.className = 'toomuch';
	else
		this.relatedElement.className = '';
	this.relatedElement.firstChild.nodeValue = currentLength;
	// not innerHTML
}


function change_selection(select_element, new_selection) {
  select_element.options[select_element.selectedIndex].selected = false;
  new_selection.selected = true;
}

function change_selection_by_value(select_element, new_value_to_select) {
  option = select_element.down('option[value=' + new_value_to_select + ']')
  change_selection(select_element, option);
}


/*
 * Infinite Scroll
 */
// endless_page.js
var currentPage = 1;

var inner_element;
var outer_element;

var inner_height;
var margin_for_load = 300;

var stop_scroll = false;

function checkScroll() {

  if (!$('dna_panel_subnav_loading')) {
    // If there's no loading element, then there are no more items to load!
    return;
  }

  inner_element = $('fullwidth');
  outer_element = $('pb_subnav_scroll');
  inner_height = outer_element.getHeight();

  if (nearBottomOfPage()) {
    currentPage++;
    new Ajax.Request('/rails/dna_panel/just_experiences?page=' + currentPage, { asynchronous: true, evalScripts: true, method: 'get' });
  } else {
    if (!stop_scroll) {
      setTimeout("checkScroll()", 250);
    }
  }
}

// scrollWidth
// scrollHeight

function nearBottomOfPage() {
  //console.log("Checking scroll: " + scrollDistanceFromBottom());
  return scrollDistanceFromBottom() < inner_height + margin_for_load;
}

function scrollDistanceFromBottom(argument) {
  return inner_element.getHeight() - outer_element.scrollTop;
  // return pageHeight() - (window.pageYOffset + self.innerHeight);
}

function pageHeight() {
  return Math.max(document.body.scrollHeight, document.body.offsetHeight);
}

// document.observe('dom:loaded', checkScroll);


/*
 * Brower Detection
 */
var BrowserDetect = {
  init: function () {
    this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
    this.version = this.searchVersion(navigator.userAgent)
      || this.searchVersion(navigator.appVersion)
      || "an unknown version";
    this.OS = this.searchString(this.dataOS) || "an unknown OS";
  },
  searchString: function (data) {
    for (var i=0;i<data.length;i++)  {
      var dataString = data[i].string;
      var dataProp = data[i].prop;
      this.versionSearchString = data[i].versionSearch || data[i].identity;
      if (dataString) {
        if (dataString.indexOf(data[i].subString) != -1)
          return data[i].identity;
      }
      else if (dataProp)
        return data[i].identity;
    }
  },
  searchVersion: function (dataString) {
    var index = dataString.indexOf(this.versionSearchString);
    if (index == -1) return;
    return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
  },
  dataBrowser: [
    {   string: navigator.userAgent,
      subString: "OmniWeb",
      versionSearch: "OmniWeb/",
      identity: "OmniWeb"
    },
    {
      string: navigator.vendor,
      subString: "Apple",
      identity: "Safari"
    },
    {
      prop: window.opera,
      identity: "Opera"
    },
    {
      string: navigator.vendor,
      subString: "iCab",
      identity: "iCab"
    },
    {
      string: navigator.vendor,
      subString: "KDE",
      identity: "Konqueror"
    },
    {
      string: navigator.userAgent,
      subString: "Firefox",
      identity: "Firefox"
    },
    {
      string: navigator.vendor,
      subString: "Camino",
      identity: "Camino"
    },
    {    // for newer Netscapes (6+)
      string: navigator.userAgent,
      subString: "Netscape",
      identity: "Netscape"
    },
    {
      string: navigator.userAgent,
      subString: "MSIE",
      identity: "Explorer",
      versionSearch: "MSIE"
    },
    {
      string: navigator.userAgent,
      subString: "Gecko",
      identity: "Mozilla",
      versionSearch: "rv"
    },
    {     // for older Netscapes (4-)
      string: navigator.userAgent,
      subString: "Mozilla",
      identity: "Netscape",
      versionSearch: "Mozilla"
    }
  ],
  dataOS : [
    {
      string: navigator.platform,
      subString: "Win",
      identity: "Windows"
    },
    {
      string: navigator.platform,
      subString: "Mac",
      identity: "Mac"
    },
    {
      string: navigator.platform,
      subString: "Linux",
      identity: "Linux"
    }
  ]

};

BrowserDetect.init();


/*
 * Image Caching
 */
function preload_background_image(element) {
  if (element) {

    background_image_prop = element.getStyle('backgroundImage')
    if (!background_image_prop.include('url(')) {
      //console.log('Skipping preload on this:');
      //console.log(element);
      return false;
    }
    img_src = background_image_prop.gsub(/url\([']?["]?([^'"]*)[']?["]?\)/, '#{1}');
    //console.log("Preloading: '" + img_src + "'");
    img = new Image();
    img.src = img_src;
  }
}


/*
 * Paging
 */
var page_active = new Array();
var page_perpage = new Array();
var page_section_items = new Array();

function init_paging(section,def,section_items,per_page)
{
	page_active[section] = def;
	page_perpage[section] = per_page;
	page_section_items[section] = section_items;
}

function show_page_less(section)
{
	page_active[section] = 1;
	pages = Math.ceil(page_section_items[section]/page_perpage[section]);
	$(section + '_page_1').show();
	for(i=2; i<=pages; i++)
	{
		$(section + '_page_' + i).hide();
	}

  // Put back the bottom-border-related stuff
	for(i = 1; i <= (pages - 1); i++)
	{
		$(section + '_page_' + i).select('.was_last_in_list').each(function (item) {
		  item.removeClassName('was_last_in_list');
		  item.addClassName('last_in_list');
		});
	}

	$('page_view_all_' + section).innerHTML = '<a href="javascript:show_page_all(\'' + section + '\')">VIEW ALL</a>'
	$('next_' + section).innerHTML = '<a href="javascript:show_page(\'' + section + '\',page_active[\'' + section + '\']+1);">&nbsp;NEXT&nbsp;' + page_perpage[section] + '&nbsp;</a>';

	$('next_' + section).removeClassName('comp_nav_next_na');
	$('next_' + section).addClassName('comp_nav_next_a');

	$('page_view_all_' + section).removeClassName('comp_nav_all_col');
	$('page_view_all_' + section).addClassName('comp_nav_all_exp');

}

function show_page_all(section)
{

	pages = Math.ceil(page_section_items[section]/page_perpage[section]);
  // console.log('For section: ' + section + ' with ' + pages + ' pages...')
	for(i=1; i<=pages; i++)
	{
		$(section + '_page_' + i).show();
	}

  // Remove the bottom-border-related stuff
	for(i = 1; i <= (pages - 1); i++)
	{
		$(section + '_page_' + i).select('.last_in_list').each(function (item) {
		  item.removeClassName('last_in_list');
		  item.addClassName('was_last_in_list');
		});
	}

	page_active[section] = 1;

	$('page_range_' + section).innerHTML = 1 + '&nbsp;-&nbsp;' + page_section_items[section] + '&nbsp;/<span class="status">&nbsp;' + page_section_items[section] + "</span>";

	$('prev_' + section).innerHTML = '&nbsp;PREV&nbsp;' + page_perpage[section];
	$('next_' + section).innerHTML = '&nbsp;NEXT&nbsp;' + page_perpage[section] + '&nbsp;';

	$('next_' + section).removeClassName('comp_nav_next_a');
	$('next_' + section).addClassName('comp_nav_next_na');

	$('page_view_all_' + section).innerHTML = '<a href="javascript:show_page_less(\'' + section + '\')">VIEW LESS</a>'

	$('page_view_all_' + section).removeClassName('comp_nav_all_exp');
	$('page_view_all_' + section).addClassName('comp_nav_all_col');

}

function show_page(section, pagenum)
{
	pages = Math.ceil(page_section_items[section]/page_perpage[section]);

	if(page_active[section]!=pagenum)
	{
		$(section + '_page_' + page_active[section]).hide();
	}
	$(section + '_page_' + pagenum).show();

	page_active[section] = pagenum;

	maxnum = ((page_active[section])*page_perpage[section]);
	if(maxnum > page_section_items[section])
	{
		maxnum = page_section_items[section];
	}

	$('page_range_' + section).innerHTML = (((page_active[section]-1)*page_perpage[section])+1) + '&nbsp;-&nbsp;' + maxnum + '&nbsp;/<span class="status">&nbsp;' + page_section_items[section] + '</span>';

  // console.log("pagenum: " + pagenum + ", pages: " + pages + ", section: " + section + ", perpage: " + page_perpage[section]);
	if(pagenum==1)
	{
    // console.log("(back to) beginning");
		$('prev_' + section).innerHTML = '&nbsp;PREV&nbsp;' + page_perpage[section];
		$('prev_' + section).removeClassName('comp_nav_prev_a');
		$('prev_' + section).addClassName('comp_nav_prev_na');
	}
	else
	{
		$('prev_' + section).innerHTML = '<a href="javascript:show_page(\'' + section + '\',page_active[\'' + section + '\']-1);">&nbsp;PREV&nbsp;' + page_perpage[section] + '</a>';
		$('prev_' + section).removeClassName('comp_nav_prev_na');
		$('prev_' + section).addClassName('comp_nav_prev_a');
	}

	if(pagenum>=pages)
	{
		$('next_' + section).innerHTML = '&nbsp;NEXT&nbsp;' + page_perpage[section] + '&nbsp;';
		$('next_' + section).removeClassName('comp_nav_next_a');
		$('next_' + section).addClassName('comp_nav_next_na');
	}
	else
	{
	  // Dup algorithm code with Rails' app/helpers/application_helpler.rb's page_preloaded_pages() code (as of 1/3/2008)
	  next_num = (pagenum == (pages - 1)) ? (page_section_items[section] - (pagenum*page_perpage[section])) : page_perpage[section]
		$('next_' + section).innerHTML = '<a href="javascript:show_page(\'' + section + '\',page_active[\'' + section + '\']+1);">&nbsp;NEXT&nbsp;' + next_num + '&nbsp;</a>';
		$('next_' + section).removeClassName('comp_nav_next_na');
		$('next_' + section).addClassName('comp_nav_next_a');
	}

}


/*
 * Profile page stuff
 */
function show_respect_received()
{
	$('respect_a_received').innerHTML = 'Received';
	$('respect_a_given').innerHTML = '<a href="javascript:show_respect_given()">Given</a>';

	$('respect_given').hide();
	$('respect_received').show();
}

function show_respect_given()
{
	$('respect_a_received').innerHTML = '<a href="javascript:show_respect_received()">Received</a>';
	$('respect_a_given').innerHTML = 'Given';

	$('respect_given').show();
	$('respect_received').hide();
}

title = "#{@member.username}'s <span id='experiences_a_stories'>Stories</span> | <span id='experiences_a_game_feed'><a href='javascript:show_epxeriences_game_feed()'>Game Feed</a></span> | <span id='experiences_a_all'><a href='javascript:show_experiences_all()'>All Experiences</a></span>"

function show_epxeriences_stories()
{
	$('experiences_a_stories').innerHTML = "Stories";
	$('experiences_a_game_feed').innerHTML = "<a href='javascript:show_epxeriences_game_feed()'>Game Feed</a>";
	$('experiences_a_all').innerHTML = "<a href='javascript:show_experiences_all()'>All Experiences</a>";

	$('experiences_stories').show();
	$('experiences_game_feed').hide();
	$('experiences_all').hide();
}

function show_epxeriences_game_feed()
{
	$('experiences_a_stories').innerHTML = "<a href='javascript:show_epxeriences_stories()'>Stories</a>";
	$('experiences_a_game_feed').innerHTML = "Game Feed";
	$('experiences_a_all').innerHTML = "<a href='javascript:show_experiences_all()'>All Experiences</a>";

	$('experiences_stories').hide();
	$('experiences_game_feed').show();
	$('experiences_all').hide();
}

function show_experiences_all()
{
	$('experiences_a_stories').innerHTML = "<a href='javascript:show_epxeriences_stories()'>Stories</a>";
	$('experiences_a_game_feed').innerHTML = "<a href='javascript:show_epxeriences_game_feed()'>Game Feed</a>";
	$('experiences_a_all').innerHTML = "All Experiences";

	$('experiences_stories').hide();
	$('experiences_game_feed').hide();
	$('experiences_all').show();
}


send_ajax_request = function(url, url_on_complete) {
  //make ajax call to that url
  new Ajax.Request(url,{
  method: 'get',
  asynchronous: true,
  onSuccess: function(transport) {
    if (url_on_complete != '#') {
      window.location = url_on_complete;
    }
  }
  });
}

update_max_text_div = function(inputid, outputid, max) {
  input = document.getElementById(inputid);
  output = document.getElementById(outputid);
  var returnvalue;
  returnvalue = true;
  if (input.value != null) {
    if (input.value.length > max)
      input.value = input.value.substring(0, max);
    if (input.value.length ==max)
      returnvalue = false;
    output.innerHTML = input.value.length + " of " + max + " characters used";
  }
  else {
    returnvalue = true;
  }
  return returnvalue;
}

set_max_text_div = function(inputid, outputid, max) {
  Event.observe(inputid, 'keydown', function() {update_max_text_div(inputid, outputid, max);});
  Event.observe(inputid, 'keyup', function() {update_max_text_div(inputid, outputid, max);});
  Event.observe(inputid, 'change', function() {update_max_text_div(inputid, outputid, max);});
  Event.observe(inputid, 'focus', function() {update_max_text_div(inputid, outputid, max);});
  update_max_text_div(inputid, outputid, max);
}
