//
// sustain.js
// Copyright (c) Connect Group Ltd. All rights reserved.
//
// $Id: sustain.js 1 2010-04-16 16:52:50Z paul $
//

$(document).ready(function() {
	var printLink = $('<li class="print"><a href="javascript:window.print();">Print Page</a></li>');
	if ($('div.extra_links ul li.friend').length == 1) {
		printLink.insertBefore($('div.extra_links ul li.friend'));
	} else {
		$('div.extra_links ul').prepend(printLink);
	}

	$('a[rel="external"]').click(function(e) { e.preventDefault(); window.open(this.href); return false; });

	$('input[title!=""]').each(function() {
		$(this).watermark($(this).attr('title'));
	});

	$("#nav li").hover(
		function() { $(this).find('ul:first').css({ visibility: "visible", display: "none" }).show('fast'); },
		function() { $(this).find('ul:first').css({ visibility: "hidden" }); }
	);

	$('div.ticker').vTicker();

	if (typeof(counter) != 'undefined') {
		setTimeout(function() { updateCounter(counter.endValue); }, 1000);
	}
});

//
// The height of an individual character in the number sprites.
//
var characterHeight = 67;

//
// animaterCounter()
// Animates the counter, moving each character to the correct position.
//
function animateCounter() {
	$('div.counter-number').each(function() {
		var value = $(this).attr('value');
		var position = calculateCharacterPosition(value);
		$(this).animate({ top: position + 'px' }, 1500);
	});
}
//
// calculateCharacterPosition(char)
// Calculates the position of the specified character in the number sprites.
// PARAMS
//  char: A character.
// RETURNS
//  The position of the specified character in the number sprites.
//
function calculateCharacterPosition(char) {
	var position = 0;
	var isNumber = !isNaN(char);

	if (isNumber) {
		position = Number(char) * characterHeight;

	} else if (char == ',') {
		position = 10 * characterHeight;
	}

	return position *-1;
}
// function loadticker(ticnum) {
// 	var fticnum = add_commas(ticnum);
// 	var numheight=67;
// 	addticker(fticnum);
// 	if (ticnum && ticnum != 0) {
// 		
// 		var s = String(fticnum);
// 		
// 		for (i=s.length;i>=0; i--)
// 		{
// 			var onum=s.charAt(i);			
// 			var x = $('#num'+i); x.attr('value', onum); if (onum == ',') { x.addClass('comma'); } else { x.removeClass('comma'); }
// 		}
// 		
// 		// $(".counter-number").each( function() {
// 		// 	var nval=$(this).attr("value");
// 		// 	if (!isNaN(nval)) {
// 		// 		var nheight = Number(nval)*numheight*-1;
// 		// 		$(this).animate({ top: nheight+'px'}, 1500 );
// 		// 	} 
// 		// 	if (nval==','){
// 		// 		$(this).animate({ top: '-670px'}, 1500 );
// 		// 	}
// 		//   		});
// 	}
// }

//
// formatAsNumber(value)
// Formats the specified value as a number with comma separators.
// PARAMS
//  value: The value to be formatted.
// RETURNS
//  $value formatted as a number with comma separators.
//
function formatAsNumber(value) {
	value += '';

	var elements = value.split('.');
	var whole = elements[0];
	var decimal = elements.length > 1 ? '.' + elements[1] : '';

	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(whole)) {
		whole = whole.replace(rgx, '$1,$2');
	}

	return whole + decimal;
}
//
// updateCounter(value)
// Updates the counter based on the specified value, resizing if necessary.
// PARAMS
//  value: The value to be displayed in the counter.
//
function updateCounter(value) {
	value = formatAsNumber(value);
	if (value && value != 0) {
		var digits = $('div.counter-number').size();
		var valueLength = String(value).length;
		var difference = valueLength - digits;

		if (difference < 0) {
			$('div.counter-number:gt(' + (valueLength - 1) + ')').remove();
		}

		for (var index = 1; index <= difference; index++) {
			$('div.counter-wrap').append('<div id="num' + (digits + (index - 1)) + '" class="counter-number">&nbsp;</div>');
		}

		for (var index = value.length; index >= 0; index--) {
			var char = value.charAt(index);
			var charElement = $('div.counter-number#num' + index);
			charElement.removeClass('comma').attr('value', char);
			if (char == ',') {
				charElement.addClass('comma');
			}
		}

		animateCounter();
	}
}
