// JavaScript Document

$(document).ready(function() {
	  
	  //  Initialize Backgound Stretcher
	  $(document).bgStretcher({
			images: bgImages, imageWidth: 1024, imageHeight: 768, slideShow: false
	  });
	  
	  
	  var bAutoScroll 		= true;
	  var totIncrement		= 0;
	  var increment			= 195;
	  var maxRightIncrement	= increment*(-1 * (nbNewsItems-3));
	  	   
	  //-------------------------------------
	  // EVENTS for the button "previous"
	  	$('#previous').click(function() {
			  	if(totIncrement<0){
					bAutoScroll = false;
					totIncrement = totIncrement+increment;
					$('#myList').animate(
						{left: totIncrement},
						1500
					);
				}		  	  
		 }); 
	 
      //-------------------------------------
	  // EVENTS for the button "next"
  	 	$('#next').click(function() {
			  	 if(totIncrement>maxRightIncrement){
					bAutoScroll = false;
					totIncrement = totIncrement-increment;
					$('#myList').animate(
						{left: totIncrement},
						1500
					);
				}		  	  
		 }); 
		
		totIncrement = maxRightIncrement;
		$('#myList').css("left", totIncrement);

	    setInterval(function(){
			if (bAutoScroll) {
				if(totIncrement<0){
					totIncrement = totIncrement+increment;
					$('#myList').animate(
						{left: totIncrement},
						1500
					);
				}		  	  
			}
		}, 8000); 	
});

function showNextBackgroundImage() {
//----------------------------------
	if (curBgImage < bgImages.length-1) curBgImage++;
	else curBgImage = 0;
	xmlhttp=GetXmlHttpObject();
	if (xmlhttp==null) {
		return;
	}
	var url="/ECMS_CLIENT/configuration/scripts/setbackground.php";
	url=url + "?b=" + bgImages[curBgImage];
	url=url+"&sid="+Math.random();
	xmlhttp.open("GET",url,true);
	xmlhttp.send(null);
	$("#bgstretchimage").attr("src", bgImages[curBgImage]);
	//$("wrapper").bgStretcher.pause();
}

function GetXmlHttpObject() {
//---------------------------	
	if (window.XMLHttpRequest) {
		// code for IE7+, Firefox, Chrome, Opera, Safari
		return new XMLHttpRequest();
	}
	if (window.ActiveXObject) {
		// code for IE6, IE5
		return new ActiveXObject("Microsoft.XMLHTTP");
	}
	return null;
}

