$(document).ready(setupHomeNews);
$(document).ready(equalizeBottomItems);
$(window).load(displayNewYearSplash);

function setupHomeNews() {

   $('#acc_nieuws_item_list').accordion({
      header:'h3'
   });
   $('#acc_nieuws_item_list h3').click(function () {
      //attribute imagepathindex contains index of array of paths, which is defined in the template itself.
      $('.nieuws_item_image .homenews_image').attr('src', homeNewsImages[this.getAttribute('imagepathindex')] );
   });
}

function equalizeBottomItems() {
   // The timeout may look a little strange,
   // but the images can load *slightly* slower than the script, resulting
   // in incorrect height measurements.
   setTimeout(doEqualize, 500)
}
function doEqualize() {
   var targetHeight, arrItemHeights;
   var jItems = $('.home_big_newsitem');
   var leftHeight = jItems.eq(0).height();
   var rightHeight = jItems.eq(1).height();

   targetHeight = Math.max(leftHeight, rightHeight);
   jItems.height(targetHeight);
}

function preloadImg(image) {
	image1 = new Image();
	image1.src = image;
}

function showFeatureImg(id,image) {
   if (image != '')
   	document.getElementById('feature_' + id).style.background = image;
}

function showFeatureBg(id,color) {
   document.getElementById('feature_' + id).style.background = color;
}

function displayNewYearSplash() {
   var splashImage, jSplash, measurements;

   var now = new Date();
   var start = new Date(2011, 11, 23);
   var end = new Date(2012, 0, 2);

   var displayTime = 8 * 1000;

   // only display within christmas and newyears period
   if (now.getTime() < start.getTime() || now.getTime() > end.getTime() ) {
      return;
   }

   splashImage = new Image();
   jSplash = $(splashImage);

   splashImage.className = 'splash_hpny';

   measurements = {
      size_x: 907,
      size_y: 559,
      correction_x: 0,
      correction_y: 0
   }
   measurements.offset_x = -1 * Math.floor(measurements.size_x / 2);
   measurements.offset_y = -1 * Math.floor(measurements.size_y / 2);

   jSplash.css({
      marginTop: measurements.correction_y,
      marginLeft: measurements.correction_x
   });


   jSplash.load(function () {
      setTimeout(showSplash, 500);
      setTimeout(hideSplash, 500 + displayTime);
   });
   jSplash.click(hideSplash);

   $('body').append(splashImage);
   splashImage.src = Globals.webRoot + 'gfx/splash_newyear_2011.png';

   function showSplash() {
      jSplash.animate({
         width: measurements.size_x,
         height: measurements.size_y,
         marginLeft: measurements.offset_x + measurements.correction_x,
         marginTop: measurements.offset_y + measurements.correction_y
      }, 400, 'swing');
   }
   function hideSplash() {
      jSplash.animate({
         width:0,
         height:0,
         marginTop:0 + measurements.correction_y,
         marginLeft:0 + measurements.correction_x
      }, 200, 'swing', function () {
         //really remove image from page.
         jSplash.css({display:'none'});
      });
   }
}
