/***
 * Delegate
 * inspired by AS2 pixlib by F.Bourre
 * (c)2008 Alban Creton, alban@ultrasupernew.com
 * */
Delegate = {
  create: function (o, f)
          {
            var aa = [];
            if( arguments.length > 2 )
              for(var a=2; a<arguments.length; a++)
              {
                aa.push(arguments[a]);
              }
          
            var _f = function()
            { 
              var aaa = [];
              for(var a=0; a<arguments.length; a++)
              {
                aaa.push(arguments[a]);
              }
              
              return f.apply(o, aaa.concat(aa)); 
            }
            return _f;
          }
};

function getClassVar(element, var_name)
{
  var_name = var_name+"-";
  var elem    = document.getElementById($(element).attr("id"));
  var classes = elem.className.split(" ");

  for(var a = classes.length-1; a>=0; a--)
  {
    if(classes[a].indexOf(var_name) == 0)
    {
      return classes[a].split(var_name).join("");
      break;
    }
  }
  return false;
}

function removeClassLike(element, classlike)
{
  var elem    = document.getElementById($(element).attr("id"));
  var classes = elem.className.split(" ");
  for(var a = classes.length-1; a>=0; a--)
  {
    for(var j in classlike)
    {
      if(classes[a].indexOf(classlike[j]) == 0)
      {
        classes.splice(a,1);
        break;
      }
    }
  }
  
  elem.className = classes.join(" ");
}


var Navigation = function( selecter, callback, pageid )
{
  this.NAV_PREV_TEXT = "&larr; Prev.";
  this.NAV_NEXT_TEXT = "Next &rarr;";
  
  this.element     = $(selecter);
  this.prevElement = $(selecter+" span.btn-prev");
  this.nextElement = $(selecter+" span.btn-next");
  this.scoring     = $(selecter+" span.page");
  this.varname     = "page";
  this.pageid      =  pageid;
  this.callback    = callback;
  
  this.initClick();
  
};

Navigation.prototype.initClick = function()
{
  var cb = Delegate.create(this, this.dataloaded)
  var sk = this.scoring;
  $("a", this.element).click(function(){
    $.post($(this).attr("href"), {}, cb, "json");
    sk.html('<img src="/images/loading.gif" />')
    
    return false;
  })
}

Navigation.prototype.dataloaded = function(data, textStatus)
{
  P_P = data.page;
  P_LP = data.lastPage;
  
  if(data.page!=data.firstPage)
  {
    // set prev link
    var lnk = "/"+this.pageid+"?"+this.varname+"="+data.prevPage;
    this.prevElement.html( '<a href="'+lnk+'">'+this.NAV_PREV_TEXT+'</a>' );
  }
  else
  {
    // no link
    this.prevElement.html( this.NAV_PREV_TEXT );
  }
  if(data.page != data.lastPage)
  {
    // set next link
    var lnk = "/"+this.pageid+"?"+this.varname+"="+data.nextPage;
    this.nextElement.html( '<a href="'+lnk+'">'+this.NAV_NEXT_TEXT+'</a>' );
  }
  else
  {
    // no link
    this.nextElement.html( this.NAV_NEXT_TEXT );
  }
  this.initClick();
  //update scoring
  this.scoring.html("<strong>"+data.page+"</strong>/"+data.lastPage);
  this.callback(data);
  
}


