
addDOMLoadEvent(vysn8Start);

function vysn8Start()
{
	vysn8SetupFilterForm();
	vysn8HideServices();
	vysn8HideAreas();
	vysn8ManageVariables();
}

function vysn8ManageVariables()
{
	if ($('vysn8_areas')) { 
		// build java only text and add functionality
		var element2 = Builder.node('p', [Builder.node('a', {id: 'vysn8_new_area_trigger', href:'#'}, 'Add new area'), Builder.node('span', {id:'vysn8_new_area'})]);
		$('vysn8_areas').insert({ before: element2 });
		// create ajax editor for add category
		new Ajax.InPlaceEditor( $('vysn8_new_area'), 'ajax.php', {okText:'add', callback: function(form, value) { return 'value=' + encodeURIComponent(value) + '&ajax=vysn8&task=newarea'}, externalControl: 'vysn8_new_area_trigger', onComplete: function() 
		{
			new Ajax.Updater('vysn8_areas', 'ajax.php', { method:'post', parameters: {ajax: 'vysn8', task: 'updateareas'}, evalScripts: true });
		}, highlightcolor: '#f07fbb'});
		
		vysn8AreaListFunctions();
	}

	if ($('vysn8_services')) { 
		// build java only text and add functionality
		var element2 = Builder.node('p', [Builder.node('a', {id: 'vysn8_new_service_trigger', href:'#'}, 'Add new service'), Builder.node('span', {id:'vysn8_new_service'})]);
		$('vysn8_services').insert({ before: element2 });
		// create ajax editor for add category
		new Ajax.InPlaceEditor( $('vysn8_new_service'), 'ajax.php', {okText:'add', externalControl: 'vysn8_new_service_trigger', callback: function(form, value) { return 'value=' + encodeURIComponent(value) + '&ajax=vysn8&task=newservice'}, onComplete: function() 
		{
			new Ajax.Updater('vysn8_services', 'ajax.php', { method:'post', parameters: {ajax: 'vysn8', task: 'updateservices'}, evalScripts: true });
		}, highlightcolor: '#f07fbb'});
		
		vysn8ServiceListFunctions();
		
		var element = Builder.node('div', [Builder.node('p', Builder.node('strong', 'Notes: '), 'Click on a title to edit the name. Click the red cross to permanently delete an item.')]);
		$('vysn8_services').insert({ after: element });	
	}
}

function vysn8AreaListFunctions()
{
		// setup ajax inplace editors for names
		$('vysn8_areas').select("li span").each(function(s){
			s.style.cursor = "pointer";
			var area_id = s.up().identify().substring(s.up().identify().lastIndexOf('_') + 1);
			new Ajax.InPlaceEditor( s, 'ajax.php', {okText:'save', highlightcolor: '#CCD9FF', savingText: "saving...", callback: function(form, value) { return 'value=' + escape(value) + '&area_id=' + area_id + '&ajax=vysn8&task=areaname'}});	
		});
		 
		 //setup ajax to show delete buttons and setup delete
		 $('vysn8_areas').select("li.okdelete").each(function(p){
			var deleteElement = Builder.node('img',{src: 'public_assets/vysn/images/delete-button-small.png', style: "margin-left: 0.5em"});
			p.insert({ bottom: deleteElement });
			$(deleteElement).style.cursor = "pointer";
			Event.observe($(deleteElement), 'click', function(e)     {
                var answer = confirm("Are you sure you want to delete this area?");
				if (answer) {
					var area_id = this.up().identify().substring(this.up().identify().lastIndexOf('_') + 1);
					new Ajax.Updater('vysn8_areas', 'ajax.php', { method:'post', parameters: {area_id: area_id, ajax: 'vysn8', task: 'deletearea'}, evalScripts: true });
				}
            }, false);
		});
}

function vysn8ServiceListFunctions()
{
		// setup ajax inplace editors for names
		$('vysn8_services').select("li span").each(function(s){
			s.style.cursor = "pointer";
			var service_id = s.up().identify().substring(s.up().identify().lastIndexOf('_') + 1);
			new Ajax.InPlaceEditor( s, 'ajax.php', {okText:'save', highlightcolor: '#CCD9FF', savingText: "saving...", callback: function(form, value) { return 'value=' + escape(value) + '&service_id=' + service_id + '&ajax=vysn8&task=servicename'}});	
		});
		 
		 //setup ajax to show delete buttons and setup delete
		 $('vysn8_services').select("li.okdelete").each(function(p){
			var deleteElement = Builder.node('img',{src: 'public_assets/vysn/images/delete-button-small.png', style: "margin-left: 0.5em"});
			p.insert({ bottom: deleteElement });
			$(deleteElement).style.cursor = "pointer";
			Event.observe($(deleteElement), 'click', function(e)     {
                var answer = confirm("Are you sure you want to delete this service?");
				if (answer) {
					var service_id = this.up().identify().substring(this.up().identify().lastIndexOf('_') + 1);
					new Ajax.Updater('vysn8_services', 'ajax.php', { method:'post', parameters: {service_id: service_id, ajax: 'vysn8', task: 'deleteservice'}, evalScripts: true });
				}
            }, false);
		});
}

