"use strict";
/*
Made By Peter Lindblom 2009
http://www.plweb.se
ver 1.019
*/
var se = {};
se.plweb = {};
se.plweb.binaryTime = function (inputSettings) {
var settings = {}, // settings used
defaultSettings = {
color: { on: "#FF0000", off: "#CCC", inactive: "transparent" },
binaryTimeDivId: "binaryTimeDiv"
}, // if a setting not defined use default
binaryTimeDivObj = null, // divObj
binaryTimeDivObjChildNodesDivObjs = [], //array of div obj
localDocumentObj = document,
timer = undefined,
calculateSize = function (binaryTimeDivId) {
var width = 0, height = 0;
binaryTimeDivObj = localDocumentObj.getElementById(binaryTimeDivId);
if (binaryTimeDivObj === null) {
alert("binaryTime Error: could not find
");
return false;
}
width = parseInt(binaryTimeDivObj.style.width, 10);
height = parseInt(binaryTimeDivObj.style.height, 10);
if ((width % 6) === 0 && (height % 4) === 0 && height !== 0 && width !== 0) {
settings.childNodeWidth = width / 6;
settings.childNodeHeight = height / 4;
return true;
}
alert("binaryTime Error:
width should be even devideable by 6 and not 0 and height should be even devideable by 4 and not 0");
return false;
},
useDefaultSettings = function (inputSettings) {
if (inputSettings === undefined) {
settings = defaultSettings;
} else {
settings = inputSettings;
if (inputSettings.color === undefined) {
settings.color = defaultSettings.color;
}
if (inputSettings.color.on === undefined) {
settings.color.on = defaultSettings.color.on;
}
if (inputSettings.color.off === undefined) {
settings.color.off = defaultSettings.color.off;
}
if (inputSettings.color.inactive === undefined) {
settings.color.inactive = defaultSettings.color.inactive;
}
if (inputSettings.binaryTimeDivId === undefined) {
settings.binaryTimeDivId = defaultSettings.binaryTimeDivId;
}
}
},
changeCellColor = function (color, col, row) {
var pos = (row * 7) + col; // a row has 6 cells and a empty div clear:both;
binaryTimeDivObjChildNodesDivObjs[pos].style.backgroundColor = color;
},
updateColumn = function (value, col) {
if (value % 2 !== 0) {
changeCellColor(settings.color.on, col, 3);
} else {
changeCellColor(settings.color.off, col, 3);
}
if (value === 2 || value === 3 || value === 6 || value === 7) {
changeCellColor(settings.color.on, col, 2);
} else {
changeCellColor(settings.color.off, col, 2);
}
if (value === 4 || value === 5 || value === 6 || value === 7) {
changeCellColor(settings.color.on, col, 1);
} else if (col !== 0) {
changeCellColor(settings.color.off, col, 1);
}
if (value === 8 || value === 9) {
changeCellColor(settings.color.on, col, 0);
} else if (col === 1 || col === 3 || col === 5) {
changeCellColor(settings.color.off, col, 0);
}
},
convertHTMLCollection2ArrayOfHTMLDivElements = function (HTMLCollectionObj) {
var htmlCollectionLength = 0, nodePos = 0, arrayOfHTMLDivElementObjs = [];
try {
arrayOfHTMLDivElementObjs = Array.prototype.slice.call(HTMLCollectionObj);
} catch (err) {
for (htmlCollectionLength = HTMLCollectionObj.length; nodePos < htmlCollectionLength; nodePos = nodePos + 1) {
arrayOfHTMLDivElementObjs.push(HTMLCollectionObj[nodePos]);
}
}
return arrayOfHTMLDivElementObjs;
},
updateTime = function () {
var now = new Date();
updateColumn(Math.floor(now.getHours() / 10), 0);
updateColumn(now.getHours() % 10, 1);
updateColumn(Math.floor(now.getMinutes() / 10), 2);
updateColumn(now.getMinutes() % 10, 3);
updateColumn(Math.floor(now.getSeconds() / 10), 4);
updateColumn(now.getSeconds() % 10, 5);
timer = setTimeout(updateTime, 1000);
},
start = function (inputSettings) {
var width = 6, height = 4, x = 0, y = 0, tempDiv, tempDivClear;
useDefaultSettings(inputSettings);
if (calculateSize(settings.binaryTimeDivId)) {
while (x < width && y < height) {
tempDiv = localDocumentObj.createElement("div");
tempDiv.style.width = settings.childNodeWidth + "px";
tempDiv.style.height = settings.childNodeHeight + "px";
if (localDocumentObj.all) {
tempDiv.style.styleFloat = "left"; //ie.....
} else {
tempDiv.style.cssFloat = "left";
}
tempDiv.style.margin = "0px";
tempDiv.style.padding = "0px";
binaryTimeDivObj.appendChild(tempDiv);
if (x === 5) {
x = 0;
y = y + 1;
tempDivClear = localDocumentObj.createElement("div");
tempDivClear.style.clear = "both";
tempDivClear.style.margin = "0px";
tempDivClear.style.padding = "0px";
binaryTimeDivObj.appendChild(tempDivClear);
} else {
x = x + 1;
}
}
binaryTimeDivObjChildNodesDivObjs = convertHTMLCollection2ArrayOfHTMLDivElements(binaryTimeDivObj.getElementsByTagName("div"));
changeCellColor(settings.color.inactive, 0, 0);
changeCellColor(settings.color.inactive, 0, 1);
changeCellColor(settings.color.inactive, 2, 0);
changeCellColor(settings.color.inactive, 4, 0);
updateTime();
}
};
start(inputSettings);
};