var Tabmin =
{
	_suggest: null,
	_suggestTimeout: null,
	
	showFormTab: function(tab_num, querystring)
	{
		var tab_open=false;
		
		if(!querystring)
			querystring='';
		
		if(!tabSet.tabClosed(tab_num))
			tab_open=true;
		
		tabSet.showTab(tab_num);
		if(tab_open)
		{
			if(!confirm('There is a form already open. Are you sure you want to load another one over it? Any changes you have made will be lost.'))
				return;
		}
		
		tabSet.tabs[tab_num].reload(querystring);
	},
	
	appendTooltips: function()
	{
		var action_buttons, button, span, ie6;
		
		action_buttons=document.getElementsByClassName('action_button');
		
		for(var i=0; i<action_buttons.length; i++)
		{
			button = action_buttons[i];
			if(button.childNodes.length > 0 && button.title.length > 0)
			{
				button.className+=' tool';
				
				span = document.createElement('span');
				span.className='tip';
				span.innerHTML=button.title;
				button.appendChild(span);
				
				ie6=('\v'=='v' && navigator.appVersion.match(/MSIE [56]\./));
				if(ie6)
				{
					button.onmouseover=function(tip)
					{
						tip.className='tip_hover';
					}.partial(span);
					
					button.onmouseout=function(tip)
					{
						tip.className='tip';
					}.partial(span);
				}
			}
			
			button.title='';
		}
	},
	
	suggest: function(input, module, verb)
	{
		if(!!this._suggestTimeout)
			clearTimeout(this._suggestTimeout);
		
		if(input.value=='')
		{
			if(!!this._suggest)
			{
				this._suggest.parentNode.removeChild(this._suggest);
				this._suggest=null;
			}
		}
		else
		{
			this._suggestTimeout=setTimeout(function()
			{
				var a, json, i, select_box, resp, err, option, options;
				
				this._suggestTimeout = null;
				
				if(!!module)
					url='modules/'+module+'/ajax.php';
				else
					url='suggest_ajax.php';
				if(!verb)
					verb='suggest';
				
				a=new Ajax(true, 'txt');
				a.get('/tabmin/'+url+'?verb='+verb+'&q='+input.value, function()
				{
					if(a.ready())
					{
						if(a.status()==200)
						{
							resp = a.response();
							
							try
							{
								json = JSON.parse(resp);
							}
							catch(err)
							{
								alert('Error parsing JSON: '+resp);
								return;
							}
							if(json['query']!=input.value)
								return;
							
							if(!!this._suggest)
								this._suggest.parentNode.removeChild(this._suggest);
							this._suggest=document.createElement('div');
							this._suggest.className='tabmin_suggest';
							select_box=document.createElement('select');
							select_box.size = 11;//(json.length > 1 ? json.length : 2);
							
							this._suggest.appendChild(select_box);
							input.parentNode.insertBefore(this._suggest, input.nextSibling);
							
							for(i=0; i<json.suggestions.length; i++)
							{
								option = document.createElement('option');
								option.appendChild(document.createTextNode(json.suggestions[i].name));
								option.value='1';
								
								try
								{
									select_box.add(option);
								}
								catch(err)
								{
									select_box.appendChild(option);
								}
							}
							
							for(; i<11; i++)
							{
								option = document.createElement('option');
								if(i==10)
									option.appendChild(document.createTextNode('Cancel'));
								option.value='0';
								
								try
								{
									select_box.add(option);
								}
								catch(err)
								{
									select_box.appendChild(option);
								}
							}
							
							select_box.onchange = function()
							{
								var fields, i;
								
								if(select_box.value == '1')
								{
									fields=json.suggestions[select_box.selectedIndex].fields;
									for(i=0; i<fields.length; i++)
									{
										if(!!(input=select_box.form.elements[fields[i].name]))
										{
											input.value=fields[i].value;
											if(fields[i].disabled===true || fields[i].disabled===false)
												input.disabled=fields[i].disabled;
										}
									}
								}
								
								this._suggest.parentNode.removeChild(this._suggest);
								this._suggest=null;
							}.bind(this);
							
							if(select_box.options.length==0)
							{
								option = document.createElement('option');
								option.appendChild(document.createTextNode('Could not find address'));
								try
								{
									select_box.add(option);
								}
								catch(err)
								{
									select_box.appendChild(option);
								}
							}
						}
						else if(a.status()!=0)
							alert('HTTP Error: '+a.status()+'; '+a.response());
					}
				}.bind(this));
			}.bind(this), 500);
		}
	},
	
	movePicture: function(li, step)
	{
		var i = 0;
		var sibling = li;
		
		if(step>0)
		{
			if(li.nextSibling==null)
				sibling = li.parentNode.childNodes[0];	
			else
				sibling = li.nextSibling.nextSibling;
		}
		else
			sibling = li.previousSibling;
		
		li.parentNode.insertBefore(li.parentNode.removeChild(li, true), sibling);
		Tabmin.sendPictureSortOrder();
	},
	
	sendPictureSortOrder: function()
	{
		var i=0, sort_order = [];
		var a;
		var ul = document.getElementById('pics');
		for(i=0; i<ul.childNodes.length; i++)
			sort_order.push(ul.childNodes[i].id.replace(/\D/g, ''));
		
		a=new Ajax(true, 'txt');
		a.post('/tabmin/modules/pictures/ajax.php','verb=sort&order='+sort_order.join(','), function()
		{
			if(a.ready())
			{
				if(a.status()==200)
				{
					resp = a.response();
				}
				else if(a.status()!=0)
					alert('HTTP Error: '+a.status()+' -- '+a.response());
			}
		});
	}
};

function blurTr(trID)
{	
	gebi(trID).style.backgroundColor = '#ee8888';
}

function unblurTr(trID)
{	
	gebi(trID).style.backgroundColor = '';
}

function removeTr(trID)
{
	var tr = gebi(trID);
	tr.style.display = "none";
}

function blurPictureTds(id)
{	
	blurTr('picturePhoto'+id);
	blurTr('pictureCaption'+id);
}

function unblurPictureTds(id)
{	
	unblurTr('picturePhoto'+id);
	unblurTr('pictureCaption'+id);
}
/*
function removePictureTds(id)
{
	removeTr('picturePhoto'+id);
	removeTr('pictureCaption'+id);
}
*/
