
////////////////////////////////////////////////////////////
$(document).ready(function(){
  $('#pageImage').html("");
  topContentDisplay();
});

function topContentDisplay(){
  //FILE = "common/xml/topimg.xml?"+new Date().getTime(); //
  FILE = "http://www.justicesurfboard.com/topimgxml/?"+new Date().getTime(); //
  PHOTOALT = "justice surf boad";
  $.ajax({
    type: "GET",
    url: FILE,
    dataType: "xml",
    success: function(xml){
      XML = xml;
      photoArray = new Array();
      ii = 0;
      $(XML).find("photo").each(function(){
        if($('#imagesPhoto').attr("src") != $(this).find("image").text()){
          preloadImage('obj'+ii,$(this).find("image").text());
        }
      });
      changeImage();
    }
  });
};

function changeImage(){
  photoArray = new Array();
  i = 0;
  $(XML).find("photo").each(function(){
    if($('#imagesPhoto').attr("src") != $(this).find("image").text()){
      photoArray[i] = $(this).find("image").text();
      i++;
    }
  });

  if($('#imagesPhoto').attr("src")){
    PHOTO = photoArray[Math.floor(Math.random()*photoArray.length)]
    $('#imagesPhotoBack').remove();
    $('#imagesPhoto').after('<img src="'+$('#imagesPhoto').attr("src")+'" width="950" height="375" alt="'+PHOTOALT+'" id="imagesPhotoBack" />')
    $('#imagesPhoto').css("display","none");
    $('#imagesPhoto').attr("src",PHOTO);
    $('#imagesPhoto').fadeIn(1500,function(){
      setTimeout('changeImage()',5000);
    });
  }else{
    $('#pageImage').append('<img src="'+photoArray[0]+'" width="950" height="375" alt="'+PHOTOALT+'" id="imagesPhoto" />')
    $('#imagesPhoto').css("display","none");
    $('#pageImage').css("position","relative");
    $('#imagesPhoto').css("position","absolute");
    $('#imagesPhoto').css("top","0px");
    $('#imagesPhoto').css("left","0px");
    $('#imagesPhoto').fadeIn(1500,function(){
      setTimeout('changeImage()',5000);
    });
  }
}

function preloadImage(obj,val){
  var d = document;
  var img = obj;
  d.img = new Image();
  d.img.src = val;
}

