if (!Array.prototype.indexOf)
{
  Array.prototype.indexOf = function (elt /*, from*/)
  {
    var len = this.length;

    var from = Number(arguments[1]) || 0;
    from = (from < 0)
         ? Math.ceil(from)
         : Math.floor(from);
    if (from < 0) {
      from += len;
    }
    for (; from < len; from++)
    {
      if (from in this &&
          this[from] === elt) {
        return from;
      }
    }
    return -1;
  };
}

function createCookie(name, value, days, path) {
/*
 days defaults to Session Expiring ("").
 path defaults to /.
*/
  var expires, pathname;
  if (days) {
    var date = new Date();
    date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
    expires = "; expires=" + date.toGMTString();
  } else {
    expires = "";
  }
  if (path) {
    pathname = path;
  } else {
    pathname = "/";
  }
  document.cookie = name + '="' + value + '"' + expires + "; path=" + pathname;
}

function deleteCookie(name, path) {
/*
 days defaults to Session Expiring ("").
 path defaults to /.
*/
  var pathname;
  if (path) {
    pathname = path;
  } else {
    pathname = "/";
  }
  document.cookie = name + '="deleted"; expires=Fri, 1 Jan 1999 01:00:00 UTC; path=' + pathname;
}

function readCookie(name, defaultValue) {
  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) {
      c = c.substring(nameEQ.length, c.length);
      if (c.indexOf('"') === 0 && c.lastIndexOf('"') === c.length - 1) {
        return c.substring(1, c.length - 1);
      } else {
        return c;
      }
    }
  }
  if (defaultValue) {
    return defaultValue;
  } else {
    return undefined;
  }
}

function changeCommunity(region_select) {
  var oldRegion, newRegion, newURL;
  var region_options = region_select.options;

  for (var i = 0, iLen = region_options.length; i < iLen; i++) {
    if (region_options[i].selected) {
      /* IE doesn't implement the disabled attribute on options,
         so we need to check it here */
      if (region_options[i].disabled) {
        return false;
      }
      newRegion = region_options[i].value;
      break;
    }
  }

  oldRegion = document.location.href.match('posts/Region/.*');

  if (oldRegion) {
    // was in the URL, parse to get the value
    oldRegion = oldRegion[0].substr(13);
    var last_slash = oldRegion.indexOf('/');
    if (last_slash !== -1) {
      oldRegion = oldRegion.substr(0, last_slash);
    }
    if (newRegion !== 'show_all_regions') {
      newURL = document.location.href.replace(oldRegion, escape(newRegion));
    } else {
      newURL = document.location.href.replace('/Region/' + oldRegion, '');
    }
    oldRegion = unescape(oldRegion);
  } else {
    // not in url, use the cookie
    oldRegion = readCookie('region_id_cookie');
    newURL = document.location.href;
    // If we are viewing all of the posts in a category, we need to redirect to inside the region
    if (newRegion !== 'show_all_regions' && newURL.search('/posts/') !== -1) {
      newURL = newURL.replace('/posts/', ['/posts/Region/', escape(newRegion), '/'].join(''));
    }
  }

  if (newRegion === 'show_all_regions') {
    deleteCookie('region_id_cookie');
  }

  if (oldRegion !== newRegion) {
    if (newRegion !== 'show_all_regions') {
      createCookie("region_id_cookie", newRegion, 3650, '/');
    }
    if (document.location.href !== newURL) {
      document.location.href = newURL;
    } else {
      document.location.reload();
    }
  }
}

function changeCategory(category_select) {
  var newCategory, newURL;
  var category_options = category_select.options;

  for (var i = 0, iLen = category_options.length; i < iLen; i++) {
    if (category_options[i].selected) {
      /* IE doesn't implement the disabled attribute on options,
         so we need to check it here */
      if (category_options[i].disabled) {
        return false;
      }
      newCategory = category_options[i].value;
      break;
    }
  }

  if (newCategory !== 'show_all_categories') {
    newURL = [document.location.protocol, '//', document.location.host, '/posts/', escape(newCategory)].join('');
  } else {
    newURL = [document.location.protocol, '//', document.location.host, '/'].join('');
  }
  if (document.location.href !== newURL) {
    document.location.href = newURL;
  }
}

function fixLinkWrapping() {
  var tagsToCheck = ['A', 'SPAN'],
    allTags, h, hlen, i, ilen, thisTag,
    linkTags = [],
    current_font_size;
  for (h = 0, hlen = tagsToCheck.length; h < hlen; h++) {
    allTags = document.getElementsByTagName(tagsToCheck[h]);
    for (i = 0, ilen = allTags.length; i < ilen; i++) {
      thisTag = allTags[i];
      if (thisTag.className.split(' ').indexOf('shrink_to_unwrap') !== -1) {
        linkTags.push(thisTag);
      }
    }
  }
  for (i = 0, ilen = linkTags.length; i < ilen; i++) {
    thisTag = linkTags[i];
    current_font_size = 13;
    while (thisTag.clientHeight >= current_font_size * 2 && current_font_size > 0) {
      current_font_size -= 1;
      thisTag.style.setProperty('font-size',
                                current_font_size.toString() + 'px',
                                'important');
    }
  }
}
