$(document).ready(function() {

//SHOW THE LOADER AND HIDE BACKGROUND IMAGE UNTIL IT IS LOADED
$('#bg img').css('display', 'none');
$('body').append('<span id="body_loader"></span>');
$('#body_loader').fadeIn();


//RANDOM IMAGE ON PAGE LOAD
$.randomImage = {
defaults: {

//you can change these defaults to your own preferences.
path: 'http://www.iansa.cl/sitioiansagro/wp-content/themes/iansagro/images/', //change this to the path of your images

myImages: ['background1.jpg'], //put image names in this bracket.
myImages2: ['background2.jpg','background3.jpg','background5.jpg'] //put image names in this bracket for page.

}
}

$.fn.extend({
randomImage:function(config) {
var config = $.extend({}, $.randomImage.defaults, config);
return this.each(function() {
if($('body').attr('class') == 'body_home'){
var imageNames = config.myImages;
}else{
var imageNames = config.myImages2;
}


//get size of array, randomize a number from this
// use this number as the array index
var imageNamesSize = imageNames.length;
var lotteryNumber = Math.floor(Math.random()*imageNamesSize);
var winnerImage = imageNames[lotteryNumber];
var fullPath = config.path + winnerImage;
//put this image into DOM at class of randomImage
// alt tag will be image filename.
$(this).attr( {
src: fullPath,
alt: winnerImage
});
});
}
});


//run the random script
$('.random').randomImage();

//random image script ends here

}); //document.ready function ends here


//WAIT UNTIL CONTENT IS LOADED
$(window).load(function() {

//hide loader
$('#body_loader').hide().remove();

//append grid if not on the home page

//fade in the image
$('#bg img').fadeIn('normal');


});


