var Tabmin =
{
	_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='';
		}
	},
	
	suggestLocation: function(value)
	{
		
		if(!!this._suggestTimeout)
			clearTimeout(this._suggestTimeout);
			
		this._suggestTimeout=setTimeout(function()
		{
			var a, json, i, locationSuggest, resp, err, option, options;
			this._suggestTimeout = null;
			
			if(value=='')
			{
				gebi('locationSuggestContainer').style.display='none';
				return;
			}
			
			
			a=new Ajax(true, 'txt');
			a.get('/tabmin/modules/complexes/ajax.php?verb=suggest&q='+value, function()
			{
				if(a.ready())
				{
					if(a.status()==200)
					{
						resp = a.response();
						//alert(resp);
						try
						{
							json = JSON.parse(resp);
						}
						catch(err)
						{
							alert('Error parsing JSON: '+resp);
							return;
						}
						
						locationSuggest = gebi('locationSuggest');
						
						while(locationSuggest.options.length > 0)
							locationSuggest.remove(0);
						
						locationSuggest.size = 10;//(json.length > 1 ? json.length : 2);
						query = json['query'];
						if(query!=value)
							return;
						
						for(var i in json.placemarks)
						{
							option = document.createElement('option');
							option.appendChild(document.createTextNode(json.placemarks[i].address));
							option.value = json.placemarks[i].address;
							
							
							try
							{
								locationSuggest.add(option);
							}
							catch(err)
							{
								locationSuggest.appendChild(option);
							}
							
						}
						locationSuggest.onchange = function(addressObj)
						{
							addressObj = addressObj.placemarks[this.selectedIndex+1];
							gebi('complexes_form').elements['address'].value = addressObj.street;
							gebi('complexes_form').elements['city'].value = addressObj.city;
							gebi('complexes_form').elements['state'].value = addressObj.state;
							gebi('complexes_form').elements['zip'].value = addressObj.zip;
							gebi('complexes_form').elements['country'].value = addressObj.country_name;
							gebi('complexes_form').elements['latitude'].value = addressObj.latitude;
							gebi('complexes_form').elements['longitude'].value = addressObj.longitude;
							
							gebi('complexes_form').elements['city'].disabled = false;
							gebi('complexes_form').elements['state'].disabled = false;
							gebi('complexes_form').elements['zip'].disabled = false;
							gebi('complexes_form').elements['country'].disabled = false;
							
							gebi('locationSuggestContainer').style.display='none';
						}.partial(json);
						
						if(locationSuggest.options.length==0)
						{
							option = document.createElement('option');
							option.appendChild(document.createTextNode('Could not find address'));
							try
							{
								locationSuggest.add(option);
							}
							catch(err)
							{
								locationSuggest.appendChild(option);
							}
						}
						gebi('locationSuggestContainer').style.display='';
						
						
					}
					else if(a.status()!=0)
						alert('HTTP Error: '+a.status()+' -- '+a.response());
				}
			});
		}.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);
}

function showAddFamilyForm(action)
{
	var trs, i;
	
	trs=document.getElementsByClassName('users_form_family');
	for(i=0; i < trs.length; i++)
	{
		if(action=='add')
			trs[i].style.display='';
		else
			trs[i].style.display='none';
	}
}*/

function displayOtherBox(opt)
{
	if(opt=='Realty Agent')
		gebi('referred_by_other').innerHTML ='Specify Agent: <input type="text" name="other" />';
	else if(opt=='Other')
		gebi('referred_by_other').innerHTML ='Specify Other: <input type="text" name="other" />';
	else
		gebi('referred_by_other').innerHTML = '';
}
