﻿var Valtech = Valtech || {};

var $D = $D || YAHOO.util.Dom;
var $E = $E || YAHOO.util.Event;
var $A = $A || YAHOO.util.Anim;
var $C = $C || YAHOO.util.ColorAnim;
var $M = $M || YAHOO.util.Motion;
var $DD = $DD || YAHOO.util.DD;
/*
Valtech.Slider = function(props){
	if(props){
		this.init(props);
	}
}
Valtech.Slider.prototype.init = function(props){
	if(!props || !props.id || props.min === null || props.max === null ){
		throw "invalidPropertyError";
	}
	this.id  = props.id;
	this.min = props.min;
	this.max = props.max;

	var _keyIncrement = props.keyIncrement || 1; 
	var actualTicks = this.max - this.min;
	this.container = $D.get(this.id);
	if(!this.container){
		throw "invalidElementError: Container \"" + this.id +"\" not present.";
	}
	$D.setStyle(this.container, "visibility", "hidden");
	$D.setStyle(this.container, "position", "absolute");
	this.label = this.container.getElementsByTagName("label")[0];
	this.sliderScale = $D.getElementsByClassName("sliderScale", "div", this.container)[0];
	if(!this.sliderScale){
		throw "invalidElementError: SliderScale not present. Container: " + this.id;
	}
	this.scaleWidth = props.scaleWidth || this.sliderScale.offsetWidth;
	this.sliderThumb = $D.getElementsByClassName("sliderThumb", "div", this.container)[0];
	if(!this.sliderThumb){
		throw "invalidElementError: sliderThumb not present. Container: " + this.id;
	}
	this.sliderValue = $D.getElementsByClassName("sliderValue", "input", this.container)[0];
	if(!this.sliderValue){
		throw "invalidElementError: sliderValue not present. Container: " + this.id;
	}
	
	$E.addListener(this.sliderValue, "blur", this.update, this);
	this.scaleFactor = actualTicks / this.scaleWidth;
	this.keyIncrement = parseInt(_keyIncrement / this.scaleFactor, 10);
	
	
	
	this.slider = YAHOO.widget.Slider.getHorizSlider(this.sliderScale, this.sliderThumb, 0, this.scaleWidth, this.keyIncrement);
	
	this.slider.subscribe("change", this.slide, this);
	if(props.initVal !== null){
		this.slider.setValue(Math.round((props.initVal - this.min)/this.scaleFactor));
	}
	$D.setStyle(this.container, "position", "");
	$D.setStyle(this.container, "visibility", "");
	
}

Valtech.Slider.prototype.slide = function(offsetFromStart, c) {

	var actualValue = c.min + parseInt(offsetFromStart * c.scaleFactor, 10);
	c.sliderValue.value = actualValue;	
	c.sliderScale.title = "Værdi: " + actualValue;
}
Valtech.Slider.prototype.update = function(e, c) {
	var v = parseFloat(c.sliderValue.value, 10);
	if (isNaN(v)) v = 0;
    c.slider.setValue(Math.round((v - c.min)/c.scaleFactor));
	return false;
}
*/
Valtech.Slider = function(props, dataManager){
	if(props && dataManager){
		this.init(props, dataManager);
	}
}
Valtech.Slider.prototype.init = function(props, dataManager){
	//console.log(props.id);
	//console.log(props.fieldName);
	//console.log(props.min);
	//console.log(props.max);
	if(!props || !props.id || !props.fieldName|| props.min === null || props.max === null ){
		throw "invalidPropertyError";
	}
	this.id = props.id;
	this.fieldName = props.fieldName;
	this.min = props.min * 100;
	this.max = props.max * 100;
	this.initVal = props.initVal * 100;
	this.dataManager = dataManager;
	var _keyIncrement = props.keyIncrement ? props.keyIncrement * 100 : 1; 
	var actualTicks = this.max - this.min;
	this.container = $D.get(this.id);

	if(!this.container){
		throw "invalidElementError: Container \"" + this.id +"\" not present.";
	}
	this.label = this.container.getElementsByTagName("label")[0];
	this.sliderScale = $D.getElementsByClassName("sliderScale", "div", this.container)[0];
	if(!this.sliderScale){
		throw "invalidElementError: SliderScale not present. Container: " + this.id;
	}
	this.scaleWidth = props.scaleWidth || this.sliderScale.offsetWidth;
	this.sliderThumb = $D.getElementsByClassName("sliderThumb", "div", this.container)[0];
	if(!this.sliderThumb){
		throw "invalidElementError: sliderThumb not present. Container: " + this.id;
	}
	this.sliderValue = $D.getElementsByClassName("sliderValue", "input", this.container)[0];
	if(!this.sliderValue){
		throw "invalidElementError: sliderValue not present. Container: " + this.id;
	}
	
	this.scaleFactor = actualTicks / this.scaleWidth;
	this.keyIncrement = parseInt(_keyIncrement / this.scaleFactor, 10);
	
	this.slider = YAHOO.widget.Slider.getHorizSlider(this.sliderScale, this.sliderThumb, 0, this.scaleWidth, this.keyIncrement); 
	
    $E.addListener(this.sliderValue, "keypress", this.updateOnEnter, this);   	    
	$E.addListener(this.sliderValue, "keyup", this.update, this);    
	this.slider.subscribe("change", this.slide, this); 
	
	if(props.initVal !== null){
		this.slider.setValue(Math.round((this.initVal - this.min)/this.scaleFactor));
	}
}
/* update slider, when input is written */
Valtech.Slider.prototype.update = function(e, c) {
    
   	var v = Math.round(parseFloat((c.sliderValue.value*100), 10));
    if (isNaN(v)) v = 0;
    c.slider.setValue(Math.round((v - c.min)/c.scaleFactor));
    return false;
}
Valtech.Slider.prototype.updateOnEnter = function(e, c) {
    if (window.event) {
        keynum = e.keyCode
    }
    else if(e.which){
        keynum = e.which
    }
    if (keynum == 13) {
       $E.preventDefault(e);
       Valtech.Slider.prototype.update (e,c)
       c.focus();
    }
    return true;
}
Valtech.Slider.prototype.handle = function(){
    //stub function for handling DataManager events
}
Valtech.Slider.prototype.slide = function(offsetFromStart, c) {
   offsetFromStart = offsetFromStart.toString().replace(/\$|\,/g,'');
    if(isNaN(offsetFromStart)){ 
        offsetFromStart = "0"; 
    }
 	var actualValue = c.min + parseInt(offsetFromStart*c.scaleFactor, 10);
 	if(isNaN(actualValue)){
 	    return;
 	}
	c.sliderValue.value = Math.round(actualValue/100);	
	c.sliderScale.title = "Værdi: " + Math.round(actualValue/100);
	var data = {};
	data[c.fieldName] = { "text" : c.id, "value" : c.sliderValue.value };
	c.dataManager.update(c.id, data);  
};
Valtech.Slider.prototype.formatNumber = function(num){
    num = num.toString().replace(/\$|\,/g,'');
    num = Math.floor(num*100+0.50000000001);
    num = Math.floor(num/100).toString();
    for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++){
        num = num.substring(0,num.length-(4*i+3))+'.'+ num.substring(num.length-(4*i+3));
    }
    return (num );
}