/*  insertAtCursor was mercilessly stolen whole-hog from http://www.yayhooray.com.  Sorry guys.
 *--------------------------------------------------------------------------*/

function insertAtCursor(myField, myValue) {
	if (document.selection) { // IE
		myField.focus();
		sel = document.selection.createRange();
		sel.text = myValue;
	} else if (myField.selectionStart || myField.selectionStart == 0) {//MOZILLA/NETSCAPE support
    var startPos = myField.selectionStart;
    var endPos = myField.selectionEnd;
    myField.value = myField.value.substring(0, startPos) + myValue + myField.value.substring(endPos, myField.value.length);
	} else {
		myField.value += myValue;
	}
}

// Replaces all instances of the given substring. By http://www.bennadel.com
String.prototype.replaceAll = function(
strTarget, // The substring you want to replace
strSubString // The string you want to replace in.
){
var strText = this;
var intIndexOfMatch = strText.indexOf( strTarget );
 
// Keep looping while an instance of the target string
// still exists in the string.
while (intIndexOfMatch != -1){
// Relace out the current instance.
strText = strText.replace( strTarget, strSubString )
 
// Get the index of any next matching substring.
intIndexOfMatch = strText.indexOf( strTarget );
}
 
// Return the updated string with ALL the target strings
// replaced out with the new substring.
return( strText );
}

function editpost(myId, threadId) {
	postid = "post"+myId;
	postcontent = $(postid).innerHTML;
	postcontent = postcontent.replaceAll( "<br />", "" ); //strip out br tags
	postcontent = postcontent.replaceAll( "<br>", "" ); //strip out br tags
	replacewith = "<form action=\""+threadId+"\" method=\"post\"><input type=\"hidden\" name=\"edit_id\" value=\""+myId+"\" /><textarea name=\"edit\" class=\"editarea\">"+postcontent+"</textarea><br /><input type=\"submit\" name=\"post_edit\" value=\"edit it!\" class=\"post_submit\">";
	$(postid).replace(replacewith);
}

/*  initOverLabels and hideLabel came from alistapart.com
 *--------------------------------------------------------------------------*/

function initOverLabels () {
  if (!document.getElementById) return;  	

  var labels, id, field;

  // Set focus and blur handlers to hide and show 
  // LABELs with 'overlabel' class names.
  labels = document.getElementsByTagName('label');
  for (var i = 0; i < labels.length; i++) {
	
    if (labels[i].className == 'overlabel') {

      // Skip labels that do not have a named association
      // with another field.
      id = labels[i].htmlFor || labels[i].getAttribute('for');
      if (!id || !(field = document.getElementById(id))) {
        continue;
      }

      // Change the applied class to hover the label 
      // over the form field.
      labels[i].className = 'overlabel-apply';

      // Hide any fields having an initial value.
      if (field.value !== '') {
        hideLabel(field.getAttribute('id'), true);
      }

      // Set handlers to show and hide labels.
      field.onfocus = function () {
        hideLabel(this.getAttribute('id'), true);
      };
      field.onblur = function () {
        if (this.value === '') {
          hideLabel(this.getAttribute('id'), false);
        }
      };

      // Handle clicks to LABEL elements (for Safari).
      labels[i].onclick = function () {
        var id, field;
        id = this.getAttribute('for');
        if (id && (field = document.getElementById(id))) {
          field.focus();
        }
      };

    }
  }
};

function hideLabel (field_id, hide) {
  var field_for;
  var labels = document.getElementsByTagName('label');
  for (var i = 0; i < labels.length; i++) {
    field_for = labels[i].htmlFor || labels[i].getAttribute('for');
    if (field_for == field_id) {
      labels[i].style.textIndent = (hide) ? '-10000px' : '0px';
      return true;
    }
  }
}

// Like stuff

function addLike (threadid) {
	phpvar = 'id='+threadid;
	var aj = new Ajax.Request(
		'like.php', {
			method:'get',
			parameters: phpvar,
			onComplete: getLike
		});
}

function getLike (likereturn) {
	$('likes').innerHTML = likereturn;
}

// Signup stuff
Event.addBehavior({
  'input#username:keyup': function() {
    $('username_check').show();
	var url = 'http://www.remixourband.com/beta/usercheck.php';
	var pars = 'un='+$F('username');
	var target = 'username_check';
	var myAjax = new Ajax.Updater(target, url, {method: 'post', parameters: pars});
  }
});

window.onload = function () {
  var myUpdater = new PeriodicalExecuter(getNewPosts, 3);
};

function getNewPosts () {
	var url = 'http://www.remixourband.com/beta/newposts.php';
	var pars = 'un='+20;
	var target = 'new_post_alert';
	var myAjax = new Ajax.Updater(target, url, {method: 'post', parameters: pars});
	$('new_post_alert').show();
}