/******************************************************************************************
*
*	DEVELOPMENT TOOLBAR
*
*******************************************************************************************/
   	
   	if (!Atira) var Atira = {}
   
   /**
	* Constructor for the tadetable builder
	*/
    Atira.DevelopmentToolbar = function(id){
   		var element = document.getElementById(id);
   		if(element){
   			element.className = 'DevelopmentToolbar';
   			this.placeholder = element;
   		}
   	}
   	
   	Atira.DevelopmentToolbar.getCSS = function(){
   		return "developmenttoolbar.css";
   	}
   	
   	/**
   	* TODO
   	*/
   	Atira.DevelopmentToolbar.prototype.render = function(){
   		this.placeholder.innerHTML = Atira.DevelopmentToolbar._renderBar();
   	}
   	  

/******************************************************************************************
*
*	UTIL
*
*******************************************************************************************/
	
	Atira.DevelopmentToolbar.pnf = function(inputId){
		
		if(inputId){
			var element = document.getElementById(inputId);
			if(element){
				var inputValue = element.value;
				
				
				if(inputValue){
					new Ajax.Request('/client.do?', 
					{	asynchronous:true, 
						onSuccess:function(data){
							var w = 900; 
							var h = 705; 
							Atira.DevelopmentToolbar.pu('/popup/pnf.do?', w, h, true);
						},
						onFailure:function(data){
							element.value = '#err';
						},  
						method:'post', 
						postBody:'method=generatePNF&id=' + inputValue
						
					});	
				}
			}
		}	
	}
	
	Atira.DevelopmentToolbar.stc = function(inputId){
		
		if(inputId){
			var element = document.getElementById(inputId);
			if(element){
				var inputValue = element.value;
				
				
				if(inputValue){
					new Ajax.Request('/client.do?', 
					{	asynchronous:true, 
						onSuccess:function(data){
							var w = 900; 
							var h = 705; 
							Atira.DevelopmentToolbar.pu('/popup/stc.do?', w, h, true);
						},
						onFailure:function(data){
							element.value = '#err';
						},  
						method:'post', 
						postBody:'method=generateStockChart&id=' + inputValue
						
					});	
				}
			}
		}	
	}
	
	Atira.DevelopmentToolbar.upd = function(){
		if(CountSystem.getInstance().getPageController().toString() == 'OverviewController'){
			OverviewCtrl.updateSelectedClientToNextDate(false, function(data){
				try{
					OverviewController.update(false);
				}catch(e){
					//noop
				}				
			});
		}
	}
	
	Atira.DevelopmentToolbar.updAuto = function(){
		if(CountSystem.getInstance().getPageController().toString() == 'OverviewController'){
			OverviewCtrl.updateSelectedClientToNextDate(true, function(data){
				try{
					OverviewController.update(false);
				}catch(e){
					//noop
				}				
			});
		}
	}
	
	
	/**
	* this function creates a popup window
	*/
	Atira.DevelopmentToolbar.pu = function(url, width, height, usescroll, callback) {		
	      
	      var scroll = 'no';
	      if(usescroll){
	      	scroll= 'yes';
	      }
	      
	      var name = "Popup";
	
	      var callerFrame = window;
	
	      var style = "toolbar=no,location=no,directories=no,statusbar=no,menubar=no,scrollbars="+ scroll + ",resizable=yes,width=" + width + ",height=" + height;
	
	      var x = (screen.width - width) / 2;
	      var y = (screen.height - height) / 2;
	
	      var notSeenX = screen.availWidth - (x + width);
	      var notSeenY = screen.availHeight - (y + height);
	
	      if (notSeenX < 0) x += (notSeenX - 20); if (x < 0) x = 0;
	      if (notSeenY < 0) y += (notSeenY - 30); if (y < 0) y = 0;
	
	      style += ",left=" + x + ",top=" + y;
	
	      var openWin = callerFrame.open (url, "popup", style);
	
		  openWin.onCallback = callback; 
	
	      openWin.focus();
	    
	      return;
	}
	
/******************************************************************************************
*
*	STATELESS RENDERING METHODS  
*
*******************************************************************************************/

	/**
	* Renders the toolbar
	*
	* @return 			a html string
	*/
	Atira.DevelopmentToolbar._renderBar = function(){
		
		
		var html = '';	
			//Atira.DevelopmentToolbar.pnf(\'ticker_input_element\'); return false;
			
			//TOOLBAR
			html+= 	'<div class="content">';
			
			html+= 	'<input type="text" autocomplete="off" id="ticker_input_element" onkeydown="if(event.keyCode == 13){Atira.DevelopmentToolbar.pnf(\'ticker_input_element\'); return false;}" class="ticker_input" size="12" />';
			
			html+= 	'<div class="pnfit">';
			html+= 	'<button onclick="Atira.DevelopmentToolbar.pnf(\'ticker_input_element\'); return false;">P&F</button>';
			html+= 	'</div>';
			
			html+= 	'<div class="pnfit">';
			html+= 	'<button onclick="Atira.DevelopmentToolbar.upd(); return false;">UPDATE</button>';
			html+= 	'</div>';
			
			html+= 	'<div class="pnfit">';
			html+= 	'<button onclick="Atira.DevelopmentToolbar.updAuto(); return false;">AUTO UPDATE</button>';
			html+= 	'</div>';

			html+= 	'<div id="advanced_toggler_placeholder" class="pnfit">';
			html+= 	'</div>';
			
			/*
			html+= 	'<div class="pnfit">';
			html+= 	'<button onclick="Atira.DevelopmentToolbar.stc(\'ticker_input_element\'); return false;">StC</button>';
			html+= 	'</div>';
			*/
			html+=  '<div style="clear:both;"></div>';
			html+= 	'</form></div>';
					
		
		return html;
	}