/*
	Website 23.0 Static (Scott Bradford: Off on a Tangent)
	Copyright (c) 1995-2012, Scott Bradford Creative Enterprises
*/

// establish the ooat object with default/initial values
ooat = {
	refreshCount: 0,
	reverseOrder: true
};

// return a random number (to make IE behave on AJAX reqests)
ooat.rand = function() {
	var rnum = (Math.floor(Math.random()*9999)).toString();
	return rnum;
}

// throw content into the page, as needed.
ooat.throwContent = function(content) {
	jQuery('#content').html(content);
};

ooat.throwAjaxError = function() {
	ooat.throwContent('<p>There has been an error.</p><p id="countdown">Trying again in <span id="countvalue">30</span> seconds...</p>');
	ooat.countDown(30);
};

// perform a countdown to a particular value before refreshing
ooat.countDown = function(time) {
	ooat.iterateValue = time-1;
	ooat.iterateCode = setInterval("ooat.iterateAction()",(1000))
};

ooat.iterateAction = function() {
	if (ooat.iterateValue < 0) {
		clearInterval(ooat.iterateCode);
		jQuery('#countdown').html('Checking...');
		ooat.refreshAction();
	}
	jQuery('#countvalue').html(ooat.iterateValue);
	ooat.iterateValue--;
};

// when we want the content reversed
ooat.flipAround = function() {	
	// actually reverse the list contents (using the jQuery reverseOrder plugin)
	jQuery('.livecontent>ul').each(function(){
		jQuery(this).children('li').reverseOrder();
	});
}

ooat.getLiveContent = function(liveMode) {
	jQuery.ajax({
		type: 'GET',
		url: '/assets/live-content.html?rand='+ooat.rand(),
		dataType: 'html',
		error: ooat.throwAjaxError,
		success: function(data) {
			var dataOutput = '';
			
			if (liveMode == 'liveNow') {
				dataOutput += '<div><h1>Special Live Coverage</h1><p id="countdown" class="live">Refreshing content in <span id="countvalue">60</span> seconds. No need to reload! Newest at <span class="position">Top</span> (<a href="#" id="orderSwitch">switch</a>)</p></p><div class="livecontent">' + data + '</div></div>';
			} else {
				dataOutput += '<div><div class="livecontent">' + data + '</div></div>'
			}
			
			ooat.throwContent(dataOutput);
			
			// correct nested list markup
			jQuery('.livecontent>ul>ul').each(function(){
				jQuery(this).appendTo(jQuery(this).prev('li'));
			});
			
			if (liveMode == 'liveNow') {
				// if we're in reverse-order mode (newest at top), reverse the root list-items
				if (ooat.reverseOrder) {
					ooat.flipAround();
				} else {
					jQuery('.position').html('Bottom');
				}
				
				ooat.countDown(60);
			}
		}
	});
};

// reload all the info from the settings file
ooat.refreshAction = function() {
	jQuery.ajax({
		type: 'GET',
		url: '/assets/ooat-mode.js?rand='+ooat.rand(),
		dataType: 'json',
		error: ooat.throwAjaxError,
		success: function(data) {
			
			if (data.mode == 'fullStop') {
				// handle the 'fullStop' status (site completely down indefinitely)
				ooat.throwContent('<p><em>Off on a Tangent</em> is currently unavailable.</p><p>Please try again later.</p>');
				
			} else if (data.mode == 'maintNow') {
				// handle the 'maintNow' status (maintence is now happening!)
				ooat.throwContent('<p><em>Off on a Tangent</em> is currently down for scheduled maintenance.</p><p id="countdown">Checking status again in <span id="countvalue">120</span> seconds...</p>')
				ooat.countDown(120);
			} else if (data.mode == 'maintDone') {
				// handle the 'maintDone' status (maintenance is complete or not happening)
				if ( ooat.refreshCount == 0 ) {
					// initial page-load; you went here directly when no maint. was happening.
					ooat.throwContent('<p>This is the <em>Off on a Tangent</em> maintenance page.</p><p>Maintenance is now complete. Please visit the <a href="http://www.scottbradford.us/">real site</a>.</p>');
				} else {
					// post page-load; maintenance was happening but it's done now.
					ooat.throwContent('<p>Maintenance is now complete.<p><p>You will be redirected to the <a href="http://www.scottbradford.us/">real site</a> momentarily...</p>');
					setTimeout('window.location="http://www.scottbradford.us/"',5000);
				}
			} else if ((data.mode == 'liveNow') || (data.mode == 'specNote')) {
				// handle the 'liveFeed' status (periodic updates from content file)
				ooat.getLiveContent(data.mode);
			} else if (data.mode == 'liveDone') {
				// handle the 'liveDone'
				if ( ooat.refreshCount == 0 ) {
					// initial page-load; you went here directly when no live coverage was happening.
					ooat.throwContent('<p>This is the <em>Off on a Tangent</em> live coverage page.</p><p>Coverage is now complete. Please visit the <a href="http://www.scottbradford.us/">real site</a>.</p>');
				} else {
					// post page-load; maintenance was happening but it's done now.
					ooat.throwContent('<p>Live coverage is now complete.<p><p>You will be redirected to the <a href="http://www.scottbradford.us/">real site</a> momentarily...</p>');
					setTimeout('window.location="http://www.scottbradford.us/"',5000);
				}
			} else {
				// handle bad data (invalid status mode)
				ooat.throwAjaxError();
			}
			
			ooat.refreshCount++;
		}
	});
	
	
};

// run the first check when the document is ready
jQuery(document).ready(function(){
	ooat.refreshAction();
	
	jQuery('#orderSwitch').live('click', function(){
		if (ooat.reverseOrder) {
			ooat.reverseOrder = false;
			jQuery('.position').html('Bottom');
		} else {
			ooat.reverseOrder = true;
			jQuery('.position').html('Top');
		}
		ooat.flipAround();
		return false;
	});
});
