/**
 * A simple DIV decorator - that applies a CSS class - and that's it
 * @constructor
 * @param {string} cssClassName A CSS style name to apply
 * A decorator need only implement a method 'decorate' 
 */
function CssDecorator( cssClassName )
{
	this.cssClassName = cssClassName;
}

/**
 * decorate a given DIV
 * @param div the HTML DIV to decorate
 * @param node the raw data node behind this DIV
 * @param {Rectangle} coords The relative coordinates of this DIV 
 */
CssDecorator.prototype.decorate = function( div, nodeFacade )
{
	div.className = this.cssClassName;
};
// ============================================================================

/**
 * The default DIV decorator - applies something like a useful style to a given DIV
 * @constructor
 * @param {string} cssClassName A CSS style name to apply
 */
function DefaultDecorator()
{
	//LOW: figure out how to create a CSS class at runtime & apply that instead
}

DefaultDecorator.prototype = new CssDecorator();

/**
 * decorate a given DIV
 * @param div the HTML DIV to decorate
 * @param node the raw data node behind this DIV
 * @param {Rectangle} coords The relative coordinates of this DIV 
 */

DefaultDecorator.prototype.mouseover = function(div, nodeFacade) {
	var style = div.style;
	style.borderWidth = "1px";
	style.borderStyle = "solid";
	style.borderColor = "#fff";
};
DefaultDecorator.prototype.mouseout = function(div, nodeFacade) {
	var style = div.style;
	style.borderWidth = "0px";
	style.borderStyle = "solid";
	style.borderColor = nodeFacade.colorThree();
};

var pad = function(num, totalChars) {
    var pad = '0';
    num = num + '';
    while (num.length < totalChars) {
        num = pad + num;
    }
    return num;
};

// Ratio is between 0 and 1
var changeColor = function(color, ratio, darker) {
	
    var difference = Math.round(ratio * 255) * (darker ? -1 : 1),
        decimal    = color.replace(
            /^#?([a-z0-9][a-z0-9])([a-z0-9][a-z0-9])([a-z0-9][a-z0-9])/i,
            function() {
                return parseInt(arguments[1], 16) + ',' +
                    parseInt(arguments[2], 16) + ',' +
                    parseInt(arguments[3], 16);
            }
        ).split(/,/);
    //alert(difference);
    //alert(parseInt(decimal[1],10));
    return [
        '#',
        pad(Math.max(parseInt(decimal[0], 10) + difference, 0).toString(16), 2),
        pad(Math.max(parseInt(decimal[1], 10) + difference, 0).toString(16), 2),
        pad(Math.max(parseInt(decimal[2], 10) + difference, 0).toString(16), 2)
    ].join('');
};
var lighterColor = function(color, ratio) {
    return changeColor(color, ratio, false);
};
var darkerColor = function(color, ratio) {
    return changeColor(color, ratio, true);
};

//var darker = darkerColor('#224e0f', .2);
//var lighter = lighterColor('#404040', .2);

//alert(darker);

DefaultDecorator.prototype.decorate = function( div, nodeFacade, parentNode )
{
	if (!parentNode) {
	var style = div.style;
	//alert(nodeFacade.colorThree());
	//alert(darkerColor(nodeFacade.colorThree().toString(), .2));
	//style.borderColor = "#fff";
	style.cursor = "pointer";
	var then = nodeFacade.getDate();
	var category = nodeFacade.getCategory();
	var hour_server = nodeFacade.hour();
	var minutes_server = nodeFacade.minute();
	var day_server = nodeFacade.day();
	var month_server = nodeFacade.month();
	var year_server = nodeFacade.year();
	var now = new Date();
	now.setHours(hour_server);
	now.setMinutes(minutes_server);
	now.setDate(day_server);
	now.setMonth(month_server - 1);
	now.setYear(year_server);
	var colorOne = nodeFacade.colorOne();
	var colorTwo = nodeFacade.colorTwo();
	var colorThree = nodeFacade.colorThree();
	
	var year = then.slice(0,4);
	var month = then.slice(5,7);
	var day = then.slice(8,10);
	var hour = then.slice(11,13);
	var minute = then.slice(14,16);
	var second = "00";
	
	var thenDate = new Date();
	thenDate.setFullYear(year, (month - 1), day, hour, minute, 0, 0);
	thenDate.setHours(hour, minute, 0, 0);
	
	var date1 = new Date();
	date1.setTime(now.getTime());
	var date2 = new Date();
	date2.setTime(thenDate.getTime());
	var diff = new Date();

	diff.setTime(Math.abs(date1.getTime() - date2.getTime()));

	var timediff = diff.getTime();

	var mins = Math.floor(timediff/60000);
	var hours = Math.floor(timediff/3600000);
	
	//var color;
	
	var color = colorOne;
	
	/*if (mins  <= nodeFacade.timeOne()) {
		style.background = colorOne;
		color = colorOne;
		style.borderColor = darkerColor(color, .1);
	}
	else if (mins <= nodeFacade.timeTwo()) {
		style.background = colorTwo;
		color = colorTwo;
		style.borderColor = darkerColor(color, .1);
	}
	else { style.background = colorThree;
		color = colorThree;
		style.borderColor = darkerColor(color, .08);
	}*/
	
	if (mins < 60) {
		color = darkerColor(color, .02 * mins/10);
		style.background = color;
		style.borderColor = darkerColor(color, .1);
	}
	else if (mins < 400){
		color = darkerColor(color, .035 * mins/60);
		style.background = color;
		style.borderColor = darkerColor(color, .1);

	}
	else {
		if (mins > 1800) mins = 1800;
		color = darkerColor(color, .045 * (mins/180));
		style.background = color;
		style.borderColor = darkerColor(color, .1);
	}
	
	
	style.borderWidth = "1px";
	style.borderStyle = "solid";
	
	}
};

