(function($){
 $.fn.RSC= function(options) {
	
	bgImageTotal=13;
 	randomNumber = Math.round(Math.random()*(bgImageTotal-1))+1;
 	imgPath=('http://www.nature-images.eu/images/homepage/'+randomNumber+'.jpg');
  

  var pathObj = $('#inner').children();
  var i = 0;
  
	function loadImg(url, obj, loading, msg)
	{
		$(loading).css({"display":"block"}).animate({opacity:1},5000);					//display the loading in progress image
		$(obj).html("<img src='"+imgPath+"' width='972' height='578'/>").css({opacity: 0});	//throw in the image into the div block
		var tmp = $(obj).children();													//gets the image obj we just thrown in
			tmp[0].onload = function(){													//when the image has complete loaded
				$(obj).animate({opacity:1},5000);										//display the image
				$(loading).animate({opacity:0},5000).css({"display":"none"});				//hide the loading in progress image
			}
			tmp[0].onerror = function(){												//when the image fail to load for various reason
				$(this).css({"display":"none"});											//we hide the image that fail
				$(obj).html($(obj).html()+"<div>"+msg+"</div>").animate({opacity:1},5000);//provides a message to the user instead
				$(loading).animate({opacity:0},5000).css({"display":"none"});				//hide the loading in progress image
			}
	}
	function getImg(obj)
	{
		url = $(obj).css({"display":"block", "visibility":"visible"}).html();			//get the image url from the div block
		loaded = false;										//check whether image has been loaded
		if(url.search(/<.*/) != -1)							//search for opening tag, doing /'<img.*/>'/ doesn't work in IE7 and Opera somehow.
		{
			loaded=true;									//found opening tag, assume its loaded
			tmp = $(obj).children();						//get the img tag obj
			url = $(tmp[0]).attr("src");						//retrieve the url
			if(tmp[1] != undefined)							//check whether there is a second block in the children of the div block (this is added in loadImg when it fail)
				loaded = false;								//there is an error message, we try to load again
		}
		if(!loaded)
			loadImg(url,obj, "#loading", "there seems to be an issue, try again later");
	}
	getImg(pathObj[0]);

 };
})(jQuery);
