$(document).ready(function(){

	$('body').localScroll({
		duration: 200
	});


	$("#features").hide();
	
	$("#features li a").each(function(){
		var imgUrl = $(this).attr('rel');
		var postUrl = $(this).attr('href');
		var altText = $(this).text();
		
		var img = new Image();

		$('#feature_area').addClass('loading');

		$(img)		
			.load(function(){
				$(this).hide();
				
				var link = document.createElement('a');
				$(link)
					.attr('href', postUrl)
					.attr('alt', altText)
					.append(this);
				
				if(postUrl == '#')
					$(link).css({cursor: 'default'});
				
				$('#feature_area')
					.removeClass('loading')
					.append(link);
					
				$(this).fadeIn();
				
				$(this).css({
					position: 'absolute',
					left: '50%',
					marginLeft: (-$(this).width() / 2) + "px"

				});
				
			})
			.attr('src', imgUrl)
			
			
		$("#authors a").each(function(){
			var url = window.location.toString();
			
			var href = $(this).attr('href');
			if(url.indexOf(href) >= 0){
				$(this).prev().attr('checked', 'checked');
			}
		});
		
		$("#authors input").change(function(){
			window.location = $(this).next().attr('href');
		})
	});
	
	/*
	
	$(".page-item-4 a").click(function(){
		return false;
	})
	
	$(".page-item-4 a").hover(function(){
		$("#nav .current_page_item").removeClass('current_page_item').addClass('current_page_item_bak');
		$("#gameMenu").fadeIn('fast');
		$(".page-item-4").addClass('current');
	});
	
	$("#feature_area, #nav li:not(.page-item-4)").hover(function(){		
		$(".page-item-4").removeClass('current');
		$("#nav .current_page_item_bak").removeClass('current_page_item_bak').addClass('current_page_item');
		$("#gameMenu").fadeOut('fast');

	});
	
	*/
	
	
	$("#gameMenu li").click(function(){
		var url = $(this).children('a').first().attr('href');
		window.location = url;
	});
	
	$("a.button span.current").parent().addClass('current');
	
	
	
	
	
	
	
	$("#cp_footer_contact_us textarea, #cp_footer_contact_us input").focus(function(){
		$(this)
			.attr('value', '')
			.unbind('focus');
	
	})
	
	
	$("#cpml_email").focus(function(){
	
		if($(this).attr('value') == "enter your email here")
			$(this).attr('value', '');
	
	})
	
	$("#cpml_email").blur(function(){
	
		if($(this).attr('value') == '')
			$(this).attr('value', "enter your email here");
	
	})
	
					
	$("#cpml_join_btn").click(function(){
		$("#cpml_join").submit();
	})
		
	$('.cpml_update').hide();
	
	var options = {
		target: '#cpml_target',
		
		beforeSubmit: function(arr, $form, options){

			var email = $('#cpml_email').attr("value");

			if(!isValidEmail(email)){
				alert("Please enter a valid email address.");
				return false;
			} else {
				$('#cpml_submit').hide();
				$('.cpml_update').show();
				_kmq.push(['identify', email]);
				return true;
			}
			
		},
		
		success: function(responseText){
			$('.cpml_update').hide();
			$('#cpml_submit').show();
		}
	};
	
	$("#cpml_join").ajaxForm(options);
	
	
	
	
	
	
	$("#cpcs_send_btn").click(function(){
		$("#cp_footer_contact_us").submit();
	})
		
	$('.cpcs_update').hide();
	
	var footer_options = {
		target: '#cpcs_target',
		
		beforeSubmit: function(arr, $form, options){

			var email = $('#cpcs_email').attr("value");

			if(!isValidEmail(email)){
				alert("Please enter a valid email address.");
				return false;
			} else {
				$("#cpcs_target").html("");
				$('#cpcs_send_btn').hide();
				$('.cpcs_update').show();
				return true;
			}
			
		},
		
		success: function(responseText){
			$('.cpcs_update').hide();
			if(responseText == "Please enter the correct CAPTCHA"){
				$('#cpcs_send_btn').show();
				
			}
		}
	};
	
	$("#cp_footer_contact_us").ajaxForm(footer_options);
	
	
	$('a[ref="trackSocial"]').click(function(){
		_gaq.push(['_trackEvent', "Social Links", $(this).attr("title"), null, null]);
		setTimeout('document.location = "' + $(this).attr('href') +'"', 100);
		return false;
	});

	
	
	$('a[ref="trackEvent"]').click(function(){
		_gaq.push(['_trackEvent', document.title, $(this).attr("title"), null, null]);
		setTimeout('document.location = "' + $(this).attr('href') +'"', 100);
		return false;
	});
	
	
	$(window).scroll(checkScroll);
	checkScroll();
});



function checkScroll(){
	var offset = $("#content").offset();
	var top = offset.top;
	var scrollPosition  = $(window).scrollTop();
	var delta = scrollPosition - top;


	if( delta >=0 ){
		$("#content").css({backgroundAttachment: "fixed"});
	} else {
		$("#content").css({backgroundAttachment: "scroll"});
	}
	
	$("#content").addClass("forceRedraw").removeClass("forceRedraw");
}


function isValidEmail(emailAddress) {
	var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
	return pattern.test(emailAddress);
}
