TP.ScratchpadHighlight = Class.create({

	scratchpadNotificationArea: null,
	visible: false,
	scroller: null, 
	boundEvent: null,
	linkContainer: null,
	scratchpadNotificationAreaWidth: null,
	
	action: null,
	
	registerAction: function(func) {
		this.action = func;
	},
	
	fixPos: function(){
		if(TP.findLocation($('content')).y > TP.getScrollPosition()){
			this.scratchpadNotificationArea.style.position = 'absolute';
			this.scratchpadNotificationArea.style.top = TP.findLocation($('content')).y + 'px';
		} else {
			this.scratchpadNotificationArea.style.top = 0;
			this.scratchpadNotificationArea.style.position = 'fixed';
		}
	},

	initialize: function(){			
		this.scratchpadNotificationArea = new Element('div');
		this.scratchpadNotificationArea.addClassName('scratchpad-notify');
		this.scratchpadNotificationArea.hide();
		
		if(typeof TP.scratchpad_highlight_ie6fix == 'undefined'){
			this.fixPos();
			Event.observe(window, 'scroll', this.fixPos.bindAsEventListener(this));
		} else {
			TP.scratchpad_highlight_ie6fix(this.scratchpadNotificationArea);
		}
		var content = new Element('p');
		content.innerHTML = '<a href="#">Scratchpad Edited.</a>';

		this.scratchpadNotificationArea.insert(content);
		
		$$('body').first().insert(this.scratchpadNotificationArea);
	
		this.boundEvent = this.clickEvent.bindAsEventListener(this);
	
		
		this.scratchpadNotificationAreaWidth = this.scratchpadNotificationArea.getWidth();
		this.scroller = new TP.PageScroller(Scratchpad.panel);
	},
	
	highlightProducts: function(){
		this.show(Scratchpad.productContainer.select('h3').first());
	},
	
	highlightContracts: function(){
		this.show(Scratchpad.contractContainer.select('h3').first());
	},
	
	clickEvent: function(event){
		event.stop();
		
		if(this.action != null && !$('rightmenu').down().visible()) {
			this.action(event);
			this.scroller.scrollEvent();
		} else {
			this.scroller.scrollEvent();
		}
		
	},
	
	show: function(container){

		//alert((TP.findLocation($('container_new')).y + $('container_new').getWidth()));
		this.scratchpadNotificationArea.style.left = (TP.findLocation($('container_new')).x + $('container_new').getWidth() - this.scratchpadNotificationAreaWidth) + 'px';
		
		var scrolled = TP.getScrollPosition();	
		var loc = TP.findLocation(container);
		var scratchpadBottom = loc.y+container.offsetHeight;
		
		if(scrolled > scratchpadBottom || !$('rightmenu').down().visible()){

			// if the notification is visible we return.
			if(this.visible == true)
				return;
		
			this.visible = true;
			this.scratchpadNotificationArea.select('p a').first().observe('click', this.boundEvent);
			$$('body').first().insert(this.scratchpadNotificationArea);	
			TP.setOpacity(this.scratchpadNotificationArea, 0);
			this.scratchpadNotificationArea.show();
			
			TP.fadeIn(this.scratchpadNotificationArea, function(){ 
				setTimeout(function(){
					TP.fadeOut(this.scratchpadNotificationArea, function(){
						this.scratchpadNotificationArea.select('p a').first().stopObserving('click', this.boundEvent);
						this.scratchpadNotificationArea.remove();
						this.visible = false;
					}.bind(this));
				}.bind(this), 2000);
			}.bind(this));
		} else {
			// if the scratchpad is in view just make it blink
			new Effect.Highlight(container, {startcolor: '#266190', endcolor: '#B81C8C', restorecolor: '#266190'});
		}
	}	

});

Event.observe(window, 'load', function(){
	TP.highlight = new TP.ScratchpadHighlight();
});