nav = {
  
  init : function() {   
     // every links in #nav except last one which is language switcher
    $('#nav a, #header h1 a, a.awards-splash').not(".last").each(function(i) {      
      $(this).click(function() {
        e = $('#main');
        // do nothing if #main on loading state
        if(!e.hasClass('loading')) {
          href = $(this).attr('href');
          $('#main').animate({opacity: 0}, 250, function() { nav.load(href); e.addClass('loading'); nav.add_spinner(); });
          nav.activate_menu_item(i);
        } 
        return false;       
      });
    });      
  },  
  
  add_spinner : function() {
    $("body").append("<div class='spinner' style='opacity: 0;'></div>");
    $(".spinner").animate({opacity: 1}, 250);
  },
  
  load : function(url) {
    $.ajax({
      type: 'GET',
      dataType: 'html',
      url: url,
      success: function(data){
        $('#main').removeClass('loading');
        nav.view(data);
        $('html,body').animate({scrollTop: 0}, 250); // scroll back to top of the page
        $(".spinner").animate({opacity: 0}, 250, function() { $('.spinner').remove(); });
      }
    });
  },
  
  view : function(data) {    
    $('#main').html(data).animate({opacity: 1}, 250);
  },
    
	activate_menu_item : function(pos) {		
		$('#nav li a').not('.last').each(function() { $(this).removeClass('active'); });
		$('#nav li a:eq('+pos+')').not('.last').addClass('active');
	}
  
};

reorder = {
  
  init : function(el) {
    $(el).sortable(
			{
				accept: 'item',
				tolerance: 'pointer',
				change : function()
				{
					$('.listing').sortable('refresh');				  
				},				
				stop : function()
				{
					hash = $('.listing').sortable('serialize', { attribute: 'id' });
					reorder.load(hash);					
				}
			}
		);
  },
  
  load : function(hash) {
    $.ajax({
      type: 'GET',
      dataType: 'json',
      url: controller_url + "/reorder",
      data: hash
    });
  }
  
};

function resize_home() {
  body_width = $('html').innerWidth();
	body_height = $('html').height();
	if($.browser.msie && $.browser.version.substr(0,3)=="6.0") {
	  margin_adjust = 0;	  
	} else {
	  margin_adjust = 23;
	}
	left_margin = ((parseInt(body_width) - parseInt($('#wrap').css('width'))) / 2) * -1;
	$('#home').css('width', body_width).css('left', left_margin + margin_adjust).css('height', body_height);
}