function get_new_ajax_object() {
   var x; // The variable that makes Ajax possible!
   try {
      // Opera 8.0+, Firefox, Safari
      x = new XMLHttpRequest();
   } catch (e) {
      // Internet Explorer Browsers
      try {
         x = new ActiveXObject("Msxml2.XMLHTTP");
      } catch (e) {
         try{
            x = new ActiveXObject("Microsoft.XMLHTTP");
         } catch (e){
            // Something went wrong
            alert("Your browser broke!");
            x = false;
         }
      }
   }
   return x;
}

function load_news() {
   var ajaxRequest = get_new_ajax_object();
   ajaxRequest.onreadystatechange = function() {
      if (ajaxRequest.readyState == 4) {
         if (ajaxRequest.status == 0 || ajaxRequest.status == 200) {
            $('div#news_expander').append("<div id=\"newsbox_in_expander\" style=\"float: left;\"></div>");
            $('div#newsbox_in_expander').html(ajaxRequest.responseText);
         } else {
            // error status code
            alert("status: " + ajaxRequest.status);
         }
      }
   }
   ajaxRequest.open("GET", "/content/plain_news.php", true);
   ajaxRequest.send(null);
//   alert("request sent");
}

$(document).ready(function() {
   // fade to/from grey in "services" image boxes
   var images = $('div.box a > img');
   images.each(function() {
      var img = $(this);
      var image = img.attr('src');
      var grey_image = image.replace('/', '/grey-');
      img.wrap("<span style=\"position: absolute;\"></span>").parent().prepend("<img style=\"margin-left: 5px;\">").find(":first-child").attr('src', grey_image);
      img.css({'position': 'absolute', 'left': -1, 'top': $(this).offsetTop, 'opacity': 0,filter: 'alpha(opacity = 0)'});
      $(this).parent().parent().hover(function() {
         img.stop().animate({opacity: 1}, 250);
      }, function() {
         img.stop().animate({opacity: 0}, 3000);
      });
   });
   // expanding news div
   var news_clicky = $('div.box-news');
   var services_box = news_clicky.parent();
   services_box.css({'position': 'relative'});
   news_clicky.wrap("<div id=\"news_expander\" style=\"position: absolute; right: 0;\"></div>");
   var news_div = news_clicky.parent();
   // load news
   load_news();
   news_clicky.bind('click', function() {
      if (news_div.outerWidth() <= 100) {
         // open it
         news_div.stop().animate({'width': '990px'}, 250);
         $('div#newsbox_in_expander').animate({width: 880}, 250);
      } else {
         // close it
         news_div.stop().animate({'width': '90px'}, 250);
         $('div#newsbox_in_expander').css('width', '0');
      }
   });
});

