TP.ContractSearch = Class.create({
		defaultString: 'Framework Agreement name or code',
		moreInfoHolder: null,
		
		initialize: function(){
			$('contract_search').observe('focus', function() { 
				if ($('contract_search').value==this.defaultString) {
					$('contract_search').value = ''; 
				}
			}.bind(this));
		
			$('content').select('div.container div ol li').each(function(element){
				element.select('li.info a').each(function(moreInfoLink){
					moreInfoLink.observe('click', this.getMoreInfo.bindAsEventListener(this, moreInfoLink,  element));
				}.bind(this));			
			}.bind(this));
			
			$$('#content .js_required').each(function(ele){
				ele.style.display = 'inline';
			});
			
			$$('.fewer-options').each(function(ele){
				ele.observe('click', function(event){
					event.stop();
					
					$('fewer-options').show();
					$('more-options').hide();
				});
			});
			
			$$('.more-options').each(function(ele){
				ele.observe('click', function(event){
					event.stop();
					
					$('more-options').show();
					$('fewer-options').hide();
				});
			});
		},

		setupMoreInfo: function(){
			
			if(this.moreInfoHolder != null)
				this.moreInfoHolder.remove();
			
			this.moreInfoHolder = new Element('div');
			this.moreInfoHolder.className = 'more-info-wrapper';
			
			var div = new Element('div');
			div.className = 'more-info';
			
			var p = new Element('p');
			p.className = 'loading';
			
			p.innerHTML = '<strong>Loading...</strong> Please wait';
			
			div.insert(p);
			
			//this.moreInfoHolder.style.width = $('search-results').getWidth() + 'px';
			this.moreInfoHolder.insert(div);
		},
		
		getMoreInfo: function(event, ele, parent){
			event.stop();
			
			if(parent.hasClassName('active')){
				this.moreInfoHolder.remove();
				this.moreInfoHolder = null;
				parent.removeClassName('active');
				$('content').style.marginBottom = '0px';
				this.running = false;
			} else {
				var contentBottom = TP.findLocation($('content')).y + $('content').getHeight();
				
				if(this.moreInfoHolder != null) {
					this.moreInfoHolder.up().removeClassName('active');
					this.moreInfoHolder.remove();
					this.moreInfoHolder = null;	
					$('content').style.marginBottom = '0px';
				}
				
				parent.addClassName('active');
				var id = ele.href.match(/contract_id=([0-9]+)$/)[1];		
				this.setupMoreInfo();
				parent.insert(this.moreInfoHolder);
				
				var miBottom = TP.findLocation(this.moreInfoHolder).y + this.moreInfoHolder.getHeight();
				var diff = miBottom-contentBottom;
				if(diff > 0) {
					$('content').style.marginBottom = diff+10 + 'px';
				}
				
				var contentBottom = TP.findLocation($('content')).y + $('content').getHeight();
				
				new Ajax.Request(location.href.toString(), {
					method:'POST',
					parameters:'more_info='+id,
					onSuccess: function(transport){
						this.moreInfoHolder.select('div.more-info').first().innerHTML = transport.responseText;
						
						this.moreInfoHolder.select('p.close').first().observe('click', function(event){
							this.getMoreInfo(event, ele, parent);
							
						}.bind(this));
						
						var miBottom = TP.findLocation(this.moreInfoHolder).y + this.moreInfoHolder.getHeight();
						var diff = miBottom-contentBottom;
						if(diff > 0) {
							$('content').style.marginBottom = diff+10 + 'px';
						}
						
						this.running = false;
						Lightview.updateViews();
						Scratchpad.initCheckBoxes();
						Scratchpad.updatePageCheckBoxes();
						
					}.bind(this)
					
				});	
			}
		}

});


Event.observe(window, 'load', function(){
	new TP.ContractSearch();
});
