/*======================================================================*\
|| #################################################################### ||
|| # PMNet Version 3.0												  #	||
|| # ---------------------------------------------------------------- # ||
|| # Copyright ©2003-2008 John Slone. All Rights Reserved. 			  #	||
|| # This file may not be redistributed in whole or significant part. # ||
|| #  																  # ||
|| #################################################################### ||
\*======================================================================*/

/* imageboard.js */

// Main imageboard object
var IB = {
	
	showThreadsPage : function(boardid, pagenum)
	{
		$.ajax({
			url : '/ajax.php',
			data : { 'action' : 'getforumthreads', 'boardid' : boardid, 'page' : pagenum },
			success : function(d)
			{
				$(".threadspages").html($('pagenav', d).text());
				$("#threadBody").html($('threadbody', d).text());
				location.hash = 'page=' + pagenum;
			}
		});
		return false;
	},
	
	checkNewThread : function(f)
	{
		if ( $(f).find("input[name='subject']").val() == '' )
		{
			alert("You cannot create a thread with no subject.");
			return false;
		}
		
		return true;
	},
	
	checkNewReply : function(f)
	{
		if ( $(f).find("input[name='comment']").val() == '' )
		{
			alert("You cannot submit a blank comment.");
			return false;
		}
		return true;
	},
	
	doJump : function(el)
	{
		val = $(el).val();
		if ( !isNaN(parseFloat(val)) && isFinite(val) )
		{
			location.href = '/ib/boards/' + val;
		}
		else
		{
			location.href = '/ib/' + $(el).val();
		}
	}
}

var Tag = {
	imgid : null,
	
	ready : function(imgid)
	{
		this.imgid = imgid;
		// Attach positioning event to the image click
		$("#tagimage").click(Tag.updatePos);
		// Create autocomplete for character selection
		$("#charstr").autocomplete(
			'/ajax_epguide.php',
			{
				minChars: 2,
				extraParams : { 'action' : 'searchchar', 'imgid' : imgid },
				width : 250,
				cellSeparator : '||',
				onItemSelect : function(d) { Tag.addCharacter(d); }
			}
		);
	},
	
	updatePos : function(e)
	{
		if(e.offsetX == null)
		{
			e.offsetX = parseInt(e.pageX - $("#tagimage").offset().left);
			e.offsetY = parseInt(e.pageY - $("#tagimage").offset().top);
		}
		$("#x_pos").val(e.offsetX);
		$("#y_pos").val(e.offsetY);
		$("#charstr").focus();
	},
	
	// Adds the character to the drop down, fills in its hidden id
	addCharacter : function(d)
	{
		var stuff = $(d).attr('extra');
		var charid = stuff[0];
		var dname = stuff[1];
		$("#charstr").val(dname);
		$("#charid_blank").val(charid);
		$("#addMeButton").focus();
	},
	
	// Add the actual entry to the database, clear the form afterwards
	add : function(f)
	{
		if ( $("#x_pos").val() == '' || $("#y_pos").val() == '' || $("#charid_blank").val()=='' )
		{
			// They have to fill everything in.
			return false;
		}
		$.ajax({
			url : '/ajax_epguide.php',
			data : $(f).serialize(),
			success : function(d)
			{
				// Do something here later, once the templates are figured out
				$("#characterTags").prepend(d);
				// Clear the data entry fields
				$("#x_pos").val('');
				$("#y_pos").val('');
				$("#charstr").val('');
				$("#charid_blank").val('');
			}
		});
	},
	
	// Remove a previously entered tag
	remove : function(el, imgtagid)
	{
		$.ajax({
			url : '/ajax_epguide.php',
			data : { 'action' : 'removeimgtag', 'imgtagid' : imgtagid },
			success : function(d)
			{
				$(el).parents('div.imgCharTag').remove();
			}
		});
		
		return false;
	}
}


/*======================================================================*\
|| ####################################################################
|| # File Revision:    $Revision: 1277 $
|| # Last Change:      $LastChangedDate: 2011-08-21 18:45:37 -0700 (Sun, 21 Aug 2011) $
|| # Last Commit By:   $Author: JDS $
|| ####################################################################
\*======================================================================*/
