YAHOO.namespace("calculate");
/*YAHOO.calculate = function() {
    $E = YAHOO.util.Event;
    $D = YAHOO.util.Dom;
    $ = $D.get;
    
	
}();*/
//YAHOO.util.Event.addListener(window, "load", init);
YAHOO.calculate.init = function() {
	$E = YAHOO.util.Event;
    $D = YAHOO.util.Dom;
    $ = $D.get;
	
	// Instantiate a Panel from markup
	YAHOO.calculate.calcPanel = new YAHOO.widget.Panel("calcPanel", { 
                        modal: true,
                        fixedcenter: false,
                        underlay: "shadow", 
                        draggable: false,
                        visible: false,
                        constraintoviewport: false, 
                        y:140,
                        close: false, 
                        zindex: 200,
                        width:'400px',
                        effect: {effect:YAHOO.widget.ContainerEffect.FADE, duration:0.5 }
                    } );
                    
	YAHOO.calculate.calcPanel.render();
	
	YAHOO.calculate.generateListeners();
}


YAHOO.calculate.generateListeners = function(e){
	YAHOO.util.Event.addListener("calc", "click", YAHOO.calculate.show, YAHOO.calculate, true);
	YAHOO.util.Event.addListener("hide", "click", YAHOO.calculate.hide, YAHOO.calculate, true);
	YAHOO.util.Event.addListener("unit", "change", updateUnits);
	YAHOO.calculate.inputListeners();
}
	
YAHOO.calculate.inputListeners = function(e){
	
	inputEls = $D.getElementsByClassName('room-dim');
	YAHOO.util.Event.on(inputEls, 'keydown', function(e){ 
		   // YAHOO.util.Dom.addClass(this, 'hasFocus'); 
		   gTotalEl = $('grand-total');
		   
		   var code;
			if (!e) var e = window.event;
			if (e.keyCode) code = e.keyCode;
			else if (e.which) code = e.which;
			
			var isDelete = false;
			var isArrowKey = false;
			if(code == 8) { // backspace
				isDelete = true
			}
			if (code == 46) { // delete
				isDelete = true;
			}
			
			if (code >= 37 && code <= 40 ) {
				isArrowKey = true;
			}		
			/*	Arrow Keys = 37 - 40	*/
			
			var character = String.fromCharCode(code);
			var currentValue = 0
			
			//handle delete			
			if(isDelete) {
				if(this.value.length > 1) {
					currentValue = parseInt(this.value.substr(0,(this.value.length-1)));
				} else if (this.value.length == 1) {
					currentValue = 0
				}
			} else {
				//handle characters
				if(isNaN(character)) {
					//currentValue = this.value;
					$E.stopEvent(e);
					return false;
					
				} else {
					currentValue = this.value + character;
				}
			}
			
			tableRow = $D.getAncestorByTagName(this, "tr");
			firstCellArray =  $D.getElementsByClassName("col-2","td", tableRow);
			firstCell =  firstCellArray[0];
			secondCellArray =  $D.getElementsByClassName("col-4","td", tableRow);
			secondCell =  secondCellArray[0];
			
			firstCellInput =  firstCell.childNodes[0];
			secondCellInput =  secondCell.childNodes[0];
			
			totalCellArray =  $D.getElementsByClassName("col-6","td", tableRow);
			totalCell = totalCellArray[0];
			totalCellInput = totalCell.childNodes[0];
			
			
			dimIndex = parseInt(this.name.substr((this.name.length-1),(this.name.length)));
			
			totalRowValue = 0;
			if (dimIndex == 1) {
				totalRowValue = secondCellInput.value*currentValue;
			} else if (dimIndex == 2) {
				totalRowValue = firstCellInput.value*currentValue;
			}
			
			var unitEl = document.getElementById('unit');
			var unit = unitEl.options[unitEl.selectedIndex].value;
			
			if (unit == 'm') {
				if  (totalRowValue > 50) {
					var message = "Error: please check that the values and units of measure that you have entered are correct.\n";
					alert(message);
					this.value = currentValue.substr(0, (currentValue.length-1));
					return false;
					
				}
			} else if (unit == 'ft') {
				if  (totalRowValue > 539) {
					var message = "Error: please check that the values and units of measure that you have entered are correct.\n";
					alert(message);
					this.value = currentValue.substr(0, (currentValue.length-1));
					return false;
				}
			}
			
			if(!isInt(totalRowValue) && !isDelete) { // backspace and delete
				$E.stopEvent(e);				
			} else {
				totalCellInput.value = totalRowValue;
				gTotalEl.value = tableTotal();
			}
		});
}
	
YAHOO.calculate.show = function(e) {
	$E = YAHOO.util.Event;
    $D = YAHOO.util.Dom;
    $ = $D.get;

    $E.stopEvent(e); //stop the link's true location

    
    YAHOO.calculate.calcPanel.show();                
}

YAHOO.calculate.hide = function(e) {
	$E = YAHOO.util.Event;
    $D = YAHOO.util.Dom;
    $ = $D.get;

    $E.stopEvent(e); //stop the link's true location
    
    /*calcPanel_c = $("calcPanel_c");
    
    calcPanel_c.style.display = "block";
    
    elements = $D.getElementsByClassName("yui-overlay","div", calcPanel_c);
    
    for (i=0;i<=elements.length;i++) {
    	elements[i].style.display = "block";
    }*/
    //.yui-panel-container .yui-overlay, div#calcPanel_c.yui-panel-container
    
    YAHOO.calculate.calcPanel.hide();                
}

//YAHOO.util.Event.on(window, 'load', YAHOO.calculate.init, YAHOO.calculate, true);
YAHOO.util.Event.onDOMReady(YAHOO.calculate.init, '', YAHOO.calculate);
