TP.RightMenu = Class.create({
		
	initialize: function() {
		
		// if the control isn't present we don't want to use this control on this page.
		if($('rightmenu-control') == null) {
			return false;
		}
	
		if(typeof TP.ProductStore == 'undefined') {
			// scratchpad is not displayed.
			if($('scratchpad') == null) {
				$('rightmenu-control').style.display= 'none';
			} else {
				$('rightmenu-control').style.display = 'block';
			}
			
			$('rightmenu').style.display = 'block';
			$('rightmenu').down().hide();
			$('rightmenu').down().style.position = 'relative';
			$('rightmenu').down().style.top = '0px';
			$('rightmenu').down().style.left = ($('rightmenu').getWidth()+20) + 'px';
			$('content').style.width = '100%';
				
		}
		
		$('rightmenu').style.overflowX = 'hidden';
		$('rightmenu').down().style.cssFloat = 'right';
		
		this.behavior.initialize();
			
	}, 
	
	activateMenu: function() {
		$('rightmenu-control').style.display = 'block';
	}, 
	
	deactivateMenu: function() {
		if($('rightmenu').select('fieldset') == null) {
			$('rightmenu-control').style.display = 'none';
			$('rightmenu').down().hide();
			$('content').style.width = "100%";
			$('rightmenu').down().style.left = ($('rightmenu').getWidth()+20) + 'px';
		}
	},
	
	behavior: {
		
		initialize: function() {
			$('rightmenu-control').down().observe('click', this.toggleRight.bindAsEventListener(this));
			//this.move = $('rightmenu').getWidth() + 20;
		},
		
		toggleRight: function(event) {
			event.stop();		
			
			$('rightmenu').down().style.width = $('rightmenu').getWidth()+'px';
			
			if($('rightmenu').down().style.display == 'none')
			{
				$('rightmenu').down().style.left = ($('rightmenu').getWidth()+20) + 'px';
				this.showRight($('rightmenu').getWidth() + 20);
			}
			else
			{
				this.hideRight($('rightmenu').getWidth() + 20);
			}
		}, 
	
		showRight: function(move) {
			
			$('content').style.width = "76.5%";
			$('rightmenu').down().style.display = 'block';
			new Effect.Move($('rightmenu').down(), { x:-move, y:0, mode:'relative', afterFinish: function(){
				
				$('rightmenu-control').down().innerHTML = 'Hide the right column';
				$('rightmenu-control').removeClassName('show-right');
				$('rightmenu-control').addClassName('hide-right');
				
				$('rightmenu').down().style.width = "";
				
			}.bind(this)});			
		}, 
		
		hideRight: function(move) {
			new Effect.Move($('rightmenu').down(), { x:move, y:0, mode:'relative', afterFinish: function(){
				
				$('content').style.width = "100%";
				$('rightmenu').down().style.display = 'none';
				
				$('rightmenu-control').down().innerHTML = 'Show the right column';
				$('rightmenu-control').removeClassName('hide-right');
				$('rightmenu-control').addClassName('show-right');
				
				$('rightmenu').down().style.width = "";
				
			}.bind(this)});						
		}
		
	}

});

document.observe('dom:loaded', function(){	
	TP.RightmenuInstance = new TP.RightMenu();

	if(TP.RightmenuInstance  != null && typeof TP.ScratchpadHighlight != 'undefined') {
		var registerAction = function() {
			try {
				TP.highlight.registerAction(function() {
					if($('scratchpad') && $('rightmenu').down().style.display == 'none') {
						$('rightmenu').down().style.width = $('rightmenu').getWidth()+'px';
						this.behavior.showRight($('rightmenu').getWidth() + 20);
					}
				}.bind(this));
			} catch (e) {
				setTimeout(registerAction, 100);
			}
		}.bind(TP.RightmenuInstance)
	
		registerAction();
	}
});
