$(function() {	
	/* set off the FM rss reader */
	getRSSFeed();
	
	// Get the twitter feed
	getTwitterFeed();

	/* Set off the countdown timer */
	var nfdday = new Date(2011, 11-1, 23, 19);

	
	$('#defaultCountdown').countdown({
		until: nfdday, 
		format: 'YDHMS',
		layout: '<div id="t7_timer">'+
			'<div id="t7_vals">'+
				'<div id="t7_y" class="t7_numbs">{ynn}</div>'+
				'<div id="t7_d" class="t7_numbs">{dnnn}</div>'+
				'<div id="t7_h" class="t7_numbs">{hnn}</div>'+
				'<div id="t7_m" class="t7_numbs">{mnn}</div>'+
				'<div id="t7_s" class="t7_numbs">{snn}</div>'+
			'</div>'+
			'<div id="t7_labels">'+
				'<div id="t7_yl" class="t7_labs">years</div>'+
				'<div id="t7_dl" class="t7_labs">days</div>'+
				'<div id="t7_hl" class="t7_labs">hours</div>'+
				'<div id="t7_ml" class="t7_labs">mins</div>'+
				'<div id="t7_sl" class="t7_labs">secs</div>'+
			'</div>'+
			'<div id="t7_timer_over"></div>'+
		'</div>'
		});	
	
	
	$("#q_email").keyup(function(){
		
		if(validateEmail($(this).val())){			
			// Make the text box green			
			$(this).removeClass('normal').removeClass('wrong').addClass('correct');
		}
		else{
			// make the text box red
			$(this).removeClass('normal').removeClass('correct').addClass('wrong');			
		}
	});
	
	$("#q-sign-img").live('click', function(){
		var txt=$("#q_email").val();
				
		if(!validateEmail(txt))
		{
			alert('Invalid Email Address');
			return;
		}
		
		// Need to swap the html with an image, but for now I'll just put text
		// $("#q-button-holder").html("<img src='/template/images/spin.gif'>");
		
		$.post('/users/q-sign.php',{q_email:txt},function(result){
			$("#q-sign-outer").html(result);
		});
	});

	$("#telegraph_email").keyup(function(){		
		if(validateEmail($(this).val())){		
			// Make the text box green			
			$(this).removeClass('normal').removeClass('wrong').addClass('correct');
		} else {
			// make the text box red
			$(this).removeClass('normal').removeClass('correct').addClass('wrong');			
		}
	});
	
	$("#telegraph-img").live('click', function(){
		var txt=$("#telegraph_email").val();
				
		if(!validateEmail(txt))
		{
			alert('Invalid Email Address');
			return;
		}
		
		$.ajax({
			url: "/ajax/telegraph.php",
			data: "email=" + txt,
			success: function(html){				
				$('#telegraph_text').html(html);
				$("#telegraph_stuff").html("<div id='telegraph_link'><p>Click <a href=''>here</a> to read T&amp;Cs</p></div>");
			}
		});
	});
	
	$("#btnPrice").click(function(e){
		// Now the button has been clicked, let's submit the membership number and email address to the ajax code and make sure this is a valid PCG member.

		// alert('got here!');
		
		// Get the values that have been entered
		var membernum = $("#txtMember").val();
		var email = $("#txtEmail").val();
				
	 	$.ajax({
			url: "/ajax/checkmembership.php",
			data: "membernumber=" + membernum + "&email=" + email,
			success: function(html){
				$('#memberprice').html(html);
			}
		});
	});
	
	if( $('#mycarousel').length > 0) {
		$('#mycarousel').jcarousel({
	        // Configuration goes here
			auto: 5,
	        wrap: 'last',
	        initCallback: mycarousel_initCallback
	    });
	}    
})

function mycarousel_initCallback(carousel)
{
    // Disable autoscrolling if the user clicks the prev or next button.
    carousel.buttonNext.bind('click', function() {
        carousel.startAuto(0);
    });

    carousel.buttonPrev.bind('click', function() {
        carousel.startAuto(0);
    });

    // Pause autoscrolling if the user moves with the cursor over the clip.
    carousel.clip.hover(function() {
        carousel.stopAuto();
    }, function() {
        carousel.startAuto();
    });
};

function validateEmail(txtEmail){
	var filter = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
	if(filter.test(txtEmail)){
		return true;
	}
	else{
		return false;
	}
}
