// Set browser-based CSS classes on the HTML tag
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;

jQuery(function($){

  var selected_screen = selected_image = rotator_handle = false;

  $('nav.site').htmlClean();

  // Screenshots
  $('.iphone .screen img').each(function(idx, img){
    var img = $(img);
  });

  $('#screen_picker A').each(function(idx, link) {
    var link    = $(link),
        img     = link.find('img'),
        sel     = ".iphone .screen img[src='"+ img.attr('src') +"']",
        bigImg  = $(sel);
    if(link.hasClass('selected')) {
      selected_screen = link;
      selected_image = bigImg;
    } else {
      bigImg.hide();
    }
    
    link.click(function(e) {
      e.preventDefault();
      if(rotator_handle) {
        clearTimeout(rotator_handle);
        rotator_handle = false;
      }
      if(selected_screen) {
        selected_screen.removeClass('selected')
        selected_image.fadeOut();
      } 
      link.addClass('selected')
      bigImg.fadeIn();
      selected_screen = link;
      selected_image = bigImg;
      return false;
    });
  });
  
  function rotate_screenshots() {
    var next = selected_screen.next();
    if(next.size()) {
      next.click()
    } else {
      selected_screen.siblings().first().click()
    }
    rotator_handle = setTimeout(rotate_screenshots, 5000); // Five seconds
  }
  
  function position_app_labels() {
    // App labels
    $('nav.apps label').each(function(idx, lbl){
      var lbl = $(lbl), 
          rel = lbl.attr("for"), 
          icon = $("#"+ rel), 
          iconW = 57,
          width = lbl.width() + 20, 
          left = icon.get(0).offsetLeft,
          center = left + (iconW / 2),
          newLeft = Math.floor( center - (width / 2) );

      lbl.css('left', newLeft+"px")
      icon.hover(function(){
        lbl.show(); //fadeIn(200);
      }, function(){
        lbl.hide(); //fadeOut(200);
      });
    });
  }
  
  if(selected_screen) {
    rotator_handle = setTimeout(rotate_screenshots, 5000); // Five seconds
    // (should this wait for document load?)
  }
  
  position_app_labels();
  
  $(window).on('load', function(){
    position_app_labels();
  });

});


jQuery.fn.htmlClean = function() {
    this.contents().filter(function() {
        if (this.nodeType != 3) {
            $(this).htmlClean();
            return false;
        }
        else {
            return !/\S/.test(this.nodeValue);
        }
    }).remove();
}

if(!jQuery.fn.on) jQuery.fn.on = jQuery.fn.bind;

