/*
	Function will countdown seconds, and show in HH:MM:SS format.
*/
function CountDown(secs,container,msg)  {
	secs=secs-1;
	var rsec = secs;
	if (secs > 0) {
		var strTimer = '';
		if (secs >= 86400) {
			strTimer = Math.floor(secs / 86400) + ' days';
			secs = secs % 86400;
		}
		if (secs >= 3600) {
			strTimer += ' ' + Math.floor(secs / 3600) + ' hours';
			secs = secs % 3600;			
		}else if (strTimer.length > 1) {
			strTimer += ' 0 hours';
		}
		if (secs >= 60) {
			strTimer += ' ' + Math.floor(secs / 60) + ' minutes';
			secs = secs % 60;			
		}else if (strTimer.length > 1) {
			strTimer += ' 0 minutes';
		}
		strTimer += ' ' + secs + ' seconds';
		var obj = document.getElementById(container);
		obj.innerHTML = strTimer;
		window.setTimeout('CountDown('+rsec+',"'+container+'","'+msg+'")', 1000)  
	}else{
		var obj = document.getElementById(container);
		obj.innerHTML = msg;		
	}
}

function reloadCaptcha()
{
	document.getElementById('captcha').src = document.getElementById('captcha').src+ '?' +new Date();
}


/**
 * Holds all the tutorials to show the user.
 */
var tutorialMesages = new Array();

/**
 * Renders the next tutorial in line
 */
function renderTutorials() {
	if (tutorialMesages.length > 0) {
		var tutorialMesage = tutorialMesages.shift();
		renderTutorial(tutorialMesage);
	}
}

/**
 * Renders a tutorial
 */
function renderTutorial(tutorialMesage) {
	var selector = tutorialMesage.selector;
	var txt = tutorialMesage.message;
	var id = tutorialMesage.id;
	
	if ($(selector).length  == 0) {
		renderTutorials();
		return;
	}
	$.scrollTo(selector,500);
	$(selector).expose({ 
            color: '#000000', 
			closeOnClick: false,
            onBeforeLoad: function() {
				$.expose.close(); 
				$('#toolTipInfo').html(txt);
				var left = this.getExposed().offset().left + this.getExposed().width() + 25;
				var top = this.getExposed().offset().top + 50;
				if ($(document).width() < (left + $('#toolTip').width())) {
					left = this.getExposed().offset().left - $('#toolTip').width() - 25;
				}
				$('#toolTip').css('left', (left + 'px') );
				$('#toolTip').css('top', (top + 'px') );
	
				$('#toolTip').hide().fadeIn();
            },  
            onBeforeClose: function() { 
				$.post("/tutorial/view.php", { tutorial_id: id });
				$('#toolTip').show().fadeOut();
            },
			onClose:  function() { 
				// setTimeout('renderTutorials()',500);
				renderTutorials();
			}
        });
}

$(document).ready(function() 
{
	$('.moreContent span:first').hide();
	$('.moreContent a:last').click(function()
	{
		$(this).parent().find('span:first').slideDown('medium');
		$(this).fadeOut('medium');
		return false;
	});
	
	// render tutorials for this page.
	renderTutorials();
	
	$('#toolTip .close').click(function()
	{
		$.expose.close(); 
	});
});


function updateSOpsPrices(obj) {
	var sops = $(obj).val();
	if (parseInt(sops) != sops) {
		return;
	}
	if (sops < 1) {
		return;
	}
	$('.sopsCostDisplayData').each( function () {
		$(this).html(
		   $.number_format( ($(this).prev('input.sopsCostData').val() * sops) , {precision: 0})
		);
	});
}