function vysn8SetupFilterForm()
{
	if ($('contact_filter'))
	{		
		var z = $('contact_filter').select("input.button");
		Event.observe(z[0], 'click', function(e)
		{
			vysn8ShowResults();
			Event.stop(e);
		});	
	}
}

function vysn8HideFilterForm()
{
	vysn8RemoveCode();
	$('contact_filter').insert({before: '<p class="vysn8_fc"><a href="#">Click here to show the filter controls</a></p>'});
	Event.observe($('contact_filter').previous("p").down("a"), 'click', function(e) { vysn8ShowFilterForm(); Event.stop(e);});	
	$('contact_filter').hide();	
	$('content').scrollTo();
}

function vysn8ShowFilterForm()
{
	 vysn8RemoveCode();
	 $('contact_filter').insert({before: '<p class="vysn8_fc"><a href="#">Click here to hide the filter controls</a></p>'});
	 Event.observe($('contact_filter').previous("p").down("a"), 'click', function(e) { vysn8HideFilterForm(); Event.stop(e);});	
	 $('contact_filter').insert({after: '<p class="vysn8_fc"><a href="#">Click here to hide the filter controls</a></p>'});
	 Event.observe($('contact_filter').next("p").down("a"), 'click', function(e) { vysn8HideFilterForm(); Event.stop(e);});	
	 $('contact_filter').show(); 
}

function vysn8RemoveCode()
{
	var x = $$('.vysn8_fc');
	for(var i=0; i < x.length; i++)
    {  
    	$(x[i]).remove();
	}
}

function vysn8ShowResults()
{
	var filterFormOutput = $('contact_filter').serialize() + '&ajax=vysn8';
	new Ajax.Updater('vysn8', 'ajax.php', {method: 'post', evalScripts: true, parameters: filterFormOutput});
}

function vysn8HideService(y)
{
	y.hide();
	if (y.previous('p').down('img'))
		y.previous('p').down('img').remove();
	y.previous('p').insert({bottom: '<img src="public_assets/vysn/images/plus.png" alt="Click to show services" style="cursor: pointer">'});
	Event.observe(y.previous('p').down('img'), 'click', function(e) { vysn8ShowService(this.up("p").next("ul"));});	
}

function vysn8HideServices()
{	
	var x = $$('ul.vysn8_services');
	for(var i=0; i < x.length; i++)
    {  	
		vysn8HideService($(x[i]));
	}	
}

function vysn8ShowService(y)
{
	y.show();
	if (y.previous('p').down('img'))
		y.previous('p').down('img').remove();
	y.previous('p').insert({bottom: '<img src="public_assets/vysn/images/minus.png" alt="Click to hide services" style="cursor: pointer">'});
	Event.observe(y.previous('p').down('img'), 'click', function(e) { vysn8HideService(this.up("p").next("ul"));});
}

function vysn8HideArea(y)
{
	y.hide();
	if (y.previous('p').down('img'))
		y.previous('p').down('img').remove();
	y.previous('p').insert({bottom: '<img src="public_assets/vysn/images/plus.png" alt="Click to show areas covered" style="cursor: pointer">'});
	Event.observe(y.previous('p').down('img'), 'click', function(e) { vysn8ShowArea(this.up("p").next("ul"));});
}

function vysn8HideAreas()
{
	var x = $$('ul.vysn8_areas');
	for(var i=0; i < x.length; i++)
    {  	
		vysn8HideArea($(x[i]));
	}	
}

function vysn8ShowArea(y)
{
	y.show();
	if (y.previous('p').down('img'))
		y.previous('p').down('img').remove();
	y.previous('p').insert({bottom: '<img src="public_assets/vysn/images/minus.png" alt="Click to hide areas covered" style="cursor: pointer">'});
	Event.observe(y.previous('p').down('img'), 'click', function(e) { vysn8HideArea(this.up("p").next("ul"));});
}
