/*
 * jquery-counter plugin
 *
 * Copyright (c) 2009 Martin Conte Mac Donell <Reflejo@gmail.com>
 * Dual licensed under the MIT and GPL licenses.
 * http://docs.jquery.com/License
 */

function dateDiff(until) {
				date1 = new Date();
				date2 = new Date();
				diff  = new Date();
				
				date1temp = new Date();
				date1temp.getUTCDate();
				date1.setTime(date1temp.getTime());
				
				date2temp = new Date(until);
				date2.setTime(date2temp.getTime());
				
				diff.setTime(Math.abs(date1.getTime() - date2.getTime()));
				
				timediff = diff.getTime();
				days = parseInt(timediff / (1000 * 60 * 60 * 24)); 
				timediff -= days * (1000 * 60 * 60 * 24);
				
				if (days < 100)
				{
					days = "0" + days;
				}
				else if (days < 10)
				{
					days = "00" + days;
				}
				
				hours = parseInt(timediff / (1000 * 60 * 60)); 
				timediff -= hours * (1000 * 60 * 60);
				
				if (hours < 10)
				{
					hours = "0" + hours;
				}
				
				mins = parseInt(timediff / (1000 * 60)); 
				timediff -= mins * (1000 * 60);
				
				if (mins < 10)
				{
					mins = "0" + mins;
				}
				
				secs = parseInt(timediff / 1000); 
				timediff -= secs * 1000;
				
				if (secs < 10)
				{
					secs = "0" + secs;
				}
	
				return (days + " " + hours + " " + mins + " " + secs);
			}
			
(function($){$.prettyPhoto={version:'2.5.6'};$.fn.countdown = function(userOptions)
{
  // Default options
  var options = {
    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: "digits.png"
  };
  var digits = [], interval;

  // Draw digits in given container
  var createDigits = function(where) 
  {
    var c = 0;
    var tempStartTime = options.startTime;

    // 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(tempStartTime.charAt(i)) >= 0) 
      {
      
        elem = $('<div id="cnt_' + i + '" class="cntDigit" />').css({
          height: options.digitHeight * options.digitImages * 10, 
          float: 'left', background: 'url(\'' + options.image + '\')',
          width: options.digitWidth});
        digits.push(elem);
        margin(c, -((parseInt(tempStartTime.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[i]) {
          case 'h': 
            digits[c].__max = (c % 2 == 0) ? 3: 2;
            break;
          case 'm':
          case 's':
            digits[c].__max = (c % 2 == 0) ? 9: 5;
        }
        ++c;
      }
      else 
        elem = $('<div class="cntSeparator"/>').css({float: 'left'})
                .text(tempStartTime.charAt(i));

      where.append(elem)
    }
  };
  
  // Set or get element margin
  var margin = function(elem, val) 
  {
    if (val !== undefined)
      return digits[elem].css({'marginTop': val + 'px'});

    return parseInt(digits[elem].css('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() {
      mtop = margin(elem) + options.digitHeight;
      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, userOptions);
  this.css({height: (options.digitHeight - 1), overflow: 'hidden'});
  createDigits(this);
  interval = setInterval(moveStep(digits.length - 1), 1000);
}})(jQuery);
