

/*function hideallsections()
{
	$$('.section').each(
		function(node)
		{
			$(node).setStyle("display","none");
			var tabid=node.id+'_tab';
			if(tabid)
			{
				$(tabid).removeClass('selected');
			}
		});
}*/

function selectsectiontab(sectionid)
{
	$$('.sectiontab.selected').each(
		function(node)
		{
			node.removeClass('selected');
		});
	if(sectionid) 
	{
		var tabid=sectionid+'_tab';
		$(tabid).addClass('selected');
		$('categorycontrols').set('html','<div class="loading">Loading...</div>');
		$('categorycontents').set('html','<div class="loading">Loading...</div>');
		$$('.section')[0].setStyle('background-color',$(tabid).getStyle('background-color'));
	}
}

function loadsection(sectionid,categoryid,listingid)
{
	var controlurl = _baseURL + "index.php?/section/getcategorycontrols/"+sectionid+'/'+categoryid;
	selectsectiontab(sectionid);
	$('categorycontrols').set('html','<div class="loading">Loading...</div>');
	$('categorycontents').set('html','<div class="loading">Loading...</div>');
	new Request.HTML(
	{
		url: controlurl,
		method: 'get', 
		update: 'categorycontrols',
		evalscripts: true,
		onComplete: function()
			{
				selectcategorytab(categoryid);
			}
		
	}).get();
	loadcategory(sectionid,categoryid,listingid);
}

function selectcategorytab(categoryid)
{
	$$('.categorytab.selected').each(
		function(node)
		{
			node.removeClass('selected');
			node.setStyle('background-color','#dddddd');
			node.getElements('div').each(function(subnode)
				{
					if(subnode.getStyle('background-color')=='#ffffff')
					{
						subnode.setStyle('background-color','#dddddd');
					}
				});
		});
	var thetab=$(categoryid+'_tab');

	if(!thetab)
	{
		thetab=$('A_tab');
	}
	if(thetab)
	{
		thetab.addClass('selected');
		thetab.setStyle('background-color','#ffffff');
		thetab.getElements('div').each(function(subnode)
			{
				if(subnode.getStyle('background-color')=='#dddddd')
				{
					subnode.setStyle('background-color','#ffffff');
				}
			});
	}

}

function scrolltoentry(listingid)
{
	if(listingid!='' && $(listingid)!=null)
	{
		var el = $(listingid); // the hash
		if(el)
		{
			window.scrollTo(0,0);
			var scroll = new Fx.Scroll(window, { wait: false, duration: 2500, transition: Fx.Transitions.Quad.easeInOut });
			scroll.toElement(el);
		}
		else
		{
			new SmoothScroll({ duration:700 }, window);
		}
	}
}
var categoryissortable=false;
function loadcategory(sectionid,categoryid,listingid)
{
	var contenturl = _baseURL + "index.php?/phonebook/renderhtml";
	$('categorycontents').set('html','<div class="loading">Loading...</div>');
	new Request.HTML(
	{
		url: contenturl,
		method: 'post', 
		update: 'categorycontents',
		evalScripts: true,
		async: false,
		onComplete: function()
			{
				oddevenlistings();
				emaildemangler = new EmailDemangler();
				adTips = new Tips('.ad_small');
				selectcategorytab(categoryid);
				scrolltoentry(listingid);
				if(categoryissortable)
				{
					sortablelistings();
				}
			}
	}).post({
				sectionid: sectionid,
				categoryid: categoryid,
				listingid: listingid
			});

		
}

function oddeventable(table)
{
	table=$(table);
	var oddeven='even';
	table.getElements('tbody tr').each(function(el)
	{
		el.removeClass('odd');
		el.removeClass('even');
		el.addClass(oddeven);
		el.removeEvents('mouseover');
		el.removeEvents('mouseout');
		if(oddeven=='odd')
		{
			el.addEvent('mouseover',function(){this.removeClass('odd');this.addClass('mouseover')});
			el.addEvent('mouseout',function(){this.removeClass('mouseover');this.addClass('odd')});
		}
		else
		{
			el.addEvent('mouseover',function(){this.removeClass('even');this.addClass('mouseover')});
			el.addEvent('mouseout',function(){this.removeClass('mouseover');this.addClass('even')});
		}
		oddeven=(oddeven=='odd'?'even':'odd');
	});
	
}

