function getElementsByClass(searchClass,node,tag) {
	var classElements = new Array();
	if ( node == null )
		node = document;
	if ( tag == null )
		tag = '*';
	var els = node.getElementsByTagName(tag);
	var elsLen = els.length;
	var pattern = new RegExp('(^|\\s)'+searchClass+'(\\s|$)');
	for (i = 0, j = 0; i < elsLen; i++) {
		if ( pattern.test(els[i].className) ) {
			classElements[j] = els[i];
			j++;
		}
	}
	return classElements;
}

function pull(itemcode) {

	$.ajax({
		type: "POST",
		url: "/comics/pull",
		data: "itemcode=" + itemcode,
		success: function(response){

				var items = response.split(',');
				$("#pulls_" + items[0]).html(items[1]);
				$("#pull_" + items[0]).hide();
				$("#pulled_" + items[0]).fadeIn();

				if ( items[2] == 1 ) {
					var options = {maxvalue: 5, curvalue: 0, itemcode: itemcode};
					$('#' + itemcode).rating('', options);
					$('#' + itemcode).fadeIn();
				}
				else {
					//$("#pulled_" + items[0]).fadeIn();
				}
		}
	});

}

function rate(itemcode,rating) {

	$.ajax({
		type: "POST",
		url: "/comics/rate",
		data: "itemcode=" + itemcode + "&rating=" + rating,
		success: function(response){
				var items = response.split(',');
				$("#pulls_" + items[0]).html(items[1]);
				$("#rating_" + items[0]).html("Avg Rating: " + items[2]);
		}
	});

}

/**
 *	Delete comic from user
 */
function deleteComic(itemcode) {

	var answer = confirm ("Are you sure you want to remove this comic?")
	if (answer) {
		$.ajax({
			type: "POST",
			url: "/comics/delete",
			data: "itemcode=" + itemcode,
			success: function(response){
					$("#comic_" + response).slideUp();
			}
		});
	}
}


/**
 *	Delete post
 */
function deletePost(id) {

	var answer = confirm ("Are you sure you want to remove this post?")
	if (answer) {
		$.ajax({
			type: "POST",
			url: "/content/delete",
			data: "id=" + id,
			success: function(response){
					if ( response ) {
						$("#post_" + response).slideUp();
					}
					else {
						alert("Unable to delete post");
					}
			}
		});
	}
}

function textCounter(field, countfield, maxlimit) {
	if (field.value.length > maxlimit) {
		field.value = field.value.substring(0, maxlimit);
	}
	else {
		countfield.value = maxlimit - field.value.length;
	}
	return true;
}

function addComment( type, id, user_id, comment ) {

	$('#submitcomment').attr("disabled", true);

	$.ajax({
		type: "POST",
		url: "/comments/add",
		data: "type=" + type + "&id=" + id + "&user_id=" + user_id + "&comment=" + URLEncode(comment),
		success: function(response){

			if ( response ) {
				$("#addComment").queue(function () {
					$(this).fadeOut();
					$(this).dequeue();
				});
				$('#newCommentText').html(response);
				$("#newComment").queue(function () {
					$(this).fadeIn();
					$(this).dequeue();
				});
				$('#comment').html('');
			}
			else {
				$('#submitError').html("Error: Unable to add comment");
				$('#submitcomment').attr("disabled", false);
			}
		}
	});

}

function URLEncode (clearString) {
  var output = '';
  var x = 0;
  clearString = clearString.toString();
  var regex = /(^[a-zA-Z0-9_.]*)/;
  while (x < clearString.length) {
    var match = regex.exec(clearString.substr(x));
    if (match != null && match.length > 1 && match[1] != '') {
    	output += match[1];
      x += match[1].length;
    } else {
      if (clearString[x] == ' ')
        output += '+';
      else {
        var charCode = clearString.charCodeAt(x);
        var hexVal = charCode.toString(16);
        output += '%' + ( hexVal.length < 2 ? '0' : '' ) + hexVal.toUpperCase();
      }
      x++;
    }
  }
  return output;
}

function URLDecode (encodedString) {
  var output = encodedString;
  var binVal, thisString;
  var myregexp = /(%[^%]{2})/;
  while ((match = myregexp.exec(output)) != null
             && match.length > 1
             && match[1] != '') {
    binVal = parseInt(match[1].substr(1),16);
    thisString = String.fromCharCode(binVal);
    output = output.replace(match[1], thisString);
  }
  return output;
}

/**
 *	Add quote to new comment text area
 */
function addQuote( id ) {
	var comment = document.getElementById('comment_' + id).innerHTML;
	var username = document.getElementById('username_' + id).innerHTML;

	$.ajax({
		type: "POST",
		url: "/comments/quote",
		data: 'username=' + username + '&comment=' + comment,
		success: function(response){
			if ( response ) {
				$('#comment').val( response );
				$('#comment').focus();

				window.location = '#addComment';
			}
			else {
				$('#submitError').html("Error: Unable to add quote");
			}
		}
	});

}


/**
 *	Open review that contains spoilers
 */
function readreview() {

	$("#reviewdisclaimer").queue(function () {
		$(this).fadeOut();
		$(this).dequeue();
	});

	$("#reviewbody").queue(function () {
		$(this).fadeIn();
		$(this).dequeue();
	});
}

/**
 *	Display the rest of the news
 */
function morenews( id ) {
	new Effect.Fade( id + '-more', {duration: .5});
	new Effect.BlindDown( id, {duration: .5});
}

function lessnews( id ) {
	new Effect.BlindUp( id, {duration: .5});
	new Effect.Appear( id + '-more', {duration: .5});
}

/**
 *	Make a comic your potw
 */
function potw(itemcode) {

	$.ajax({
		type: "POST",
		url: "/comics/potw",
		data: "itemcode=" + itemcode,
		success: function(response){

				/**
				 *	Reset all other options
				 */
				var elements = getElementsByClass('potw_images');
				for (i = 0; i < elements.length; i++) {
					//document.getElementById(elements[i].id).innerHTML = 'Make POTW';
					//document.getElementById(elements[i].id).style.color = '#2981b3';
					document.getElementById(elements[i].id).src = '/images/comics/potw_rollover_off.gif';
				}

				var items = response.split(',');
				//$("#potw_" + items[0]).html('Your POTW');
				//$("#potw_" + items[0]).css("color","#212454");
				$("#potw_images_" + items[0]).attr('src', '/images/comics/potw_selected.gif');

				window.location.reload( false );
		}
	});

}