function initApplication() {
  setupLinks();
  setupSearchForm();
  setupNewsletterForm();
  setupTwitter();
  setupFlickr();
}

function setupLinks() {
  $('#headerimg h1, #follow, #subscribe').click(function(e) {
    openNavigationLink.call(this, e);
  });
}

function setupSearchForm() {
  $('#s').focus(function() { 
    this.value = (this.value == 'Search the site' ? '' : this.value); 
  }); 
  $('#s').blur(function() { 
    this.value = (this.value == '' ? 'Search the site' : this.value);
  }); 
}

function setupNewsletterForm() {
  $('#wpnewsletter_email').focus(function() { 
    this.value = (this.value == 'Your email' ? '' : this.value); 
  }); 
  $('#wpnewsletter_email').blur(function() { 
    this.value = (this.value == '' ? 'Your email' : this.value);
  }); 
  if (!(jQuery.browser.msie && parseInt(jQuery.browser.version)<=6)) {
    $('#wpnewsletter_form').ajaxForm(function() { 
      $('#newsletter').addClass('complete');
      $('#newsletter-form').hide();
    });
  }
}

function setupTwitter() {
  $("#updates").tweet({
    username: "cdiglobal",
    count: 1,
    loading_text: "Loading&hellip;"
  });
}

function setupFlickr() {
  $.getJSON("http://api.flickr.com/services/feeds/photos_public.gne?id=43251062@N05&lang=en-us&format=json&jsoncallback=?",
  function(data){
    var item = data.items[0];
    $("<img/>").attr("src", item.media.m).appendTo("#flickr")
    .wrap("<a href='" + item.link + "'></a>");
    $("#flickr img").hide();
    setTimeout(function() { scaleImage() }, 2000)
  });
}

function scaleImage() {
  var img = $("#flickr img");
  var width = img.attr("width");
  var height = img.attr("height");
  if (width < height) {
    img.attr("width", 254);
    img.attr("height", height/width * 254);
  } else {
    img.attr("width", width/height * 150);
    img.attr("height", 150);
  }
  img.show()
}

function openNavigationLink(e) {
  linkNode = this.getElementsByTagName('a').item(0)
  if (!linkNode) { return; }
  window.location = linkNode.getAttribute('href')
}

$(document).ready(initApplication);

