

/*--------------------------------------------------------------
Import JS into header function
/*--------------------------------------------------------------*/


function importJS(jsFile) {
  var scriptElt = document.createElement('script');
  scriptElt.type = 'text/javascript';
  scriptElt.src = jsFile;
  document.getElementsByTagName('head')[0].appendChild(scriptElt);
}


$(document).ready(function() {
  
  if($('#body').length > 0) {
    if($('#sub-navigation').height() > $('#body').height() ) {
      $('#body').height($('#sub-navigation').height());
    }
  }

/* Breadcrumbs */
$("a.breadcrumb").hover(function() {
    $(this).next('em').children().css({"background-position" : "-210px -256px"});
    $(this).css({"backgroundColor" : "#eee"});
    $(this).prev('em').css({"backgroundColor" : "#eee"});
    $(this).next('em').css({"backgroundColor" : "#eee"});
  }, function() {
    $(this).css({"backgroundColor" : "#fff"});
    $(this).prev('em').css({"backgroundColor" : "#fff"});
    $(this).next('em').css({"backgroundColor" : "#fff"});
    $("#breadcrumbs span").css({"background-position" : "-200px -256px"});
});


/*--------------------------------------------------------------
Enable site search
/*--------------------------------------------------------------*/

$('input#search-submit').each( function() { 
  var input = $(this); 
  $('<a href="#">' + this.value + '</a>') 
    .attr( 'class', this.className )  
    .insertBefore( this) 
    .click( function() {  
      input.click(); 
      return false;  
    }); 
  $('<em class="blue-button-finish">&nbsp;</em>') 
    .insertBefore(this); 
  input.hide();  
}); 

/*--------------------------------------------------------------
Form Processing
/*--------------------------------------------------------------*/

var initialValue = "";
var userValue = "";

$("input.round-field").click(function() {
  if (userValue == initialValue || $(this).val() == initialValue) {
    initialValue = $(this).val();
    $(this).val("");
  } else {
    userValue = $(this).val();
  } 
});
$("input.round-field").blur(function() {
  if($(this).val() == "") {
    $(this).val(initialValue);
  }
})


/*--------------------------------------------------------------
Hero Cycle
/*--------------------------------------------------------------*/

/* Set the cycle speed in seconds */
var cycleSpeed = 5;
/* Set the transition speed in seconds */
var transitionSpeed = 0.25;


/* Build cycle reflection and link */

$("div#hero").before('<a id="hero-url" href="' + $("#hero a:first-child").attr("href") + '">' + $("#hero a:first-child").attr("title") + '</a>'); 
if (!$.browser.msie) {
  $("div#hero a img").reflect({height:20,opacity:0.25});  
}

/* Build cycle captions */

$('div#hero a').each(function(index) {
  cyclecount = $('div#hero a').length;
  if(index == cyclecount-1) { var starter = " selected" } else { var starter = "" };
  $('div#hero').after('<li id="caption-bg-hero-' + (cyclecount-index) + '" class="caption-bg' + starter + '"><em><span>' + $("a.hero-" + (cyclecount-index)).attr("title") + '</span></em><span class="hero-captions-finish">&nbsp;</span></li>');
  $('div#hero').after('<li id="hero-' + (cyclecount-index) + '" class="hero-caption' + starter + '"><a href="javascript:void(0)">' + $("a.hero-" + (cyclecount-index)).attr("title") + '</a></li>');
});

$('li.hero-caption').wrapAll('<ul id="hero-captions" />');
$('li.caption-bg').wrapAll('<ul id="caption-bgs" />');
$('ul#hero-captions, ul#caption-bgs').css({"margin-left" : (616-$('ul#hero-captions').width())/2 + "px"});

$('ul#caption-bgs li.caption-bg').animate({
  opacity: 0.0
}, 0);



/* Reset cycle once it's finished */

function resetCycle() {
  $("a.hero-1, .reflection-1").show();
  $(".reflection-1").animate({
    opacity: 1.0
  }, transitionSpeed*1000);
  $("a.hero-1").animate({
    opacity: 1.0
  }, transitionSpeed*1000, function() {
      $("a#hero-url").attr("href", $("li#hero-1 a")
        .attr("href")).attr("title", $("li#hero-1 a").html())
        .html($("li#hero-1 a").html());
      cyclecount = $('div#hero a').length;
      
      $('div#hero a').each(function(index) {
        $("a.hero-" + ((cyclecount-1)-index)).show().animate({
          opacity: 1.0
        }, 0);
        $(".reflection-" + ((cyclecount-1)-index)).show().animate({
          opacity: 1.0
        }, 0);
      });
  });
  $('li.hero-caption a').attr("href", "javascript:void(0)");
  startCycle();
  $('li#caption-bg-hero-' + cyclecount).animate({
    opacity: 0.0
  }, transitionSpeed*1000);
};

/* Start the cycle */

function startCycle() {
  $('li#caption-bg-hero-1').animate({
    opacity: 1.0
  }, 0);
  $("a.hero-1").animate({
    opacity: 1.0
  }, cycleSpeed*1000, function() {
    heroCycle(1);
  });
}
startCycle();

/* Cycle */

function heroCycle(index) {
  cyclecount = $('div#hero a').length;
  $('li.hero-caption a').attr("href", "javascript:void(0)");  
  $('li#caption-bg-hero-' + (index)).animate({
    opacity: 0.0
  }, transitionSpeed*1000);
  $('li#caption-bg-hero-' + (index+1)).animate({
    opacity: 1.0
  }, transitionSpeed*1000);
  $(".reflection-" + index).animate({
    opacity: 0.0
  }, transitionSpeed*1000);
  $("a.hero-" + index).animate({
    opacity: 0.0
  }, transitionSpeed*1000, function() {
    $("a.hero-" + index).hide();
    $("a#hero-url").attr("href", $("a.hero-" + (index+1)).attr("href"))
      .attr("title", $("li#hero-" + (index+1) + " a").html())
      .html($("li#hero-" + (index+1) + " a").html());
    $("a.hero-" + (index+1)).animate({
      opacity: 1.0
    }, cycleSpeed*1000, function () {
      if (index == (cyclecount-1)) {
        resetCycle();
      } else {
        heroCycle(index+1);
      }
    });
  }); 
  //_gaq.push(['_trackEvent', 'Homepage box', 'View', $('a#hero-url' + index).attr('href')]);
};

function continueCycle(current) {
  cyclecount = $('div#hero a').length;
  $("a.hero-1, .reflection-1, li#caption-bg-hero-1").animate({
    opacity: 0.0
  }, 0);
  if(current == cyclecount) {
    resetCycle();
  } else {
    $('li.hero-caption a').attr("href", "javascript:void(0)");
    for (i=current+1;i<=cyclecount;i++) {
      $("a.hero-" + i).show().animate({
        opacity: 1.0
      }, 0);
      $(".reflection-" + i).show().animate({
        opacity: 1.0
      }, 0);
    }
    heroCycle(current);
  }
}

/* Control the Cycle */

$('li.hero-caption a').click(function() {
  $('span canvas').each(function(index) { 
    $(this).appendTo($('#hp-features .container'));
    $(this).addClass("reflection-" + (index+1))
  })
  $('li.caption-bg').stop();
  var showImg = ($(this).parent().attr('id'));
  var showNext = parseInt(showImg.replace(/hero-/,''));
  $('#hero a, canvas').stop().hide();
  $('li.caption-bg').animate({
    opacity: 0.0
  }, 0);
  $('ul#hero-captions li a').each(function(index) {
    urlindex = index+1;
    if (("hero-" + urlindex) !== showImg) {
      $('li#hero-' + urlindex + ' a').attr("href", "javascript:void(0)");
    };
  });
  $('li#caption-bg-' + showImg).animate({
    opacity: 1.0
  }, transitionSpeed*1000);
  $("." + showImg + ", .reflection-" + showNext).show();
  $("." + showImg + ", .reflection-" + showNext).animate({
    opacity: 1.0
  }, 0, function() {
    $("." + showImg).animate({
      opacity: 1.0
    }, 2, function() {
      $("#" + showImg + " a").attr("href", $("a." + showImg).attr("href"));
      $("a#hero-url").attr("href", $("a." + showImg).attr("href"))      
    });
  });
  $('li#caption-bg-' + showImg).animate({
    opacity: 1.0
  }, 8000, function() {
    continueCycle(showNext);
  });
})


/*--------------------------------------------------------------
Blog Subscriptions - disabled for now


$("ul#blog-subscriptions li i").show().animate({
  opacity: 0
}, 0);
$("ul#blog-subscriptions li a").hover(function() {
  $(this).parent().children("i").animate({ opacity: 1.0 }, 200);
  $(this).parent().children("em").animate({ opacity: 0 }, 200)
}, function() {
  $(this).parent().children("i").animate({ opacity: 0 }, 200);
  $(this).parent().children("em").animate({ opacity: 1.0 }, 200);
});
/*--------------------------------------------------------------*/


/*--------------------------------------------------------------
Store
/*--------------------------------------------------------------*/

$("a.audio-sample").click(function() {
  if ($(this).hasClass("selected")) {
    $(this).css({"background-position" : "-645px -1138px"});
    $(this).removeClass("selected");
  } else {
    $("a.audio-sample").css({"background-position" : "-645px -1138px"});
    $(this).css({"background-position" : "-645px -1171px"}).addClass("selected"); 
  }
});


/*--------------------------------------------------------------
Include Ajax Tabs - disabled for now


 if($('div.tabs').length !== 0) {
  $("head").append('<script type="text/javascript" src="' + $("#branding").attr("href") + 'js/ajax-tabs-initialize.js"></scr' + 'ipt>');
} 
if($('div.static-tabs').length !== 0) {
  $("head").append('<script type="text/javascript" src="' + $("#branding").attr("href") + 'js/static-tabs-initialize.js"></scr' + 'ipt>');
}
/*--------------------------------------------------------------*/


/*--------------------------------------------------------------
Make the Site Feedback link a Fancybox

$("head").append('<script type="text/javascript" src="' + $("#branding").attr("href") + 'js/jquery.modalbox.js"></scr' + 'ipt>');

$("a#fancybox-feedback-link").fancybox({
  'titleShow' : false,
  'transitionIn'  : 'elastic',
  'transitionOut' : 'elastic'
});
/*--------------------------------------------------------------*/


/*---------------------------------------------------------------
Track external links in Google Analytics
----------------------------------------------------------------*/
$("a[rel*='external']").click(function(){
  _gaq.push(['_trackPageview', '/external-links/'+ $(this).attr('href')]);
});
$('a#hero-url').click(function() {
  _gaq.push(['_trackEvent', 'Homepage box', 'Click', $('a#hero-url').attr('href')]);
  //_gaq.push(['_trackPageview', '/clicks-on-homepage-heros/'+ $('a#hero-url').attr('href')]);
});
  
});
