// utils.js.  Version 0.1.2.
// CyDot, 01/18/2010 23:55:09.
// Utils --------------------------------------------
function getTrg (id) {
if (!document.getElementById) var obj =document.all.id;
else var obj =document.getElementById(id);
return obj;
}
// physioTec scripts.
// screen.js,   Version 0.2.
// CyDot, 2009-12-20.
// Window size-----------------------------------------
var wDef =1090;
var hDef =800;
var wFull =screen.availWidth;
var hFull =screen.availHeight;
var sizeStatus ="INIT";
var cName ="screen";
var cHours =2;
function initResize () {
setCookie(cName, "FULL", cHours);
resize();
}
function resize () {
var sizeStatus =readCookie(cName);		// alert("sizeStatus: "+sizeStatus);
switch (sizeStatus) {
case "DEFAULT" : setFullSize(); break;
case "CHANGED" : setDefaultSize(); break;
case "FULL" : setDefaultSize(); break;
default : setDefaultSize();
}
}
window.onresize =function() {
setCookie(cName, "CHANGED", cHours);
getTrg("screenSize").firstChild.nodeValue = "DEFAULT SCREEN";
}
function setDefaultSize () {
window.resizeTo(wDef, hDef);
window.moveTo((wFull -wDef) /2, 0);
setCookie(cName, "DEFAULT", cHours);
getTrg("screenSize").firstChild.nodeValue = "FULL SCREEN";
}
function setFullSize () {
window.moveTo(1, 1);
window.resizeTo(wFull, hFull);
setCookie(cName, "FULL", cHours);
getTrg("screenSize").firstChild.nodeValue = "DEFAULT SCREEN";
}
// Cookie ------------------------------------------------
function setCookie(name, value, hours) {
if (hours) {
var date = new Date();
date.setTime(date.getTime()+(hours *60 *60 *1000));
var expires = "; expires=" +date.toGMTString();
}
else var expires ="";
document.cookie =name +"=" +value +expires +"; path=/";
}
function readCookie(name) {
var nameEQ = name + "=";
var ca = document.cookie.split(';');
for(var i=0;i < ca.length;i++) {
var c = ca[i];
while (c.charAt(0)==' ') c = c.substring(1, c.length);
if (c.indexOf(nameEQ)==0) return c.substring(nameEQ.length, c.length);
}
return null;
}
function eraseCookie (name) {
setCookie(name, "", -1);
}
// --------------------------------------------------------
// storage.js.
// CyDot, 2009-12-20.
var Storage = new function () {
/* --------- Private Properties --------- */
var dataContainer = {};
/* --------- Private Methods --------- */
function linearize () {
var string = "", name, value;
for (name in dataContainer) {
name = encodeURIComponent(name);
value = encodeURIComponent(dataContainer[name]);
string += name + "=" + value + "&";
}
if (string != "") {
string = string.substring(0, string.length - 1);
}
return string;
}
function read () {
if (window.name == '' || window.name.indexOf("=") == -1) {
return;
}
var pairs = window.name.split("&");
var pair, name, value;
for (var i = 0; i < pairs.length; i++) {
if (pairs[i] == "") {
continue;
}
pair = pairs[i].split("=");
name = decodeURIComponent(pair[0]);
value = decodeURIComponent(pair[1]);
dataContainer[name] = value;
}
}
function write () {
window.name = linearize();
}
/* --------- Public Methods --------- */
this.set = function (name, value) {
dataContainer[name] = value;
write();
};
this.get = function (name) {
var returnValue = dataContainer[name];
return returnValue;
};
this.getAll = function () {
return dataContainer;
};
this.remove = function (name) {
if (typeof(dataContainer[name]) != undefined) {
delete dataContainer[name];
}
write();
};
this.removeAll = function () {
dataContainer = {};
write();
};
/* --------- Construction --------- */
read();
};
// Inspired by	Matthew Foster.
// EventDispatcher.js.  Version 0.1.0.
// CyDot, 2010-01-18.
function EventDispatcher () {
this.listenerChain ={};
this.addEventListener =function (type, listener) {
if (!this.listenerChain[type]) this.listenerChain[type] =[listener];
else if (this.isEventListener(type, listener)===null) 
this.listenerChain[type].push(listener);
}
this.hasEventListener =function (type) {
return (typeof this.listenerChain[type] !="undefined");
}
this.isEventListener =function (type, listener) {
var typeArr =this.listenerChain[type];
for (var i = 0; i < typeArr.length; i++) {
if (typeArr[i]==listener) return i;
}
return null;
}
this.removeEventListener =function (type, listener) {
if (!this.hasEventListener(type)) return false;
var ind =this.isEventListener(type, listener);
if (ind!==null) this.listenerChain.splice(i, 1);
}
this.dispatchEvent =function (eObj) {
if (!this.hasEventListener(eObj.type)) return false;
var typeArr =this.listenerChain[eObj.type];
for (var i = 0; i < typeArr.length; i++) {
typeArr[i][eObj.type](eObj);
}
}
}
// tween.js.  Version 0.1.2.
// CyDot, 2010-01-15.
/* Constructor.
* @ param id: html object id.
* @ param sec: transition time in seconds.
* @ param from: start point in px.
* @ param to: end point in px.
* @ param axis: string "x" or "y" axis.
* @ param ease: array. Ind 0 = ease type, ind 1 = ease method, ind 2 = ease amplification. 
* Usage: ["expo", "IN", 3].
*/
function PositionTween (id, sec, from, to, axis, ease, listener) {
// Public props ------------
this.id =id;
this.obj;
this.from =from;
this.to =to;
this.eType =ease[0];
this.eMethod =ease[1];
this.eAmp =ease[2];
this.dir =1;
this.eD =new EventDispatcher();
this.listener =listener;
// Private props ------------
var inst =this;
var frameTime =33;
var timerID;
var duration =sec *1000;
var frames;
var frame =0;
var active =false;
var dist =inst.to -inst.from;
var axis =(axis=="x") ? "left" : "top";
// Public functions ------------
this.registerObj =registerObj;
this.adjustTime =adjustTime;
this.getFrames =getFrames;
this.goFrm =goFrm;
this.initMove =initMove;
this.stopp =stopp;
this.changeDir =changeDir;
// Initial -------------------
adjustTime();
function adjustTime () {
frames =Math.floor(duration /frameTime);
duration =frames *frameTime;
}
// Public functions --------------
this.onTweenStop =function (eO) {
alert("pos: "+eO.pos);
}
function getFrames () {
return frames;
}
function goFrm (frame) {
inst.frame =frame;
setCurrPos();
}
function initMove () {
inst.eD.addEventListener("onTweenStop", inst.listener);
active =true;
move();
}
function stopp () {
active =false;
delete timerID;
inst.eD.dispatchEvent({type: "onTweenStop", pos: frame /frames});
inst.eD.removeEventListener("onTweenStop", inst.listener);
}
function changeDir () {
if (inst.dir==1 && frame==0) return;
else if (inst.dir==-1 && frame==frames) return;
inst.dir *=-1;
}
function registerObj () {
inst.obj =document.getElementById(inst.id);
}
// Private functions -------
function nextFrm () {
frame +=inst.dir;
setCurrPos();
}
function setCurrPos () {
inst.obj.style[axis] =Math.round(getCurrPos()) +"px";
}
function getCurrPos () {
var val =Ease[inst.eType](frame /frames, inst.eMethod, inst.eAmp);
return inst.from +dist *val;
}
function move () {
if (!active) return;
if (inst.dir==1 && frame==frames) {stopp(); return}
else if (inst.dir==-1 && frame==0) {stopp(); return}
nextFrm();
timerID =setTimeout(function () {move();}, frameTime);
}
}
// Prototyping -----------------------------------------------
PositionTween.prototype.play =function () {
if (!this.obj) this.registerObj();
this.initMove();
}
PositionTween.prototype.stop =function () {
this.stopp();
}
PositionTween.prototype.reverse =function () {
this.changeDir();
}
PositionTween.prototype.goTo =function (frame) {
if (frame===null) frame =this.getFrames();
this.goFrm(frame);
}
// End class PositionTween ------------------------------------
// Static Class Ease --------------------------------------------
var Ease = new function () {
// Public ease methods -----------
this.expo =function (t, a) {
a =a || 2;
return Math.pow(t, a);
}
this.sine =function (t) {
return Math.cos(Math.PI + Math.PI/2 *t) +1;
}
this.linear =function (t) {
return t;
}
// Public ease types -----------
this.IN =function (t, method, a) {
return this[method](t, a);
}
this.OUT =function (t, method, a) {
t =1 -t;
return 1 -this[method](t, a);
}
this.IN_OUT =function (t, method, a) {
return this.easeComb(t, method, "IN", "OUT", a);
}
this.OUT_IN =function (t, method, a) {
return this.easeComb(t, method, "OUT", "IN", a);
}
// Private ease types -----------
this.easeComb =function (t, method, type1, type2, a) {
var t1 =0.5;
var t2 =1 -t1;
if (t < t1) {
t /=t1;
return this[type1](t, method, a) /2;
} 
else {
t =(t -t1) /t2;
return this[type2](t, method, a) /2 +t1;
}
}
}
// End Ease ----------------------------------------------------------
