// Globals
gearname = '';
gearEID = '';

function CloseEditCategories()
{
	$('editcategory').innerHTML = '';
}

//========================================================================================
function EditCategories( entitytype, entityid, tagsearch )
{
	new Ajax.Request( '/category/editentity',
	{
		method:   'post',
		postBody: 'ajax=1&&entitytype='+entitytype+'&entityid='+entityid+'&tagsearch='+escape(tagsearch),
		onSuccess: function( t )
		{
			Modalbox.show( t.responseText, {width: '800', height: '500', title: 'Edit Categories' } );			
		}
	} );
}

//========================================================================================
function GetCategories( entitytype, entityid, tagsearch )
{
	new Ajax.Request( '/category/getentitycategories',
	{
		method:   'post',
		postBody: 'ajax=1&&entitytype='+entitytype+'&entityid='+entityid+'&tagsearch='+escape(tagsearch),
		onSuccess: function( t )
		{
			$('categories').innerHTML = t.responseText;	
			new Effect.Highlight( 'categories' );
		}
	} );
}

//========================================================================================
function UpdateCategory( entitytype, entityid, tagid, tagsearch )
{	
	var checked = $('tag_'+tagid).checked;
	
	if ( checked )
	{
		$('tagrow_'+tagid).style.backgroundColor = 'lightgreen';
		UpdateTag( entitytype, entityid, tagid, 1, tagsearch );		
	}
	else
	{
		$('tagrow_'+tagid).style.backgroundColor = '#EFEFEF';
		UpdateTag( entitytype, entityid, tagid, 0, tagsearch )
	}		
}

//========================================================================================
function UpdateTag( entitytype, entityid, tagid, value, tagsearch )
{
	new Ajax.Request( '/tag/update',
	{
		method:    'post',
		postBody:  'ajax=1&tagid='+tagid+'&entitytype='+entitytype+'&entityid='+entityid+'&value='+value,
		onComplete: function ( t )
		{
			GetCategories( entitytype, entityid, tagsearch );
		}
	} );
}

//========================================================================================
function SaveTags( entitytype, entityid )
{
	var tag = $('tag').getValue();
	$('tag').setValue( '' );
	
	new Ajax.Request( '/tag/save',
	{
		method:   'post',
		postBody: 'ajax=1&tag='+escape(tag)+'&entitytype='+entitytype+'&entityid='+entityid,
		onSuccess: function( t )
		{
			$('taglist').innerHTML = t.responseText;
			new Effect.Highlight( 'taglist' );
			$('tagforminstructions').style.display = 'none';
		}
	} );
	
	return false;
}

//========================================================================================
function DeleteTag( tag_entity_id )
{	
	new Ajax.Request( '/tag/delete/' + tag_entity_id,
	{
		postBody: 'ajax=1', 
		onSuccess: function( t )
		{
			$('taglist').innerHTML = t.responseText;
			new Effect.Highlight( 'taglist' );			
		}
	} );
	
	return false;
}

//========================================================================================
function RemoveRelation( eeid, entityid )
{	
	new Ajax.Request( '/ee/delete/' + eeid + '/' + entityid,
	{
		postBody: 'ajax=1', 
		onSuccess: function( t )
		{
			$('related').innerHTML = t.responseText;
			new Effect.Highlight( 'related' );			
		}
	} );
	
	return false;
}

