﻿/// <reference name="MicrosoftAjax.js" />
/// <reference name="MicrosoftAjaxWebForms.js" />
/// <reference path="jquery-1.3.2.js" />
/// <reference path="jQueryWatermark.js" />

var autoPlay = true;
var homeImages = null;
var curImage = -1;
var homeInterval = null;
var incrementDelay = 8000;
var areaId = 1;

$(document).ready(function() {
	//Disable Main Image if a menu is clicked to prevent request from being cancelled
	$('a').click(function(e) {
		if (homeInterval !== null) {
			window.clearTimeout(homeInterval);
		}
	});

	var svc = new AlevaHealth.Web.HealthyLegs.HomeImageService();
	svc.GetHomeImages(areaId, getHomeImages_complete);


	var pos = $('.find-form').position();
	$('.ajaxBackgroundFilter').css({ 'top': pos.top, 'left': pos.left });
	$('.ajaxLoading').css({ 'top' : pos.top + 81, 'left' : pos.left + 65});
});

function getHomeImages_complete(results) {
	homeImages = results;
		
	$('#homeImageContainer').load(function() {
		$(this).fadeIn('slow');
	});

	// Finally start the display cycle
	showImage(0);

	if (autoPlay) {
		homeInterval = window.setTimeout(incrementHomeImage, incrementDelay);
	}
}

function incrementHomeImage() {
	if (curImage < (homeImages.length - 1)) {
		showImage(curImage + 1);
	} else {
		showImage(0);
	}

	homeInterval = window.setTimeout(incrementHomeImage, incrementDelay);
}

function showImage(index) {
	
	var doFadeOut = (curImage >= 0);

	var homeimage = $('#homeImageContainer');

	if (doFadeOut) {
		homeimage.fadeOut('slow', function() {
			$(this).hide('normal', function() {
			$(this).html(homeImages[index].Output).fadeIn('slow');
			})
		});
	} else {
		homeimage.html(homeImages[index].Output);
	}

	var prevIndex = curImage;
	curImage = index;
}


if (typeof (Sys) !== 'undefined')
	Sys.Application.notifyScriptLoaded();
if(typeof(Sys)!=='undefined')Sys.Application.notifyScriptLoaded();