/*
 * Copyright (C) 2010 Marek Będkowski visit: http://slidemenus.bedkowski.pl
 *
 * This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */
function startMatrixClock(matrixClockDivId, dir){
	var m=document.getElementById(matrixClockDivId);

	// check hight of one letter
	var mm=document.createElement('div')
	mm.appendChild(document.createTextNode('1'));
	m.appendChild(mm)
	var letterHeight=mm.offsetHeight
	var letterWidth=mm.offsetWidth
	m.removeChild(mm)
	$(m).css('height', letterHeight+'px')
	dir = dir||1
	function div(layerId,left,max, action, N) {
		var div=document.createElement("div");
		div.id=layerId;
		var jDiv=$(div).moveToGroup(layerId, AL_MODE.LOOP).css('left', left+'px')
		var initialPos = dir>0?0:-max*letterHeight;
		var frames=[];
		for(var i=0,j=dir<0?max:0; i<=max; ++i,j+=dir) {
			div.appendChild(document.createTextNode(j+' '));
			frames.push({layerId:layerId+'_'+j, num:j, props:{top: initialPos+(letterHeight*-j*dir)}});
		}
		frames.sort(function(a,b){
			return a.num - b.num;
		});
		function eff(d) {
			return d < 0 ? easeInBounce : easeInBack;
		}
		jDiv.addFrames(frames[frames.length-1].props, 1)
		for(var i=0;i<frames.length;i++) {
			var frame=frames[i];
			jDiv.addFrames(frame.props, !i ? 150 : 100, !i ? eff(-dir) : eff(dir), frame.layerId);
			if(action != N) {
				jDiv.addFrames({action: action}, 1)
			}
		}
		return div;
	}

	var startPos = 5;
	var stop= function(g){g.stop();};
	m.appendChild(div('hours1', startPos, 2, stop));
	m.appendChild(div('hours2', startPos+=8, 9, stop));

	m.appendChild(div('minutes1', startPos+=13, 5, stop));
	m.appendChild(div('minutes2', startPos+=8, 9, stop));

	m.appendChild(div('seconds1', startPos+=13, 5, stop));
	m.appendChild(div('seconds2', startPos+=8, 9, stop));

	var curFrame={};
	function gs(group,frame){if(!curFrame[group]||curFrame[group]!=frame)$.Al(group).gotoAndPlay(frame);curFrame[group]=frame;}
	(function(m){
		m=new Date().toString().match(/(\d)(\d):(\d)(\d):(\d)(\d)/);
		gs('hours1', 'hours1_'+m[1]);
		gs('hours2', 'hours2_'+m[2]);

		gs('minutes1', 'minutes1_'+m[3]);
		gs('minutes2', 'minutes2_'+m[4]);

		gs('seconds1', 'seconds1_'+m[5]);
		gs('seconds2', 'seconds2_'+m[6]);

		$('#'+matrixClockDivId+' p')[0].innerHTML=m[0]

		setTimeout(arguments.callee, 1000);
	})();
}