//========================================================================================
var EditEntity = Class.create(
{	
	setOptions: function( options )
	{
		Object.extend( this.options, options || {} );
	},

	initialize: function()
	{
	},
	
	viewlist: function( obj, options )
	{
		this.setOptions( options );		
		var response     = $(this.options.response);
		var objstatus       = $(this.options.objstatus);		
		//objstatus.innerHTML = "Loading...";
		
		new Ajax.Request( this.options.path + '/viewlist/' + obj.id,
		{
			postBody: 'ajax=1&pageid='+obj.pageid,
			onSuccess: function( t )
			{
				//objstatus.innerHTML   = '';
				response.innerHTML = t.responseText;
				new Effect.Highlight( response, { duration: 0.5 } );
			}
		} );
	},
	
	add: function( obj, options )
	{
		this.setOptions( options );
		
		var objstatus 		= $(this.options.objstatus);
		var title		= this.options.createverb ? this.options.createverb : 'Create';
			 title      = title + ' ' + this.options.windowtitle;
		var width		= this.options.width;
				
		//objstatus.innerHTML = 'Adding...';

		new Ajax.Request( this.options.path + '/create/' + obj.entitytype + '/' + obj.entityid,
		{
			postBody: 'ajax=1', 
			onSuccess: function( t )
			{
				//objstatus.innerHTML 	= '';				
				Modalbox.show( t.responseText, {title: title, width: width} );				
			}
		} );	
	},

	editor: function( obj, options )
	{
		this.setOptions( options );

		var objstatus 		= $(this.options.objstatus);
		var title		= 'Edit ' + this.options.windowtitle;
		var width		= this.options.width;

		//objstatus.innerHTML = 'Loading...';

		new Ajax.Request( this.options.path + '/edit/' + obj.id,
		{
			postBody: 'ajax=1&response='+this.options.response,
			onSuccess: function( t )
			{
				//objstatus.innerHTML 	= '';
				Modalbox.show( t.responseText, {title: title, width: width } );				
			}
		} );
	},
	
	summary: function( obj, options )
	{
		this.setOptions( options );

		var objstatus 		= $(this.options.objstatus);
		var title		= 'Edit ' + this.options.windowtitle;
		var width		= this.options.width;

		//objstatus.innerHTML = 'Loading...';
		
		new Ajax.Request( this.options.path + '/edit/' + obj.id,
		{
			postBody: 'ajax=1&response='+this.options.response,
			onSuccess: function( t )
			{
				//objstatus.innerHTML 	= '';
				Modalbox.show( t.responseText, {title: title, width: width } );				
			}
		} );
	},
	
	save: function( form, options )
	{
		this.setOptions( options );
		
		var objstatus 		= $(this.options.objstatus);
		var response 	= $(this.options.response);
		
		//objstatus.innerHTML = 'Saving...';
		
		form.request(
		{
			
			method: 		'post',
			parameters: { ajax:'1' },				
			onComplete: function( t )
			{
				//objstatus.innerHTML 		= '';
				
				if ( response )
				{
					response.innerHTML 	= t.responseText;
					new Effect.Highlight( response, { duration: 2.0 } );
				}
				
				Modalbox.hide();				
			}
		});
	},
	
	deleteobj: function( obj, options )
	{
		this.setOptions( options );
		
		var objstatus 		= $(this.options.objstatus);
		var response 	= $(this.options.response);
		
		//objstatus.innerHTML = 'Removing...';

		new Ajax.Request( this.options.path + '/delete/' + obj.id,
		{
			postBody: 'ajax=1', 
			onSuccess: function( t )
			{
				//objstatus.innerHTML 		= '';
				response.innerHTML 	= t.responseText;
				new Effect.Highlight( response, { duration: 1.0 } );
			}
		} );
	}	
});

var EditURL = Class.create( EditEntity,
{
	options: 
	{
		path: 		 '/url',
		objstatus: 	 'url_status',
		response: 	 'urls',
		windowtitle: 'URL',
		width:       500 		
	}
});

var EditName = Class.create( EditEntity,
{
	options: 
	{
		path: 		 '/name',
		objstatus:      'name_status',
		response: 	 'names',
		windowtitle: 'Name',
		width:       500 		
	}
});

var EditContact = Class.create( EditEntity,
{
	options: 
	{
		path: 		 '/contact',
		objstatus: 	 'contact_status',
		response: 	 'contacts',
		windowtitle: 'Contact',
		width:       500 				
	}
});

var EditLocation = Class.create( EditEntity,
{
	options: 
	{
		path: 		 '/location',
		objstatus: 	 'location_status',
		response: 	 'locations',
		windowtitle: 'Location',
		width:       800 				
	}
});

var EditImage = Class.create( EditEntity,
{
	options: 
	{
		path: 		 '/image',
		objstatus: 	 'image_status',
		response: 	 'images',
		createverb:  'Upload',
		windowtitle: 'Image',
		width:       800
	}
});

var EditWiki = Class.create( EditEntity,
{
	options: 
	{
		path: 		 '/wiki',
		width:       1024,
		windowtitle: 'Wiki'
	},
	
	save: function( $super, form, options )
	{
		var content = FCKeditorAPI.GetInstance('body').GetXHTML();
		form.body.setValue( content );

		$super(form, options);
	}
});

var RelHowto = Class.create( EditEntity,
{
	options:
	{
		path: 		 '/howto',
		width:       800,
		windowtitle: 'Howto'
	},
	
	
	add: function( obj )
	{
		new Ajax.Request( '/ee/create/' + obj.entitytype + '/' + obj.entityid,
		{
			method:   'post',
			postBody: 'ajax=1&&entitytype='+obj.entitytype+'&entityid='+obj.entityid,
			onSuccess: function( t )
			{
				Modalbox.show( t.responseText, {width: '600', height: '180', title: 'Edit Related Howto' } );			
			}
		} );
	},
	
	/*remove function( obj )
	{
		
	} */
});