function oddevenlistings()
{
	var listings=$('listings');
	var oddeven='even';
	if(listings)
	{
		listings.getElements('.listing').each(function(el)
		{
			el.removeClass('odd');
			el.removeClass('even');
			el.addClass(oddeven);
			el.getElements('span').each(function(el2)
			{
				el2.removeClass('odd');
				el2.removeClass('even');
				el2.addClass(oddeven);
			});
			oddeven=(oddeven=='odd'?'even':'odd');
		});
	}
}

var theSortableTable=null;
function sortabletable()
{
 	theSortableTable= new Sortables('#selecttablecontent tbody',
				{
					constrain:true,
					clone:false,
					revert:true,
					link:'chain',
					onComplete: function()
						{
							var thedata=this.serialize(function(el)
									{
										return el.id;
									});
							var theurl= _baseURL + 'index.php?/project/reorder';
							var therequest = new Request(
							{
								 url:theurl,
								 method: 'post',
								 onComplete: function(responseTree, responseElements, responseHTML, responseJavaScript)
								{
									oddeventable('selecttablecontent');
								}
							}).post({
								order: thedata
							});
						}
				});	
}

var theSortableListings=null;
function sortablelistings()
{
 	theSortableListings= new Sortables('.listings',
				{
					constrain:true,
					clone:false,
					revert:true,
					link:'chain',
					onComplete: function()
						{
							var thedata=this.serialize(function(el)
									{
										return el.id;
									});
							var theurl=_baseURL + 'index.php?/listing/reorder';
							var therequest = new Request(
							{
								 url:theurl,
								 method: 'post',
								 onComplete: function(responseTree, responseElements, responseHTML, responseJavaScript)
								{
									oddevenlistings();
								}
							}).post({
								order: thedata,
								categoryid: $$('.categoryselector select')[0].value
							});
						}
				});	
}


/*function loadalphacategory(lettertoload,sectiontofill,entrytoshow)
{
	$$('.alphatab.selected').each(
		function(node)
		{
			node.removeClass('selected');
			node.setStyle('background-color','#dddddd');
			node.getElements('div').each(function(subnode)
				{
					if(subnode.getStyle('background-color')=='#ffffff')
					{
						subnode.setStyle('background-color','#dddddd');
					}
				});
		});
	//$(sectiontofill+'_category').innerHTML="<div class='loading'>Loading...</div>";
	var url = _baseURL + "index.php?/category/getalphalistings/"+lettertoload+'/'+sectiontofill;
	new Request.HTML(
	{
		url: url,
		method: 'get', 
		update: sectiontofill+'_category',
		evalscripts: true,
		onComplete: function()
			{
				var thetab=$(lettertoload+'_tab');
				thetab.addClass('selected');
				thetab.setStyle('background-color','#ffffff');
				thetab.getElements('div').each(function(subnode)
					{
						if(subnode.getStyle('background-color')=='#dddddd')
						{
							subnode.setStyle('background-color','#ffffff');
						}
					});
				if(entrytoshow!='' && $(entrytoshow)!=null)
				{
					var el = $(entrytoshow); // the hash
					if(el)
					{
						window.scrollTo(0,0);
						var scroll = new Fx.Scroll(window, { wait: false, duration: 2500, transition: Fx.Transitions.Quad.easeInOut });
						scroll.toElement(el);
					}
					else
					{
						new SmoothScroll({ duration:700 }, window);
					}
				}
			}
	}).get();
	
}*/

function sendinvoice(listingid)
{
	var jsonRequest = new Request.JSON(
	{
		url: _baseURL + 'index.php?/organization/invoice/' + listingid, 
		onComplete: function(options,txt)
		{
			notimooManager.show({sticky: 1, title: '',message: '<img style="float:left;" src="'+_baseURL+'system/application/assets/siteimages/messagemessage.png" />'+txt});
		}
	}).get();
}

function sendconfirmation(listingid)
{
	var jsonRequest = new Request.JSON(
	{
		url: _baseURL + 'index.php?/organization/confirm/' + listingid, 
		onComplete: function(options,txt)
		{
			notimooManager.show({sticky: 1, title: '',message: txt});
		}
	}).get();
}

function onchangedphonebook(phonebookcontrol)
{
	var selectedphonebookid = phonebookcontrol[phonebookcontrol.selectedIndex].value;
	if(selectedphonebookid==0)
	{
		document.location.href= _baseURL + 'index.php?/phonebook/edit';
	}
	else
	{
		document.location.href= _baseURL + 'index.php?/phonebook/change/' + selectedphonebookid;
	}
}