Cufon.replace('#nav > ul > li > a');
Cufon.replace('#sbtext .title');
Cufon.replace('h1');

$(document).ready(function() {
	$('input.autotab').autotab();
	jQuery.validator.messages.required = "";
	jQuery.validator.messages.email = "";
	jQuery.validator.messages.digits = "";
	jQuery.validator.messages.number = "";
	$('#formDonate').validate({	});
	
	$('#main').css('height', $('#content_mid').height() + 'px');
	
	// Set defaults
	$('#scroller-images ul li:gt(0)').hide();
	$('#scroller-text ul li:eq(0)').addClass('active');
	
	// Initial Calculations
	var height = $('#scroller-text ul li:first').outerHeight();
	var total  = $('#scroller-text ul li').length;
	
	var clone = $('#scroller-text ul li:gt('+(total - 3)+')').clone(); // Clone last two elements
	$(clone).prependTo('#scroller-text ul'); // Add to top of list
	$('#scroller-text ul').css('top', '-' + height + 'px'); // Adjust list so first item is in the middle
	
	$('#scroller-text ul li').live('click', function() {
		var top = $(this).position().top;
		var pos = $('#scroller-text ul').position().top;
		var num = ((Math.abs(top) + pos) / height) + 1;
		
		if(num == 1)
		{
			$('#scroller-up').trigger('click');
		}
		else if(num == 2)
		{
			var url = $('#scroller-images ul li:visible').find('a').attr('href');
			var check = $('#scroller-images ul li:visible').find('a').attr('target');
			
			if(check != '')
			{
				form = document.createElement("form");
				form.method = "GET";
				form.action = url;
				form.target = "_blank";
				document.body.appendChild(form);
				form.submit();
			}
			else
			{
				window.location = url;
			}
		}
		else if(num == 3)
		{
			$('#scroller-down').trigger('click');
		}
	});
	
	// Scroll Up
	$('#scroller-up:not(.disabled)').live('click', function() {
		var items  = $('#scroller-text ul li').length;
		var height = $('#scroller-text ul li:first').outerHeight();
		var pos    = $('#scroller-text ul').position().top;
		var index  = Math.abs(pos) / height;
		var clone = $('#scroller-text ul li:eq('+ (total - 1) + ')').clone(); // Clone last element for smooth scrolling
		$(clone).removeClass('active').prependTo('#scroller-text ul');

		$('#scroller-down, #scroller-up').addClass('disabled'); // Temporarily disable scroll functions
		$('#scroller-text ul').css('top', '-' + ((index + 1) * height) + 'px'); // Re-adjust the top for the newly cloned element
		
		$('#scroller-text ul li.active').removeClass('active'); // Assign active property
		$('#scroller-text ul li:eq(' + (index + 1) +')').addClass('active');
		
		var image = $('#scroller-text ul li:eq(' + (index + 1) +')').find('img').attr('alt'); // Show proper image
		$('#scroller-images ul li:visible').stop().fadeOut(300);
		$('#scroller-images ul li:eq(' + (image - 1) + ')').stop().fadeIn(300);
		
		$('#scroller-text ul').stop().animate({
			top: '-' + ((index) * height) + 'px'
		}, 300, function() {
			$('#scroller-down, #scroller-up').removeClass('disabled');
		});
	});
	
	// Scroll Down
	$('#scroller-down:not(.disabled)').live('click', function() {
		var items  = $('#scroller-text ul li').length;
		var height = $('#scroller-text ul li:first').outerHeight();
		var pos    = $('#scroller-text ul').position().top;
		var index  = Math.abs(pos) / height;
		var clone = $('#scroller-text ul li:eq('+ (index + 1) + ')').clone(); // Clone first element for smooth scrolling
		$(clone).removeClass('active').appendTo('#scroller-text ul');
		
		$('#scroller-down, #scroller-up').addClass('disabled'); // Temporarily disable scroll functions
		
		$('#scroller-text ul li.active').removeClass('active'); // Assign active property
		$('#scroller-text ul li:eq(' + (index + 2) +')').addClass('active');
		
		var image = $('#scroller-text ul li:eq(' + (index + 1) +')').find('img').attr('alt'); // Show proper image
		if(image == total)
		{
			image = 0;
		}
		$('#scroller-images ul li:visible').stop().fadeOut(300);
		$('#scroller-images ul li:eq(' + (image) + ')').stop().fadeIn(300);

		$('#scroller-text ul').stop().animate({
			top: '-' + (height - pos) + 'px'
		}, 300, function() {
			$('#scroller-down, #scroller-up').removeClass('disabled'); // Temporarily disable scroll functions
		});
	});
});

function field_auto_switch(elem)
{
	var max = $(elem).attr('maxlength');
	var val = $(elem).val().length;
	
	if(val == max)
	{
		$(elem).next('input').focus();
	}
}
