var ratio=1680/1114;
var imgsrc = 'img/example_bodybg.jpg';

var gg_BgImg = new Array();
var gg_BgImgRatio = new Array();

var gg_currItem = 1
var gg_numItem = 6


gg_BgImg[1] = 'img/example_bodybg.jpg';
gg_BgImg[2] = 'slideshow/Ph.Corinto_CMA3653.jpg';
gg_BgImg[3] = 'slideshow/sweets.jpg';
gg_BgImg[4] = 'slideshow/Ph.Corinto_CMA3662.jpg';
gg_BgImg[5] = 'slideshow/theden.jpg';
gg_BgImg[6] = 'slideshow/flagship_pesto.jpg';

gg_BgImgRatio[1] = 1868/1200;
gg_BgImgRatio[2] = 1800/1196;
gg_BgImgRatio[3] = 1800/1252;
gg_BgImgRatio[4] = 1800/1196;
gg_BgImgRatio[5] = 1800/1196;
gg_BgImgRatio[6] = 1800/1238;

function setBGImage() {
	
//				$('#background').css('filter', 'alpha(opacity=100)');

	
		var ll_newItem = Math.floor(Math.random()*gg_numItem) + 1;
	
//	gg_currItem = gg_currItem + 1;
	
	if (ll_newItem == gg_currItem)
		gg_currItem = gg_currItem + 1;
	else
		gg_currItem = ll_newItem;
	
	if (gg_currItem > gg_numItem)
		gg_currItem = 1;
	
	var ob_imgCloseImg = new Image(); 
	$(ob_imgCloseImg)
		.load (function () {			
			bgWidth=$(window).width();
			bgHeight=bgWidth/gg_BgImgRatio[gg_currItem];
			if(bgHeight<$(window).height()){
				bgHeight=$(window).height();
				bgWidth=bgHeight*gg_BgImgRatio[gg_currItem];
			}
			
			
			
			if( $.browser.msie && $.browser.version < 7 ){	
				$("#background-container").height($('#pagecell').height())
			}
			$("#background img").width(bgWidth).height(bgHeight);
			$("#background img").attr('src',gg_BgImg[gg_currItem]);			
			
			return;

			var ll_NewImg = $('<img>');
			ll_NewImg.css({
				position:'absolute',
				left:'0px',
				top:'0px'
//				'z-index':'-1'
			});
			ll_NewImg.width(bgWidth).height(bgHeight);
			ll_NewImg.attr('src',gg_BgImg[gg_currItem]);		
			
//			var ll_NewDiv = $('<div>');
//			ll_NewDiv.css({
//				position:'fixed',
//				left:'0px',
//				top:'0px',
//				'z-index':'-1'
//			});
			
			var ll_NewDiv = $('#background').clone();
			ll_NewDiv.css({
				'z-index':'-1'
			});


			ll_NewImg.appendTo(ll_NewDiv);
			ll_NewDiv.appendTo('#background-container');

					$('#backgroundImg').attr('src',ll_NewImg.attr('src')) 
					$('#backgroundImg').width(bgWidth).height(bgHeight);
//					$('#backgroundImg').css({
//						width:ll_NewImg.css('width'),
//						height:ll_NewImg.css('height')
//					});

					$('#background').empty().remove();
					ll_NewDiv.attr('id','background');
					ll_NewDiv.css('z-index','1');
					ll_NewImg.attr('id','backgroundImg');





//			$('#background').animate(
//				{opacity: 0},
//				1000,
//				function(){
//					$('#backgroundImg').attr('src',ll_NewImg.attr('src')) 
//					$('#backgroundImg').width(bgWidth).height(bgHeight);
////					$('#backgroundImg').css({
////						width:ll_NewImg.css('width'),
////						height:ll_NewImg.css('height')
////					});
//
//					$('#background').empty().remove();
//					ll_NewDiv.attr('id','background');
//					ll_NewDiv.css('z-index','1');
//					ll_NewImg.attr('id','backgroundImg');
//
//
//
////					$('#backgroundImg').show();
////					ll_NewImg.css('display','none');
////					ll_NewImg.empty().remove();
//				}
//			);
//			
			


		})
	  .attr('src', gg_BgImg[gg_currItem]);
	  
	  
}

$(document).bind('ready',function()
{
		
		setBGImage();
		setInterval(function(){
	  	setBGImage();
	  },10000);
	  
})

$(window).bind('resize',function()
{
	bgWidth=$(window).width();
	bgHeight=bgWidth/ratio;
	if(bgHeight<$(window).height()){
		bgHeight=$(window).height();
		bgWidth=bgHeight*ratio;
	}
	$("#background img").width(bgWidth).height(bgHeight);		
	if( $.browser.msie && $.browser.version < 7 ){		
		scrollTop = $(window).scrollTop();	
//			$('#background-container').css('top','0px')
		$("#background-container").height($(window).height())
		$('#background-container').css('top',scrollTop + 'px')
	}		
});
