// JavaScript Document
$(document).ready(
	function()
	{
		$('#txtCode').keyup(
			function(event)
			{
				if(event.keyCode == 13)
				getCode();
			}
		);
		
		$('#btnPromoGo').click(
			function()
			{
				getCode();
			}
		); // end code lookup click
		
		$('div#c').click(function() { window.location = 'choice.php'; });
		$('div#i').click(function() { window.location = 'cni/index.php'; });
				
		$('#btnTryAgain').click(
			function()
			{
				$('#promoFail').fadeOut(300,
					function()
					{
						$('#promoAsk').fadeIn(300);
					}
				);
			}
		); // end button click
		
		$('#txtCode').toggleClass('gray').val('Enter it here');
		$('#txtCode').focus(
			function()
			{
				if($(this).val() == 'Enter it here') {
					$(this).val('');
					$(this).toggleClass('gray');
				}
			}
		);
		
		$('#txtCode').blur(
			function()
			{
				thisVal = $.trim($(this).val());
				if(thisVal.length == 0) {
					$(this).val('Enter it here');
					$(this).toggleClass('gray');
				}
			}
		);
		
	} // end document ready function
); // end document ready 

function getCode()
{
	var code = $('#txtCode').val();

	$.post('getcode.php', { code: code}, 
		function(data)
		{
			if(data.found == '1') {
				window.location = 'choice/promotions.php';
			} else { // no matches
				$('#promoAsk').fadeOut(300, 
					function()
					{
						$('#promoFail').fadeIn(300);
					}
				);
			} // end if data found 
		}
	, 'json');
}

function slideSwitch(gallery)
{
	var $active = $(gallery + ' img.active');
	
	if($active.length == 0)
		$active = $(gallery + ' img:last');
		
	var $next = $active.next().length ? $active.next() : $(gallery + ' img:first');
	$active.addClass('last-active');
	
	$next.css({opacity: 0.0})
		.addClass('active')
		.animate({opacity: 1.0}, 1000,
			function()
			{
				$active.removeClass('active last-active');
			}
		);
}

$(function() { setInterval("slideSwitch('#animBox')", 3000); });