﻿var Calc = new Class({
	initialize : function(x,y) {
		this.Root = $$('body') ? $$('body').getLast() : $$('BODY').getLast();
		this.CalcActive = true;
		var Mode = 'CalcSkk';
		var Result = 'CalcEur';
		conversion = 30.126;
		var backColor = '#FFF6EB';//'#B9DCFF';
		var frontColor = '#FF7F50';//'#7EBBF8';
		var textColor = '#FFFEF2';
		
		document.ondragstart = function() {return false;};
		
		var add_num_click_to_display = function(event) {
			var name = $(event.target).get('id').toString();
			var number = name.substr(name.length - 1);
			switch(Mode) {
				case 'CalcEur' :
					var newNumber = (CalcEur.get('value') != 0) ? CalcEur.get('value') + '' + number : number;
					CalcEur.setProperty('value',newNumber);
					CalcSkk.setProperty('value',Math.round(newNumber * conversion * 100) / 100);
				break;
				case 'CalcSkk' :
					var newNumber = (CalcSkk.get('value') != 0) ? CalcSkk.get('value') + '' + number : number;
					CalcSkk.setProperty('value',newNumber);
					CalcEur.setProperty('value',Math.round(newNumber / conversion * 100) / 100);
				break;
			}
		}
		
		var add_num_to_display = function(event) {
			var target = $(event.target).get('id').toString();
			value = $(event.target).get('value').toString();

		var mynum = /^\d+[,\.]?\d{0,2}$/
		while ((value.search(mynum) == -1) && (value != '')) {
			value = value.substr(0,value.length - 1);
		}


			search_val  = /[,]/
			if((pos = value.search(search_val)) !== -1) {
				if(pos == value.length - 1) {
					value = value.substr(0,pos);
					if(value.indexOf('.') == -1) {
						value = value + '.';
					}
				} else {
					value = value.substr(pos + 1,value.length - 1);
					if(value.indexOf('.') == -1) {
						value = '.' + value;
					}
				}
				
			}

			
			if(isNaN(value)) {
				value = '0';
			}
$(event.target).setProperty('value',value);
		
			switch(target) {
				case 'CalcEur' :
					CalcSkk.setProperty('value',Math.round(value * conversion * 100) / 100);
				break;
				case 'CalcSkk' :
					CalcEur.setProperty('value',Math.round(value / conversion * 100) / 100);
				break;
			}
		}
		
		var add_dot_to_display = function(event) {
			if($(Mode).get('value').toString().indexOf('.') == -1) {
				$(Mode).setProperty('value',$(Mode).get('value').toString() + '.');
			}
		}
		
		var change_mode = function(event) {
			var text = $(event.target).get('html');
			switch(text) {
				case '€/Sk' :
					text = 'Sk/€';
				break;
				case 'Sk/€' :
					text = '€/Sk';
				break;
			}
			$(event.target).setProperty('html',text);
			CalcEur.setProperty('value','0');
			CalcSkk.setProperty('value','0');
			temp = Mode;
			Mode = Result;
			Result = temp;
		}
		
		this.imHere = function() {
			var myTween = new Fx.Tween(this.CalcDiv,{duration: 200,property: 'background-color'})
			myTween.start('#FB65B6').chain(
				function(){this.start(backColor);}
			);
		}
		
		var CalcShadow = new Element('div',{
			'id' : 'CalcShadow',
			'styles' : {
				'background' : 'url(media/images/shadow.png) left top no-repeat',
				'width' : '260px',
				'height' : '310px',
				'position' : 'absolute',
				'top' : '-300px',
				'left' : '-3px'
			}
		});
		
		var CalcCurrencyRate = new Element('div',{
			'html' : 'Konverzný kurz: 1€ = 30.1260 Sk',
			'styles' : {
				'float' : 'left',
				'width' : '180px',
				'margin-top' : '5px',
				'margin-left' : '2px',
				'font-size' : '9px',
				'color' : frontColor			}
		});
				
		this.CalcDiv = new Element('div',{
			'id' : 'CalcDiv',
			'styles' : {
				'display' : 'block',
				'width' : '200px',
				'height' : '260px',
				'background' : backColor,
				'position' : 'absolute',
				'left' : x,
				'top' : y,
				'font-family' : 'Verdana',
				'font-size' : '12px',
				'border' : '3px solid ' + frontColor,
				'cursor' : 'auto'
			}
		});
					
		var CalcTop = new Element('div',{
			'id' : 'CalcTop',
			'styles' : {
				'position' : 'relative',
				'cursor' : 'move',
				'width' : '200px',
				'height' : '20px',
				'background' : frontColor,
				'font-weight' : 'bold'
			},
			'events' : {
				'mousedown' : function(event) {
					var coords = this.getCoordinates();
					var distX = event.page.x - coords.left;
					var distY = event.page.y - coords.top;
					
					document.addEvent('mousemove', moveListener = function(event) {
						this.setStyles({left : event.page.x - distX, top : event.page.y - distY});
					}.bind(this));
				}.bind(this.CalcDiv),
				'mouseup' : function() {
					document.removeEvents();
				}
			}					
		});
		
		var CalcHeading = new Element('div',{
			'id' : 'CalcHeading',
			'html' : 'Eurokalkulačka',
			'styles' : {
				'float' : 'left',
				'color' : textColor
			}
		});
		
		var CalcClose = new Element('div',{
			'html' : 'x',
			'id' : 'CalcClose',
			'styles' : {
				'float' : 'right',
				'width' : '15px',
				'text-align' : 'right',
				'color' : textColor,
				'cursor' : 'pointer'
			},
			'events' : {
				'click' : function(event) {
					this.CalcActive = false;
					this.CalcDiv.destroy();
				}.bind(this)
			}
		});
		
		var CalcDisplay = new Element('div',{
			'id' : 'CalcDisplay',
			'styles' : {
				'width' : '180px',
				'margin-left' : '10px',
				'margin-top' : '5px'
			}
		});
		
		var CalcCurrencyEur = new Element('div',{
			'id' : 'CalcCurrencyEur',
			'html' : '€',
			'styles' : {
				'float' : 'right',
				'text-align' : 'right',
				'color' : frontColor,
				'width' : '20px',
				'margin-top' : '5px'
			}
		});
		
		var CalcCurrencySkk = new Element('div',{
			'id' : 'CalcCurrencySkk',
			'html' : 'Sk',
			'styles' : {
				'float' : 'right',
				'text-align' : 'right',
				'color' : frontColor,
				'width' : '20px',
				'margin-top' : '5px'
			}
		});
		
		var CalcEur = new Element('input',{
			'id' : 'CalcEur',
			'value' : '0',
			'styles' : {
				'float' : 'left',
				'width' : '150px',
				'height' : '14px',
				'background' : textColor,
				'text-align' : 'right',
				'border' : '1px solid ' + frontColor,
				'overflow' : 'hidden',
				'color' : '#000',
				'margin-top' : '5px'
			},
			'events' : {
				'keyup' : add_num_to_display.bind(this),
				'focus' : function(event) {
					if($(event.target).get('value').toString() == '0') {
						$($(event.target)).setProperty('value','');
					}
				}
			}
		});
		
		var CalcSkk = new Element('input',{
			'id' : 'CalcSkk',
			'value' : '0',
			'styles' : {
				'float' : 'left',
				'width' : '150px',
				'height' : '14px',
				'background' : textColor,
				'text-align' : 'right',
				'border' : '1px solid ' + frontColor,
				'overflow' : 'hidden',
				'color' : '#000',
				'margin-top' : '5px'
			},
			'events' : {
				'keyup' : add_num_to_display.bind(this),
				'focus' : function(event) {
					if($(event.target).get('value').toString() == '0') {
						$($(event.target)).setProperty('value','');
					}
				}
			}
		});
		
		var marginLeft = navigator.appName == 'Microsoft Internet Explorer' ? '5px' : '10px';
		
		var CalcKeypad = new Element('div',{
			'id' : 'CalcKeypad',
			'styles' : {
				'float' : 'left',
				'margin-top' : '10px',
				'margin-left' : marginLeft,
				'width' : '130px'
			}
		});
		
		var CalcButtons = new Array();
		
		for(var i = 0;i < 10;i++) {
			var clear = i%3 == 0 ? 'left' : 'none';
			var text = ((i+1)%10).toString();
			CalcButtons[i] = new Element('a',{
				'id' : 'CalcButton_' + text,
				'html' : text,
				'styles' : {
					'float' : 'left',
					'text-decoration' : 'none',
					'clear' : clear,
					'display' : 'block',
					'width' : '30px',
					'height' : '30px',
					'text-align' : 'center',
					'line-height' : '30px',
					'margin-top' : '10px',
					'margin-right' : '10px',
					'font-weight' : 'bold',
					'background' : frontColor,
					'cursor' : 'pointer',
					'color' : textColor
				},
				'events' : {
					'click' : add_num_click_to_display.bind(this)
				}
			});
		}
		var buttonStyles = CalcButtons[2].getStyles('float','display','width','height','text-align','line-height', 'margin-top',
													'margin-right','font-weight','background','cursor','color','text-decoration');
		
		var CalcDot = new Element('a',{
			'id' : 'CalcDot',
			'html' : '.',
			'styles' : buttonStyles,
			'events' : {
				'click' : add_dot_to_display
			}
		});
		
		var CalcCancel = new Element('a',{
			'id' : 'CalcCancel',
			'html' : 'C',
			'styles' : buttonStyles,
			'events' : {
				'click' : function() {
					CalcEur.setProperty('value','0');
					CalcSkk.setProperty('value','0');
				}
			}
		});
		
		buttonStyles.float = 'right';
		buttonStyles.width = '39px';
		buttonStyles.marginRight = navigator.appName == 'Microsoft Internet Explorer' ? '5px' : '10px';
	
		var CalcSwitch = new Element('div',{
			'id' : 'CalcSwitch',
			'styles' : {
				'float' : 'right',
				'width' : '30px',
				'margin-top' : '10px'
			}
		});
		
		var CalcEurSkk = new Element('a',{
			'id' : 'CalcEurSkk',
			'html' : 'Sk/€',
			'styles' : buttonStyles,
			'events' : {
				'click' : change_mode
			}
		});
		
		
		//------------- title bar -----------
		CalcShadow.inject(this.CalcDiv.inject(this.Root));
		CalcTop.inject(this.CalcDiv);
		CalcHeading.inject(CalcTop);
		CalcClose.inject(CalcTop);
		//-------------- display -------------
		CalcDisplay.inject(this.CalcDiv);
		CalcCurrencySkk.inject(CalcDisplay);
		CalcSkk.inject(CalcDisplay);
		CalcCurrencyEur.inject(CalcDisplay);
		CalcEur.inject(CalcDisplay);
		//-------------- keypad ---------------
		CalcKeypad.inject(this.CalcDiv);
		CalcButtons.each(function(item) {
			item.inject(CalcKeypad);
		});
		CalcDot.inject(CalcKeypad);
		CalcCancel.inject(CalcKeypad);
		//-------------- currency switch ------
		CalcSwitch.inject(this.CalcDiv);
		CalcEurSkk.inject(CalcSwitch);
		//-------------- currency info ---------
		CalcCurrencyRate.inject(this.CalcDiv);
	}
});

window.addEvent('domready', function() {
	var myCalc;
	$('CalcStarter').addEvent('click', function(evt) {
		var myEvent = new Event(evt).stop();
		var CalcActive;
		if(myCalc) {
			CalcActive = myCalc.CalcActive ? true : false;
		}
		if(CalcActive) {
			myCalc.imHere();
		} else {
			myCalc = new Calc(evt.page.x,evt.page.y);
		}
	});
});

