// main js
jQuery(document).ready(function(){

  $('a.nav-link').click(function(){
    var ele = $(this);
    var id = ele.attr('id');
    slideContent(ele);
    slideFG(id);

    // track a custom page view
    trackCustomPageView('/'+id);

    return false;
  });

  $("a.pop-up,a.specs,a.terms").each(function(){
    var href = $(this).attr('href');
    $(this).attr('href', href+'?l=0');
  });
  
  // fancy box
  if ( $.browser.msie ) {
    $("a.pop-up").fancybox({
      "type":"iframe",
      "width": 665,
      "height": 415,
      'onStart': function() {
        // log a custom page view
        trackCustomPageView($(this).attr('href'))
      }
    });
  }
    else
  {
    $("a.pop-up").fancybox({
      "type":"iframe",
      "width": 657,
      "height": 403,
      'onStart': function() {
        // log a custom page view
        trackCustomPageView($(this).attr('href'))
      }
    });
  }
  
  $("a.specs").fancybox({
    "type":"iframe",
    "width": 750,
    "height": 580,
    'onStart': function() {
      // log a custom page view
      trackCustomPageView('/specs')
    }
  });
  
  $("a.terms").fancybox({
    "type":"iframe",
    "width": 750,
    "height": 550,
    'onStart': function() {
      // log a custom page view
      trackCustomPageView('/specs')
    }
  });

  // set all the form fields to blank on focus
  $('.form-field').focus(function(){
    var f = $(this);
    var val = f.attr('value');
    var alt = f.attr('alt');
    
    if(val==alt)
    {
      f.attr('value','');
      f.css('color', 'black');
    }
  });

  // if they field is blank on blur, set it to the alt value
  $('.form-field').blur(function(){
     var f = $(this);
     var val = f.attr('value');
     var alt = f.attr('alt');

     if(val=='')
     {
       f.attr('value',alt);
       f.css('color', 'silver');
     }
   });
   
   // listen to the submit link
   $('#contact-submit').click(function () {
     $('#contact-form').submit();
     return false;
   });
});

function slideContent(ele){
  var move = ele.attr('rel');
  var step = -752;
  $('div#slider').stop().animate({'left': (move*step) + 'px'},1500);
  setCurrentSlide(ele);
  return false;
}

function slideFG(id){
  var move = $('div#'+id+'-fg').attr('rel');
  var step = -2000;
  $('div#slider-fg').stop().animate({'left': (move*step) + 'px'},1500);
  $('div#skyline').stop().animate({'backgroundPosition': ((-step/5)*move) + 'px'},1500);
}

function setCurrentSlide(ele){
  $('ul#nav li, ul#footer li').removeClass('current');
  ele.parent().addClass('current');
}

/**
 * Tracks a custom page view for a "page" that was actually loaded via
 * Javascript. This used by the main nav. Since the "/faster" page is
 * actually the homepage, we set it to be so here.
 *
 * @param uri
 */
function trackCustomPageView(uri)
{
  if (uri == '/faster')
  {
    uri = '/';
  }

  _gaq.push(['_trackPageview', uri]);
}

