// CSS Browser Selector   v0.2.3b (M@: added noscript support)
// Documentation:         http://rafael.adm.br/css_browser_selector
// License:               http://creativecommons.org/licenses/by/2.5/
// Author:                Rafael Lima (http://rafael.adm.br)
// Contributors:          http://rafael.adm.br/css_browser_selector#contributors
var css_browser_selector = function() {
  var 
    ua = navigator.userAgent.toLowerCase(),
    is = function(t){ return ua.indexOf(t) != -1; },
    h = document.getElementsByTagName('html')[0],
    b = (!(/opera|webtv/i.test(ua)) && /msie (\d)/.test(ua)) ? ((is('mac') ? 'ieMac ' : '') + 'ie ie' + RegExp.$1)
      : is('gecko/') ? 'gecko' : is('opera') ? 'opera' : is('konqueror') ? 'konqueror' : is('applewebkit/') ? 'webkit safari' : is('mozilla/') ? 'gecko' : '',
    os = (is('x11') || is('linux')) ? ' linux' : is('mac') ? ' mac' : is('win') ? ' win' : '';
  var c = b+os+' js'; 
  if(is('firefox')) {
    var ver = parseFloat(ua.replace(/^.*firefox\/(.*).*$/, '$1'));
    if(ver >= 3.1) c += " gecko-new";
  }
  h.className = h.className.replace('noscript', '') + h.className?' '+c:c;
}();


$(function(){
/*
   $('.relative-time').each(function(){
      var date = Date.fromString( $(this).attr('rel'));
      $(this).html(date.toRelativeTime());
   });
*/
   
   
  $('.tool-tip').bt({
    positions: 'top',
    padding: 15,
    shrinkToFit: true,
    width: 225,
    spikeLength: 15,
    spikeGirth: 15,
    cornerRadius: 15,
    fill: 'rgba(0, 0, 0, .8)',
    strokeWidth: 3,
    strokeStyle: '#FFF',
    cssStyles: {color: '#FFF', fontWeight: 'bold', size:"12pt", textAlign:"center"}
  });
   
});

function showSlideshowComic(name, len, ext) {
   var baseUrl = '/media/slideshow-comics/',
       images = [],
       ext = ext || '.jpg',
       options = {
         title: "Slideshow Comic "+ name,
         gallery: "Slideshow Comic "+ name,
         continuous: false,
         fade: false,
         animate: false,
         resize: false,
         autoresize: false,
         animateFade: false,
         fadeDuration: 0,
         resizeDuration: 0,
         modal: true
       };
   for(var i=1; i<=len; i++) {
      var cnt = (i < 10) ? '0'+i : i,
          img = {
            player: 'img',
            content: baseUrl + name +'/'+ cnt + ext,
            options: options
          };
/*
      console.log('Image:')
      console.log(img)
*/
      images.push( img );
   }
   Shadowbox.open(images)   
}


/*
 * Returns a description of this past date in relative terms.
 * Example: '3 years ago'
 */
Date.prototype.toRelativeTime = function() {
  var delta       = new Date() - this;
  var units       = null;
  var conversions = {
    millisecond: 1, // ms    -> ms
    second: 1000,   // ms    -> sec
    minute: 60,     // sec   -> min
    hour:   60,     // min   -> hour
    day:    24,     // hour  -> day
    month:  30,     // day   -> month (roughly)
    year:   12      // month -> year
  };

  for(var key in conversions) {
    if(delta < conversions[key]) {
      break;
    } else {
      units = key; // keeps track of the selected key over the iteration
      delta = delta / conversions[key];
    }
  }

  // pluralize a unit when the difference is greater than 1.
  delta = Math.floor(delta);
  if(delta !== 1) { units += "s"; }
  return [delta, units, "ago"].join(" ");
};

/*
 * Wraps up a common pattern used with this plugin whereby you take a String 
 * representation of a Date, and want back a date object.
 */
Date.fromString = function(str) {
  return new Date(Date.parse(str));
};


var HTML = {
  _all_tags: ['a','abbr','acronym','address','area','b','base','bdo','big','blockquote','body','br','button','caption','cite','code','col','colgroup','dd','del','dfn','div','dl','DOCTYPE','dt','em','fieldset','form','h1','h2','h3','h4','h5','h6','head','html','hr','i','img','input','ins','kbd','label','legend','li','link','map','meta','noscript','object','ol','optgroup','option','p','param','pre','q','samp','script','select','small','span','strong','style','sub','sup','table','tbody','td','textarea','tfoot','th','thead','title','tr','tt','ul'],
  _init: function() {
    for(var $i=0; $i<this._all_tags.length; $i++) {
      var $tag = this._all_tags[$i]
      //eval( 'HTML.'+ $tag +' = function (){ return HTML._write_tag("'+ $tag +'", arguments); }' );
      this[$tag] = new Function("return HTML._write_tag('"+ $tag +"', arguments);");
      this.namespace += ';var '+ $tag +' = HTML.'+ $tag;
    }
  },
  _write_tag: function(tag, options) {
    var $content = ''; var $atts = "";
    for(var $i=0; $i<options.length; $i++) {
      var $arg = options[$i];
      if (typeof($arg) == 'string' || typeof($arg) == 'number')
        $content += $arg.toString();
      else if (typeof($arg) == 'function')
        $content += $arg();
      else if ( $arg instanceof Object )
        for($prop in $arg) {
          if(typeof $atts[$prop] != 'function') // A fix for Prototype
            $atts += ' '+ $prop +'="'+ $arg[$prop] +'"'; 
        }
    }
    if($content == '')
      return '<'+ tag + $atts +'/>';
    else
      return '<'+ tag + $atts +'>'+ $content +'</'+ tag +'>';
  },
  namespace: 'var text = HTML.text',
  text: function() { 
    var $content=''; 
    for(var $i=0; $i<arguments.length; $i++){ $content += arguments[$i].toString(); }
    return $content; 
  }
};
HTML._init();