/*
 * jquery-counter plugin ported to Logi by M.Kostadinov.
 *
 * Copyright (c) 2009 Martin Conte Mac Donell <Reflejo@gmail.com>
 * Dual licensed under the MIT and GPL licenses.
 * http://docs.jquery.com/License
 */
Logi.countdown = function (parent, options) {
    // Default options
    var defaultOptions = {
	    stepTime: 60,
	    // startTime and format MUST follow the same format.
	    // also you cannot specify a format unordered (e.g. hh:ss:mm is wrong)
	    format: "dd:hh:mm:ss",
	    startTime: "01:12:32:55",
	    digitImages: 6,
	    digitWidth: 53,
	    digitHeight: 77,
	    timerEnd: function(){},
	    image: "web/img/logi/digits.png"
    };
    options = options || {};
    for (var o in defaultOptions) {
    	options[o] = options[o] || defaultOptions[o]; 
    }
    var digits = [], interval;
    
    // Draw digits in given container
    var createDigits = function(where) {
    	var c = 0;
    	var ww = 0;
    	// Iterate each startTime digit, if it is not a digit
    	// we'll asume that it's a separator
    	for (var i = 0; i < options.startTime.length; i++) {
    		if (parseInt(options.startTime.charAt (i)) >= 0) {
    			var elem = Logi.DOM.Element.create ('div', {id : 'cnt_' + i, className : "cntDigit" });
    			elem.style.height = (options.digitHeight * options.digitImages * 10) + 'px'; 
    			elem.style.cssFloat = 'left';
    			elem.style.background = 'url(\'' + options.image + '\') 0 -1px';
    			elem.style.width = (options.digitWidth) + 'px';
    			elem.style.left = (ww) + 'px';
    			ww += options.digitWidth;
    			digits.push (elem);
    			margin(c, -((parseInt(options.startTime.charAt (i)) * options.digitHeight * options.digitImages)));
    			digits[c].__max = 9;
    			// Add max digits, for example, first digit of minutes (mm) has 
    			// a max of 5. Conditional max is used when the left digit has reach
    			// the max. For example second "hours" digit has a conditional max of 4 
    			switch (options.format.charAt (i)) {
    				case 'h':
    					digits[c].__max = (c % 2 == 0) ? 2: 9;
    					if (c % 2 == 0) {
    						digits[c].__condmax = 4;
    					}
    					break;
    				case 'd': 
    					digits[c].__max = 9;
    					break;
    				case 'm':
    				case 's':
    					digits[c].__max = (c % 2 == 0) ? 5: 9;
    			}
    			++c;
    		} else {
    			var elem = Logi.DOM.Element.create ('div', {className : "cntSeparator" });
    			elem.style.cssFloat = 'left';
    			elem.innerHTML = options.startTime.charAt (i);
    			ww += 10;
    		}
    		where.appendChild(elem);
    	}
    };
  
    // Set or get element margin
  	var margin = function(elem, val) {
  		if (val !== undefined) {
  			window.q = window.q || []; 
  			window.q.push ([elem, val]);
  			return digits[elem].style.marginTop = val + 'px';
  		}
  		return parseInt (digits[elem].style.marginTop.replace('px', ''));
  	};

    // Makes the movement. This is done by "digitImages" steps.
    var moveStep = function(elem) {
    	digits[elem]._digitInitial = -(digits[elem].__max * options.digitHeight * options.digitImages);
    	return function _move() {
    		var mtop = margin (elem) + options.digitHeight;
    		//Logi.log (elem, mtop, digits[elem]);
    		if (mtop == options.digitHeight) {
    			margin(elem, digits[elem]._digitInitial);
    			
    			if (elem > 0) {
    				moveStep(elem - 1)();
    			} else  {
	    			clearInterval(interval);
	    			for (var i=0; i < digits.length; i++) margin(i, 0);
	    			options.timerEnd();
	    			return;
	    		}
	    		if ((elem > 0) && (digits[elem].__condmax !== undefined) && 
	    				(digits[elem - 1]._digitInitial == margin(elem - 1))) {
	    			margin(elem, -(digits[elem].__condmax * options.digitHeight * options.digitImages));
	    		}
	    		return;
    		}
    		margin(elem, mtop);
    		if (margin(elem) / options.digitHeight % options.digitImages != 0) {
    			setTimeout(_move, options.stepTime);
    		}
    		if (mtop == 0) digits[elem].__ismax = true;
    	};
    };
    //$.extend(options, options);
  	//this.css({height: options.digitHeight, overflow: 'hidden'});
  	createDigits (parent, options);
  	interval = setInterval (moveStep (digits.length - 1), 1000);
};
