/**
 * Helper functions
 */
function checkBrowser() {

  /*
  //check if the visitor is using IE6 or lower:
  if ($.browser.msie && $.browser.version.substr(0,1) < 7) {
  
    var warningSource  = 'general-browser-warning.php';
    var warningContent = "<iframe width='100%' height='650px' frameborder='0' scrolling='no' allowtransparency='true' src='" + warningSource + "'></iframe>";
    
    $('html', document).css('background', '#f0f0f0').html(warningContent);
    
    return false;
  
  }
  */
  
  return true;

}

function loadCssFile(cssUrl) {

  //append a <link> tag to the document <head></head>, then set
  //its attributes as required:
  $('<link>').appendTo('head').attr({
    rel: "stylesheet",
    type: "text/css",
    href: cssUrl
  });

}

function loadJsFile(scriptUrl) {

  //jQuery's 'getScript()' method isn't ideal since it can take a while
  //for the external file to load and be evaluated, rather set all AJAX
  //requests to be synchronous first (so other code waits for the file
  //to finish loading, then set them back to asynchronous:
  $.ajaxSetup({async: false});
  $.getScript(scriptUrl);
  $.ajaxSetup({async: true});

}

/*
function trackPageview() {

  //load Google Analytics asynchronously:
  var _gaq = _gaq || [];
      _gaq.push(['_setAccount', 'UA-7147362-1']);
      _gaq.push(['_trackPageview']);
  
  (function() {
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
  })();

}
*/


/**
 * Init functions
 */
function initCufon() {

  Cufon.replace('h1, h2, h3, h4, h5, h6');

}

function initDefaultTextInputs() {

  $('input.default-text').each(function() {
  
    $(this).focus(function() {
    
      //if a default value hasn't been stored yet, do so now:
      if (!$(this).data('default-text')) {
        $(this).data('default-text', $(this).val());
      }
      
      //if the input field's current value is the default
      //text, empty it:
      if ($(this).val() == $(this).data('default-text')) {
        $(this).val('');
      }
    
    });
    
    $(this).blur(function() {
    
      //if no value was typed in the input field, retrieve
      //and display the stored default text:
      if (!$(this).val().length)
      {
        $(this).val($(this).data('default-text'));
      }
    
    });
  
  });

}

function initFacebookSdk() {

  (function(d, s, id) {
    var js, fjs = d.getElementsByTagName(s)[0];
    if (d.getElementById(id)) {return;}
    js = d.createElement(s); js.id = id;
    js.src = "//connect.facebook.net/en_US/all.js#xfbml=1";
    fjs.parentNode.insertBefore(js, fjs);
  }(document, 'script', 'facebook-jssdk'));

}

function initLightbox() {

  $('a[rel=lightbox]').lightBox({
  
    imageBlank: '/images/lightbox-blank.gif',
    //imageBtnClose: '/images/lightbox-button-close.gif',
    imageBtnPrev: '/images/lightbox-button-prev.gif',
    imageBtnNext: '/images/lightbox-button-next.gif',
    imageLoading: '/images/lightbox-icon-loading.gif'
  
  });

}

function initMainMenu() {

  $('ul#main-menu-items > li', 'div#main-menu').each(function() {
  
    $(this).hover(
                  function() {
                  
                    if ($('ul.sub-menu-items', this).length > 0)
                      $('ul.sub-menu-items', this).fadeIn('fast');
                  
                  },
                  function() {
                  
                    if ($('ul.sub-menu-items', this).length > 0)
                      $('ul.sub-menu-items', this).fadeOut('fast');
                  
                  }
                 );
  
  });

}


/**
 * Dum duum duuuuum!
 */
/*
trackPageview();
*/

$(document).ready(function() {

  //check the user's browser:
  if (checkBrowser()) {
  
    //load the pretty fonts:
    initCufon();
    
    //if there is a main menu on the page, initialize it:
    if ($('div#main-menu').length > 0)
      initMainMenu();
    
    //if inputs of class 'default-text' exist, initialize them:
    if ($('input.default-text').length > 0)
      initDefaultTextInputs();
    
    //if there are lightbox links on the page, initialize them:
    if ($('a[rel=lightbox]').length > 0)
      initLightbox();
    
    //initialize the facebook sdk:
    initFacebookSdk();
    
    //if there is a field with class "focus" on the page, focus on it:
    $('.focused').focus();
  
  };

});

