// Version 10.01.29
// Version 10.02.01
// Version 10.02.02
// Version 10.02.03
	// Added "Add Another Picture" javascript
// Version 10.02.12
	// Fixed picture disappearing when there is only one picture and the up or down arrow is clicked

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, i, j, button, span, ie6;
		
		action_buttons=document.getElementsByClassName('action_button');
		
		for(var i=0; i<action_buttons.length; i++)
		{
			button = action_buttons[i];
			if(button.title.length > 0)
			{
				for(j=0; j<button.childNodes.length; j++)
				{
					if(button.childNodes[j].nodeType!=3)
					{
						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);
						}
						break;
					}
				}
				
				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);
		}
	},
	
	addPicture: function(element, class_name)
	{
		var li;
		
		li = document.getElementsByClassName(class_name)[0];
		li = li.cloneNode(true);
		li.getElementsByTagName('input').item(0).value = '';
		li.getElementsByTagName('textarea').item(0).value = '';
		element.appendChild(li);
		window.scrollBy(0, 200);
	},
	
	movePicture: function(element, steps)
	{
		var lis, index, i, sibling;
		
		lis = element.parentNode.getElementsByTagName('li');
		if(lis.length<=1)
			return;
		
		index=false;
		for(i=0; i<lis.length; i++)
		{
			if(lis[i]==element)
				index=i;
		}
		
		if(index===false)
			return;
		
		sibling = element;
		if(steps>0)
		{
			if(index==lis.length-1)
				sibling = lis[0];
			else if(index==lis.length-2)
				sibling = lis[lis.length-1].nextSibling;
			else
				sibling = lis[(index+2) % lis.length];
		}
		else
		{
			if(index==0)
				sibling=lis[lis.length-1].nextSibling;
			else
				sibling = lis[(index-1) % lis.length];
		}
		
		element.parentNode.insertBefore(element.parentNode.removeChild(element, true), sibling);
		this.sendPictureSortOrder();
	},
	
	sendPictureSortOrder: function()
	{
		var i=0, sort_order = [];
		var a;
		var ul = document.getElementById('pics');
		var lis = ul.getElementsByTagName('li');
		for(i=0; i<lis.length; i++)
			sort_order.push(lis[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());
			}
		});
	}
};

