var Toggle = Class.create(
{	
	initialize: function()
	{
		this.menu_state = 'shown';
		this.phases_state = false;
	},
		
	/*
	 * Show / hide menu on left
	 */
	menu: function()
	{
		var div_menu = $('menu');
		var div_toggle = $('toggle');
		var div_handle = $('toggle-handle');
		var div_main = $('main');

		switch (this.menu_state)
		{
			// Hide the menu
			case 'shown':
				this.menu_state = 'hidden';
				div_handle.setStyle({
					width: '20px',
					backgroundImage: 'url(' + theme_path + 'images/icons/view_both.png)',
					backgroundRepeat: 'no-repeat',
					backgroundPosition: '100% 50%'
				});
				
				jQuery('#' + div_menu.id).slideUp(animate_med,
					function() {
						jQuery('#' + div_main.id).animate({
							width:	'96%'
						}, animate_med);
						jQuery('#' + div_toggle.id).animate({
							left: '25px'
						}, animate_med)
					});
			break;
		
			// Show the menu
			case 'hidden':
				this.menu_state = 'shown';

				div_handle.setStyle({
					width: '20px',
					backgroundImage: 'url(' + theme_path + 'images/icons/view_right.png)',
					backgroundRepeat: 'no-repeat',
					backgroundPosition: '100% 50%'
				});
				
				jQuery('#' + div_main.id).animate({
					width:	'76%'
				}, animate_med, 'linear', function() {
					var div_menu = $('menu');
					jQuery('#' + div_menu.id).slideDown((animate_med));
				});
				
				jQuery('#' + div_toggle.id).animate({
					left: '20%'
				}, animate_fast)
			break;
		}
		div_handle.blur();
	},
		
	/*
	 * Toggle menu items
	 */
	menu_item: function(id)
	{
		var div_item = $(id);
		var handle = $(id + '-handle');
		var item_state = div_item.visible();

		if (item_state)
		{
			jQuery('#' + div_item.id).slideUp(animate_med);
			handle.setStyle({
				backgroundImage: 'url(' + theme_path + 'images/icons/folding_10px.png)',
				backgroundRepeat: 'no-repeat',
				backgroundPosition: '0 0'
			});
		}
		else
		{
			jQuery('#' + div_item.id).slideDown(animate_med);
			handle.setStyle({
				backgroundImage: 'url(' + theme_path + 'images/icons/folding_10px.png)',
				backgroundRepeat: 'no-repeat',
				backgroundPosition: '0 100%'
			});
		}
		handle.blur();
	},
		
	/*
	 * Toggle phases
	 */
	phase: function(id, force)
	{
		if (!id)
		{
			return;
		}

		var div_item = $(id);
		var handle = $(id + '-handle');

		var cur_state = div_item.getStyle('display'); //div_item.style.display;
		var new_state = (!force) ? ((!cur_state || cur_state == 'none') ? 'block' : 'none') : force;

		div_item.setStyle({
			display: new_state
		});
		handle.setStyle({
			backgroundRepeat:	'no-repeat',
			backgroundPosition:	(new_state == 'none') ? '0 0' : '0 100%'
		});

		handle.blur();
	},
	
	/*
	 * Toggle all phases
	 */
	all_phases: function(ids)
	{
		// Show hide all phases
		var new_state = (this.phases_state) ? 'none' : 'block';
		for (var i = 0; i < ids.size(); i++)
		{
			this.phase('phase' + ids[i], new_state);
		}
		
		// Change text
		var handles = document.getElementsByClassName('toggle-all-phases');
		for (var i = 0; i < handles.length; i++)
		{
			if (!this.phases_state)
			{
				handles[i].setStyle({
					backgroundPosition: '0 100%'
				});
				handles[i].setAttribute('title', hide_all);

			}
			else
			{
				handles[i].setStyle({
					backgroundPosition: '0 0'
				});
				handles[i].setAttribute('title', show_all);
			}
		}
		
		this.phases_state = (this.phases_state) ? false : true; 
	},
		
	/*
	 * Toggle search form
	 */
	search: function()
	{
		var div_item = $('search-form');
		var header = $('search-form-header');
		var handle = $('search-form-handle');
		var item_state = div_item.getStyle('display');

		if (item_state == 'none')
		{
			handle.setStyle({
				backgroundImage: 'url(' + theme_path + 'images/icons/folding_10px.png)',
				backgroundRepeat: 'no-repeat',
				backgroundPosition: '0 100%'
			});
			jQuery('#' + div_item.id).slideDown(animate_fast);
			$('sf').value = 1;
		}
		else
		{
			
			handle.setStyle({
				backgroundImage: 'url(' + theme_path + 'images/icons/folding_10px.png)',
				backgroundRepeat: 'no-repeat',
				backgroundPosition: '0 0'
			});
			jQuery('#' + div_item.id).slideUp(animate_fast);
			$('sf').value = 0;
		}
		handle.blur();
	},
	
	checkbox: function(source, name)
	{
		chk = document.getElementsByName(name);

		for (i = 0; i < chk.length; i++)
		{
			if(!chk[i].disabled)
			{
				chk[i].checked = source.checked ;
			}
		}
		
		source.blur();
	}
});