EASING = {
  easeInQuad: function (x, t, b, c, d) {
		return c*(t/=d)*t + b;
	},
	easeOutQuad: function (x, t, b, c, d) {
		return -c *(t/=d)*(t-2) + b;
	},
	easeInOutQuad: function (x, t, b, c, d) {
		if ((t/=d/2) < 1) return c/2*t*t + b;
		return -c/2 * ((--t)*(t-2) - 1) + b;
	},
	easeInCubic: function (x, t, b, c, d) {
		return c*(t/=d)*t*t + b;
	},
	easeOutCubic: function (x, t, b, c, d) {
		return c*((t=t/d-1)*t*t + 1) + b;
	},
	easeInOutCubic: function (x, t, b, c, d) {
		if ((t/=d/2) < 1) return c/2*t*t*t + b;
		return c/2*((t-=2)*t*t + 2) + b;
	},
	easeInQuart: function (x, t, b, c, d) {
		return c*(t/=d)*t*t*t + b;
	},
	easeOutQuart: function (x, t, b, c, d) {
		return -c * ((t=t/d-1)*t*t*t - 1) + b;
	},
	easeInOutQuart: function (x, t, b, c, d) {
		if ((t/=d/2) < 1) return c/2*t*t*t*t + b;
		return -c/2 * ((t-=2)*t*t*t - 2) + b;
	},
	easeInQuint: function (x, t, b, c, d) {
		return c*(t/=d)*t*t*t*t + b;
	},
	easeOutQuint: function (x, t, b, c, d) {
		return c*((t=t/d-1)*t*t*t*t + 1) + b;
	},
	easeInOutQuint: function (x, t, b, c, d) {
		if ((t/=d/2) < 1) return c/2*t*t*t*t*t + b;
		return c/2*((t-=2)*t*t*t*t + 2) + b;
	},
	easeInSine: function (x, t, b, c, d) {
		return -c * Math.cos(t/d * (Math.PI/2)) + c + b;
	},
	easeOutSine: function (x, t, b, c, d) {
		return c * Math.sin(t/d * (Math.PI/2)) + b;
	},
	easeInOutSine: function (x, t, b, c, d) {
		return -c/2 * (Math.cos(Math.PI*t/d) - 1) + b;
	}
}


function distance_of_time_in_words($from_time, $include_seconds)
{
  $to_time = (((new Date()).getTime())/1000 >> 0);

  $distance_in_minutes = Math.floor(Math.abs($to_time - $from_time) / 60);
  $distance_in_seconds = Math.floor(Math.abs($to_time - $from_time));

  $string = '';
  $parameters = [];

  if ($distance_in_minutes <= 1)
  {
    if (!$include_seconds)
    {
      $string = $distance_in_minutes == 0 ? 'less than a minute' : '1 minute';
    }
    else
    {
      if ($distance_in_seconds <= 5)
      {
        $string = 'less than 5 seconds';
      }
      else if ($distance_in_seconds >= 6 && $distance_in_seconds <= 10)
      {
        $string = 'less than 10 seconds';
      }
      else if ($distance_in_seconds >= 11 && $distance_in_seconds <= 20)
      {
        $string = 'less than 20 seconds';
      }
      else if ($distance_in_seconds >= 21 && $distance_in_seconds <= 40)
      {
        $string = 'half a minute';
      }
      else if ($distance_in_seconds >= 41 && $distance_in_seconds <= 59)
      {
        $string = 'less than a minute';
      }
      else
      {
        $string = '1 minute';
      }
    }
  }
  else if ($distance_in_minutes >= 2 && $distance_in_minutes <= 44)
  {
    $string = '%minutes% minutes';
    $parameters['%minutes%'] = $distance_in_minutes;
  }
  else if ($distance_in_minutes >= 45 && $distance_in_minutes <= 89)
  {
    $string = 'about 1 hour';
  }
  else if ($distance_in_minutes >= 90 && $distance_in_minutes <= 1439)
  {
    $string = 'about %hours% hours';
    $parameters['%hours%'] = Math.round($distance_in_minutes / 60);
  }
  else if ($distance_in_minutes >= 1440 && $distance_in_minutes <= 2879)
  {
    $string = '1 day';
  }
  else if ($distance_in_minutes >= 2880 && $distance_in_minutes <= 43199)
  {
    $string = '%days% days';
    $parameters['%days%'] = Math.round($distance_in_minutes / 1440);
  }
  else if ($distance_in_minutes >= 43200 && $distance_in_minutes <= 86399)
  {
    $string = 'about 1 month';
  }
  else if ($distance_in_minutes >= 86400 && $distance_in_minutes <= 525959)
  {
    $string = '%months% months';
    $parameters['%months%'] = Math.round($distance_in_minutes / 43200);
  }
  else if ($distance_in_minutes >= 525960 && $distance_in_minutes <= 1051919)
  {
    $string = 'about 1 year';
  }
  else
  {
    $string = 'over %years% years';
    $parameters['%years%'] = Math.floor($distance_in_minutes / 525960);
  }

  for(var it in $parameters)
  {
    $string = $string.split(it).join($parameters[it])
  }
  return $string;
  
}