var xhr;
var lastSender;


$(document).ready(function() {
  
  //load galleria theme
  Galleria.loadTheme('scripts/galleria/src/themes/classic/galleria.classic.js');
  //Galleria.loadTheme('scripts/galleria/src/themes/fullscreen/galleria.fullscreen.js');//
  
 
  
  // Initial page animation
  $('#banner').css({marginLeft:'-200px'}).animate({marginLeft:'0'}, 800);
  $('#navigation').css({marginLeft:'-400px'}).animate({marginLeft:'0'}, 800);
  //$('#content').hide(0).delay(1500).fadeIn(1000);
  
  //fix for width sensitive scripts
  
  // Setup AJAX
  init_links(true);
  
  $('#banner').mouseover(function(){
    $('#navigation').stop().animate({marginLeft:'0'}, 400);
    $('#subnav').stop().animate({marginLeft:'0'}, 400);
  });
  
  // Setup link handling
  //var homeCache = $('#content').html();
  var firstLevelPages = ['/home.aspx', '/default.aspx'];
  $.address.change(function(event) {  
    if(event.value != '/'){
      var firstLevel = $.inArray(event.value, firstLevelPages) >=0;
      loadpage(null, event.value, firstLevel);
    }
    else {
      //$('#content').html(homeCache);
      $('#navigation').stop().animate({marginLeft:'0'}, 400);
    
    }
  });  
  
  // Setup background image
  $('#background-image').maxImage({ isBackground: true });
  
  //hide loader image
   $('#loaderimg').hide();
  
});

function init_links(firstlevel){
  // Re-write page links
  $('a').not('#home').each(function(){
    var href = $(this).attr('href');
    if(href.indexOf('#') < 0
       && href.indexOf('www.') < 0
       && href.indexOf('http') < 0
       && href.indexOf('mailto') < 0
      ) {
      $(this).attr('href', '#' + href);
    }
  });
}

function loadpage(sender, page, firstlevel){
 /* Commenting out as sender is always null
  if(lastSender){
    $(lastSender).removeClass('selected');
  }
  if(sender){
    lastSender = sender;
    $(lastSender).addClass('selected');
  }
*/
  
  //show loader image
  $('#loaderimg').show();
  
  // Slide the nav back
  //commenting out as firstlevel is never true
  //if(firstlevel){
  //  $('#subnav').animate({marginLeft:'-400px'},200);
  //  $('#content').fadeOut(400);
  //}
  //else
  //{
    $('.content-area').fadeOut(400);
  //}
  
  // Perform the AJAX op
  if(xhr !== undefined) {
    xhr.abort();
  }

    xhr = jQuery.ajax({
    url:page,
      async: false,
      cache: false,
    success: function(data) {
      PostAjax(firstlevel, data);
    }
    });
  
  function PostAjax(firstlevel, data)
  {
/*
    if(firstlevel){
        $('#navigation').stop().animate({marginLeft:'0'}, 400);
        $('#content').delay(400).html('').html(data).fadeIn(400);
        $('#subnav').stop().delay(3800).animate({marginLeft:'0'}, 400);
        
      }
      else {
  */
        $('#content').html(data);
    
  init_links(false);
    
    $('#navigation').stop().animate({marginLeft:'-200px'}, 400);
        //$('#content').html('').html(data)

        $('.content-area').stop().fadeOut(0).fadeIn(800);       
   //   }
      
      $('.content-area').mouseover(function(){
        $('#navigation').stop().animate({marginLeft:'-200px'}, 400);
        
        if ($('#subsubnav').length) {
           $('#subnav').stop().animate({marginLeft:'-200px'}, 400);
        }
      });
    
    resizeElements();
        
 
    
    resizeDealerSolutions();

  //hide loader image
  $('#loaderimg').hide();
    
    
 //    alert($('.news-image img:first').attr('src'));

//  $('.news-image img:first').load(function(){
//alert($('.news-image img:first').attr('src'));
 

 //alert('ajax ready');
    
  }
}

$(window).resize(function () {

resizeElements();

});

function resizeElements()
{
  var logoPos = $('#logo').offset();
    var newsOffset = logoPos.top - 300;
    if(newsOffset < 0)
      newsOffset = 0;
    var windowHeight = $('body').height();
    var newsHeight = windowHeight - newsOffset - 20;
    
    $('.news-list').css({ 'top' : newsOffset + 'px', 'height' : newsHeight +'px'});
  
  var iframeWidth = $('body').width() - 460 - 50;
  var iframeHeight = windowHeight - 50;
  $('iframe').not('#google').css({position: 'relative',
                   top: '20px',
                   left: '20px',
                   'width': iframeWidth +'px',
                   'height': iframeHeight +'px'});
}



function resizeDealerSolutions() {
  /*
  var width = $('body').width - 370;
  var height = $('body').height - 40;
  $('#dealer-solutions').css({ 'width': width +'px', 'height': height +'px'});
  alert('resized dealer solutions');
  */
}