var editlocation = new EditLocation;
var editurl      = new EditURL;
var editname     = new EditName;
var editcontact  = new EditContact;
var editwiki     = new EditWiki;
var editimage    = new EditImage;
var relatedhowto = new RelHowto;

//========================================================================================
function SaveName( form )
{		
	$('editname_status').innerHTML = 'Saving...';
	
	form.request(
	{
		method: 		'post',
		parameters: { ajax:'1' },				
		onComplete: function( t )
		{						
			$('editname_status').innerHTML 	= '';
			$('namelist').innerHTML 			= t.responseText;
			new Effect.Highlight( 'namelist' );
			$('editname').hide();
		}
	});	
}

//========================================================================================
function DeleteName( nameid )
{
	$('editname_status').innerHTML = 'Deleting...';
	new Ajax.Request( '/name/delete/' + urlid,
	{
		postBody: 'ajax=1', 
		onSuccess: function( t )
		{
			$('editname_status').innerHTML 	= '';
			$('namelist').innerHTML 			= t.responseText;
			new Effect.Highlight( 'namelist' );			
		}
	} );	
}

//========================================================================================
function EditName( nameid )
{
	$('editname_status').innerHTML = 'Loading...';
	
	new Ajax.Request( '/name/edit/' + urlid,
	{
		postBody: 'ajax=1', 
		onSuccess: function( t )
		{
			$('editname_status').innerHTML 	= '';			
			$('editname').innerHTML 			= t.responseText; 
			new Effect.Highlight( 'editname', { duration: 1.0 } );
		}
	} );
}


//========================================================================================
function GetEntities( args ) 
{
	new Ajax.Request( '/entity/module',
	{

		method:   'post',
		postBody: 'ajax=1&entitytype='+args.entitytype+'&pageid='+args.pageid+'&sort='+args.sort+'&tag='+args.tag+'&dir='+args.dir,
		onSuccess: function( t )
		{
			
			//alert('AJAX REQUEST!!!');
			$('entitylist').innerHTML = t.responseText;
		}
	} );
}

//========================================================================================
function GetForumModule( args )
{
	var objstatus 			= $( args.id + '_status' );	
	var qstr 			= $H(args).toQueryString();;
	//objstatus.innerHTML 	= 'Loading';
	
	new Ajax.Request( '/forum/module',
	{
		method:   'post',
		postBody: 'ajax=1&'+qstr,
		onSuccess: function( t )
		{
			//objstatus.innerHTML 		= '';
			$(args.id).innerHTML = t.responseText;
		}
	} );
}

//========================================================================================
// observing all starboxes
document.observe('starbox:rated', saveStar);


//========================================================================================
function saveStar( event )
{
	var parms 		= event.memo.identity.split( '_' );	
	var entityid   = parms[1];
	
	$('ratingstatus').innerHTML = 'Updating...';
	new Ajax.Request('/rating/save', 
	{
		postBody:   'ajax=1&entityid='+entityid+'&rating='+event.memo.rated,
		onComplete: function( t ) 
		{ 
			$('ratingstatus').innerHTML = t.responseText;
			new Effect.Highlight( 'ratingstatus' );			
		}
	} );
}

//========================================================================================
function limitText(limitField, limitCount, limitNum) {
	if (limitField.value.length > limitNum) {
		limitField.value = limitField.value.substring(0, limitNum);
	} else {
		limitCount.value = limitNum - limitField.value.length;
	}
}

//========================================================================================
function COMMENT_Save( obj )
{	
	reviewbody = $('body').value;
	commentid = $('commentid').value;
	//alert ( reviewbody );
	
	$('commentform').request(
	{
		method: 		'post',
		parameters: { ajax:'1' },
		onComplete: function( t )
		{	
			//alert ("2:" + $('commentid').value );
			
			Modalbox.hide();
			//alert ( $('comment_' + commentid ).innerHTML );
			$('comment_' + commentid ).innerHTML = reviewbody;
			new Effect.Highlight( $('comment_' + commentid ), { duration: 2.0 } );		
		}
	} );
}


//========================================================================================
function COMMENT_Edit( obj )
{
	new Ajax.Request( '/comment/edit/'+obj.entityid,
	{
		postBody:   'ajax=1&entityid='+obj.entityid+'&commentid='+obj.commentid,
		onComplete: function( t )
		{ 
			modalreturn = Modalbox.show( t.responseText, {width: '600', height: '275', title: 'Edit Collection' } );
	
			//alert("Return from modal: " + modalreturn);
			return modalreturn;
		}
	} );
}

