var picdelay = 5000;
var bannerdelay = 5500;
var counter = 0;
var timer = timer2 = null;

function nextImage (forward) {
	var $el = $('#picholder');
	var images = [
		['images/rotatingindexpics/8.jpg', '/residential-services/new-home-installations'],
		['images/rotatingindexpics/11.jpg', '/products-indoorair-filters.php'],
		['images/rotatingindexpics/12.jpg', 'all Govt_of_AB_Energy_Efficiency_Rebates.pdf','_blank'],
		['images/rotatingindexpics/10.jpg', '/maintenancecontracts.php'],
		['images/rotatingindexpics/5.jpg', 'http://www.lennox.com/resources/energycalculator.asp', '_blank'],
		['images/rotatingindexpics/7.jpg', 'http://www.bbb.org/edmonton/business-reviews/heating-and-air-conditioning/weiss-johnson-sheet-metal-ltd-in-edmonton-ab-100269', '_blank']
	];
	
	if (forward == undefined) forward = true;
	var idx = $el.data('index') || 0;
	idx = (idx + (forward?1:-1)) % images.length;
	if (idx == -1) idx = images.length - 1;
	$el.data('index', idx);
	
	var img = new Image();
	img.src = images[idx][0];
	
	$(document.createElement('a'))
		.attr({'href' : images[idx][1], 'target' : (images[idx][2]||'')})
		.css({
			'left'   : (forward?'100%':'-100%'),
			'width'  : '578px',
			'height' : '373px'
		})
		.append(img)
		.appendTo($el)
		.animate({
			'left' : '0%'
		}, 500, function() {
			$(this).prev().remove();
		})
		.prev().animate({
			'left' : (forward?'-100%':'100%')
		}, 500);
	
	clearTimeout(timer);
	timer = setTimeout('nextImage()', picdelay);
}

function nextBanner () {
	var $el = $('#adbanner');
	var images = [
		['images/ads/abithot.jpg', '/residential-services/air-conditioning'],
		['images/ads/wintershallreturn.jpg', '/residential-services/air-conditioning']
	];
	
	var idx = $el.data('index') || 0;
	idx = (idx + 1) % images.length;
	$el.data('index', idx);
	
	var img = new Image();
	img.src = images[idx][0];
	
	$(document.createElement('a'))
		.attr({'href' : images[idx][1], 'target' : (images[idx][2]||'')})
		.css({'left':'-100%'})
		.append(img)
		.appendTo($el)
		.animate({
			'left' : '0%'
		}, 750, function() {
			$(this).prev().remove();
		})
		.prev().animate({
			'left' : '100%'
		}, 750);
	
	clearTimeout(timer2);
	timer2 = setTimeout('nextBanner()', bannerdelay);
}

$(function() {
	// preload images
	var im = ['images/ads/abithot.jpg', 'images/ads/toughtobelieve.jpg',
		'images/rotatingindexpics/1.jpg', 'images/rotatingindexpics/2.jpg',
		'images/rotatingindexpics/3.jpg', 'images/rotatingindexpics/5.jpg', 'images/rotatingindexpics/6.jpg','images/rotatingindexpics/7.jpg'];
	var img = null;
	
	for (var i=0; i < im.length; i++) {
		img = new Image();
		img.src = im[i];
	};
	
	// hook picholder buttons
	$('#picholder .right').click(function() {
		nextImage();
	});
	
	$('#picholder .left').click(function() {
		nextImage(false);
	});
	
	timer = setTimeout('nextImage()', picdelay);
	
	
	// Google Analytics
	 var _gaq = _gaq || [];
	 _gaq.push(['_setAccount', 'UA-17798060-7']);
	 _gaq.push(['_trackPageview']);

	 (function() {
	   var ga = document.createElement('script'); ga.type =
	'text/javascript'; ga.async = true;
	   ga.src = ('https:' == document.location.protocol ? 'https://ssl' :
	'http://www') + '.google-analytics.com/ga.js';
	   var s = document.getElementsByTagName('script')[0];
	s.parentNode.insertBefore(ga, s);
	 })();
});

