var firstPlay = true;
var headerIndex = 0;
var headerDirectory = 'images/header/';
var autoplayInterval = null;
var isAutoplaying = true;
var swooshTimeout = null;
var contentTimeout = null;
var waitInterval = null;
var Header = DUI.Class.create({
    init: function(c, img, url) {
        this.content = c;
        this.image = img;
		this.url = url;
    },
    debug: function() {
        alert('content: ' + content + ', image: ' + image);
    }
});
function newImage(s) {
    var image = new Image();
    image.src = s;
    return image;
}
var headerData = new Array(
    new Header(
        '\
        <h2>Service. Experience. Value.</h2>\
		<p>For over 20 years, Assist-2-Sell has been serving the real estate needs of people just like you.</p>\
		\
		<p>If you are thinking of buying or selling a home, you have found the right place to start your journey...</p>\
        ',
        newImage(headerDirectory+'header1.jpg'),
		'UserArea/CommonPages/SiteContentData.aspx?SiteContentType=C4sLtAbge6dpFeiVg2G7lXS3jMXKy5B8ovD1sH4VFPA=&NewsFlag=aShXMEVROxa21CrlAw792w=='
    ),
    new Header(
        '\
        <h2>Selling a Home?</h2>\
		<p>Whether you&rsquo;re in a seller&rsquo;s market, a buyer&rsquo;s market, or a foreclosure market, Assist-2-Sell&rsquo;s fully licensed and experienced brokers will guide you to get the job done.</p>\
		<br />\
		<p>...and save you money along the way...</p>\
        ',
        newImage(headerDirectory+'header2.jpg'),
		'UserArea/CommonPages/SiteContentData.aspx?SiteContentType=gTV3kWEzna3U9ooV$$yvbuFk$$mx$$5qo4pTZwj8KIlPYzm9kdPA41Xzp0rxuHW011ax9gNMvLzUxBIK6I1bAW5Kw==&NewsFlag=aShXMEVROxa21CrlAw792w=='
    ),
    new Header(
        '\
        <h2>Buying a Home?</h2>\
		<p>Assist-2-Sell real estate professionals will focus on every detail of your needs, and provide you with information and the right tools to keep you informed of the best home buys on the market.</p>\
        ',
        newImage(headerDirectory+'header3.jpg'),
		'UserArea/CommonPages/SiteContentData.aspx?SiteContentType=Fqx18e4xKorja$$GNJR2vZVX+KjX4Js+AEWQTIEDqwGT4rXbm0mL+qmDovMrTy9gGVKRP8xvIK9QpwTwqPDDt$$g==&NewsFlag=aShXMEVROxa21CrlAw792w=='
    )/*,
    new Header(
        '\
		<p>Assist-2-Sell Real Estate Professionals will focus on every detail of your needs. And provide you with information and the right tools to keep you informed of the best home buys on the market.</p>\
        \
        <p>In today&rsquo;s complex real estate market, you need every advantage available...</p>',
        newImage(headerDirectory+'header4.jpg'),
		'UserArea/OfficeMap.aspx'
    )*/
);

function nextHeader() {
    switchHeader((headerIndex == headerData.length - 1) ? 0 : headerIndex + 1);
}
function previousHeader() {
    switchHeader((headerIndex == 0) ? headerData.length - 1 : headerIndex - 1);
}
function switchHeader(index) {
    if (headerIndex != index) {
        headerIndex = index;
        $('#headerNavigation img:not(:first, :last)').each(function(i) {
            this.src = 'images/header/' + ((i != headerIndex) ? 'off' : 'on') + '.png';
        });
        $('#headerImage').stop().animate({ opacity: 0 }, { duration: 250, queue: false });
        $('#headerContent').stop().animate({ opacity: 0 }, { duration: 250, queue: false, complete: showHeader });
		$('#headerSwoosh1, #headerSwoosh2').stop().animate({ right : -297 }, { duration : 250, queue : false });
		$('#start_button').stop().animate({ opacity : 0 }, { duration : 50, queue : false });
    }
}
function generateNavigation() {
    for (var i = 0; i < headerData.length; i++) {
        $('#headerNavigation img:last').before('<img src="' + headerDirectory + ((i == headerIndex) ? 'on' : 'off') + '.png" width="9" height="9" alt="" />');
    }
}
function showHeader() {
	clearTimeout(swooshTimeout);
	clearTimeout(contentTimeout);
	if (!headerData[headerIndex].image.complete) {
	    waitInterval = setInterval(waitComplete, 100);
	    return;
	} else {
	    var opacity = $('#headerImage').css('opacity');
	    $('#headerImage').replaceWith('<img id="headerImage" src="' + headerData[headerIndex].image.src + '" />');
	    $('#headerImage').css('opacity', opacity);
	}
	$('#headerImage').animate({ opacity: 1 }, { duration: 2000, queue: false });
	$('#headerSwoosh1, #headerSwoosh2').stop().css({ top : 297, right : '0px' });
	$('#headerSwoosh1').animate({ top : 47 }, { duration : 750, queue : false, easing : "easeOutQuad" });
	swooshTimeout = setTimeout(function() {
		$('#headerSwoosh2').animate({ top : 66 }, { duration : 750, queue : false });
	}, 200);
	contentTimeout = setTimeout(function() {
		$('#headerContent').css({ top : -30 }).html(headerData[headerIndex].content).animate({ opacity: 1 }, { duration: 1500, queue: false });
		$('#headerContent').animate({ top : 0 }, { duration : 1000, queue : false, easing : "easeOutQuad", complete : function() {
			$('#start_button').css('opacity', 0).attr('href', headerData[headerIndex].url).animate({ opacity : 1 }, { duration : 1000, queue : false });
		} });
	}, 500);
}
function waitComplete() {
    if (headerData[headerIndex].image.complete) {
        clearInterval(waitInterval);
        showHeader();
    }
}
function clickHeader(obj) {
    switchHeader($(obj).data('index'));
}
function setAutoplay() {
	if(isAutoplaying)
		enableAutoplay();
	else
		disableAutoplay();
}
function enableAutoplay() {
	isAutoplaying = true;
	$('#autoplay').html('autoplay on');
	autoplayInterval = setInterval(nextHeader, 11000);
}
function disableAutoplay() {
	isAutoplaying = false;
	$('#autoplay').html('autoplay off');
	clearInterval(autoplayInterval);
}
function toggleAutoplay() {
    if (isAutoplaying)
        disableAutoplay();
    else
        enableAutoplay();
}
/*
 *  DOM Ready
 */
$(document).ready(function() {
    generateNavigation();
    showHeader();
    $('#headerNavigation').stop().animate({ bottom: 0 }, { duration: 1000, queue: false });
    $('#headerNavigation img:not(:first, :last)').each(function(i) {
        $(this).data('index', i);
        $(this).click(function() { disableAutoplay(); clickHeader(this) });
    });
    $('#headerNavigation img:first').click(function() { disableAutoplay(); previousHeader() });
    $('#headerNavigation img:last').click(function() { disableAutoplay(); nextHeader() });
    setAutoplay();
    $('#autoplay').click(toggleAutoplay);
});