/* this script rotates left column ad banners */

$(document).ready(function() {
	// initial load
	initial_ad_load();
	// rotate ads - loop ads 2 more times every 30 secs
	initial_looping();
	// now we loop every 5 mins - (this is deactivated for now per underwriting - ads will rotate 3 times and stop)
	//rotate_zone4();	
	//rotate_zone5();		
});

function initial_ad_load() {
	var adLeft = $('.adserverLeft');	
	adLeft.writeCapture().load('../../lib/adserver/zone4.inc?randval='+ Math.random(), null, function(response, status, xhr) {
		if(status == "success") {
			adLeft.hide().delay(1000).fadeIn(1000);
		}
	});

	var adRight = $('.adserverRight');
	adRight.writeCapture().load('../../lib/adserver/zone5.inc?randval='+ Math.random(), null, function(response, status, xhr) {
		if(status == "success") {
			adRight.hide().delay(1000).fadeIn(1000);
		}
	});
}

function initial_looping() {	
	counter = 0;
	trackLoops = setInterval(initialLoop,30000);

	function initialLoop() {
		counter++;
		
		if(counter<=2) {			
			var adLeft = $('.adserverLeft');
			adLeft.fadeOut(1000, function() {
				adLeft.writeCapture().load('../../lib/adserver/zone4.inc?randval='+ Math.random(), null, function(response, status, xhr) {
					if(status == "success") {
						adLeft.hide().delay(1000).fadeIn(1000);
					}
				});
			});

			var adRight = $('.adserverRight');
			adRight.fadeOut(1000, function() {
				adRight.writeCapture().load('../../lib/adserver/zone5.inc?randval='+ Math.random(), null, function(response, status, xhr) {
					if(status == "success") {
						adRight.hide().delay(1000).fadeIn(1000);
					}
				});
			});			
		}
		else {
			clearInterval(trackLoops);
		}
	}	
}

function rotate_zone4() {		
	var timerId = setInterval(function() {	
		
		var adLeft = $('.adserverLeft');		
		adLeft.fadeOut(1000, function() {
			adLeft.writeCapture().load('../../lib/adserver/zone4.inc?randval='+ Math.random(), null, function(response, status, xhr) {
				if(status == "success") {
					adLeft.hide().delay(1000).fadeIn(1000);
				}
				else {
					clearInterval(timerId);
					// sleep for 10 mins
					setTimeout('rotate_zone4()', 600000);
				}				
			});
		});
	
	}, 300000);
};


function rotate_zone5() {	
	var timerId = setInterval(function() {	
		
		var adRight = $('.adserverRight');		
		adRight.fadeOut(1000, function() {
			adRight.writeCapture().load('../../lib/adserver/zone5.inc?randval='+ Math.random(), null, function(response, status, xhr) {
				if(status == "success") {
					adRight.hide().delay(1000).fadeIn(1000);
				}
				else {
					clearInterval(timerId);
					// sleep for 10 mins
					setTimeout('rotate_zone5()', 600000);
				}				
			});
		});
	
	}, 300000);
};


