// Make the sidebar be as tall as the content next to it

$(function() {
	$('#section_content').bind('resize', function(e) {
		$("#sidebar").height($("#section_content").outerHeight());
	});
	
	$('#content.blog .right').bind('resize', function(e) {
		$('#content.blog .left').height($('#content.blog .right').outerHeight());
	});
	
	$('#section_content').resize();
	$('#content.blog .right').resize();
});


// Homepage slides
$(function() {
	homepageSlideshow($("#slideshow .slides"), '/wp-content/themes/particle/images/homepage/slideshow/toggle.png', 15000, true);
});


function sendRegistration() {

	// clear errors
	$("#signup .input_text").removeClass("error_input");

	if ($("#error").size() <= 0) {
		$("<div id=\"error\"></div>").insertBefore("#main");
	}
	var hasError = false;
	var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;

	var emailaddressVal = $("input[name=beta_email]").val();
	if(emailaddressVal == '') {
		$("#error").html('<p>Please enter your email address.</p>');
		$("input[name=beta_email]").addClass("error_input");
		hasError = true;
	}

	else if(!emailReg.test(emailaddressVal)) {
		$("#error").html('<p>Enter a valid email address.</p>');
		$("input[name=beta_email]").addClass("error_input");
		hasError = true;
	}

	else if ($("input[name=beta_email]").val() != $("input[name=beta_email2]").val()) {
		$("#error").html("<p>E-mail addresses don't match!.</p>");
		$("input[name=beta_email]").addClass("error_input");
		$("input[name=beta_email2]").addClass("error_input");
		hasError = true;
	}

	else if (!$("input[name=beta_realname]").val() ) {
		$("#error").html('<p>Please enter your name.</p>');
		$("input[name=beta_realname]").addClass("error_input");
		hasError = true;
	}

	if(hasError == true) { 
		$("#error").slideDown(function() { setTimeout(function() { $("#error").slideUp('fast') }, 2000); });
		return false;
	}


	$("form#form_beta").attr("action","").submit();
}

// Used to track forward/back navigation between sections			
var current_section = "";

function showSection(section, animate) {

	// hide divs that should be removed when sections load
	$(".section_hide").hide();

	window.location.hash = section;
	if (animate) {
		$("html, body").animate({'scrollTop' : $("#big_boxes").offset().top }, "slow");
	}
	$(".section").hide();
	$("#section-"+section+"").show();

	$(".bigbox").find(".opaque").removeClass("active passive").addClass("passive");
	$("#"+section+"_button").find(".opaque").removeClass("passive").addClass("active");
	current_section = section;
}

/**
* This would help loading images in hidden sections
*/
function load_section_images() {
	$(".section:hidden").css({"position" : "absolute", "visibility" : "hidden"});
	setTimeout(function() {
		$(".section").each(function(e) {
			if ($(this).css("visibility") == "hidden") {
				$(this).css({"visibility":"visible","position":"relative","display":"none"});
			}
		});
		}, 1000);
	}

	function poll_forwardback() {
		if (window.location.hash && current_section && window.location.hash.replace("#", "") != current_section) {
			showSection(window.location.hash.replace("#", ""), true);
		}
	}

/**
* 'Forgot password' scroll functionality
*/
function toggle_forgot(anchor) {
	if ($("#forgot_password").is(":visible")) {
		$("#login_box").animate({'scrollTop' : 0 }, "slow", function() { $("#forgot_password").hide(); });
	}
	else {
		$("#forgot_password").show();
		$("#login_box").animate({'scrollTop' : 135 }, "slow", function() {	});
	}
}

/**
* The 'Read more' functionality
*/
function read_more(e) {
	if (e) {
		if (!$(this).data("original")) {
			$(this).parentsUntil(".list").find(".list_content").slideDown('fast');
			$(this).data("original", $(this).html()).html("Close");
		}
		else {
			$(this).html($(this).data("original"));
			$(this).data("original", null);
			$(this).parentsUntil(".list").find(".list_content").slideUp('fast');
		}
	}
}

/**
* Shows the 'read more' content area for each blog post if it exists.
* @param e - the blog entry element
*/
function blog_more(e, postId) {
	if ($(e).find(".read_more").is(":visible")) {
		$(e).find(".read_more").slideUp("fast");
		$(e).find(".blog_more").html("Read more");
		$(e).find(".post_content").css('display', '');
	}
	else {
		// Either load or use data
		if ($(e).find(".read_more").data("more")) {
			$(e).find(".read_more").html($(e).find(".read_more").data("more"));
			$(e).find(".post_content").css('display', 'none');
			$(e).find(".read_more").slideDown("fast");
			$(e).find(".blog_more").html("Read less");
		}
		else {
			$(e).find(".blog_more").html("Loading...");
			$(e).find(".read_more").load('./?ajaxpost=1&p='+postId, function(responseText) {
				$(e).find(".post_content").css('display', 'none');				
				$(this).slideDown("fast");
				$(this).data("more",responseText);
				$(this).parent().find(".blog_more").html("Read less");
			});
		}
	}
}

$(function() {
	$(".bigbox").hover(function(e) { $(this).addClass('hover'); }, function() { $(this).removeClass('hover'); });
	$(".navbar").click(function() { showSection($(this).attr("id").replace("_button",""), true) });

	if (window.location.hash && document.referrer != window.location) {
		showSection(window.location.hash.replace("#", ""), false);
	}

	// Fading elements go from 50% to 100% when hovered and vice versa
	$(".fading").hover(
		function(e) { $(this).fadeTo(300,1); }, 
		function(e) { $(this).fadeTo(300,0.5); }
	);
	$(".fading").css("opacity",0.7);

	// Active/inactive elements
	$(".active_focus").focus(function(e) { $(this).addClass("active"); });
	$(".active_focus").blur(function(e) { $(this).removeClass("active"); });

	$(".active_hover").hover(
		function(e) { $(this).addClass("active"); },
		function(e) { $(this).removeClass("active"); }
	);

	// "read more"
	$(".list_more").click(read_more);
	$(".list").find(".list_content").hide();

	// Preload images
	load_section_images();

	// Show error if exists
	$("#error").slideDown("fast", function() {
		setTimeout(function() { $("#error").slideUp("fast"); },5000);
	});

	// Show message if exists
	$("#message").slideDown("fast", function() {
		setTimeout(function() { $("#message").slideUp("fast"); },5000);
	});
	// Login bar toggle
	$("#login_toggle").click(function() {
		if ($("#login").is(":visible")) $("#login").slideUp("fast");
		else $("#login").slideDown("fast");
	});

	// Poll for forward-back navigation
	setInterval(poll_forwardback, 250);
});


// Samples

$(function()
{
	$('.sample .top .sample_click').click(function()
	{
		$('.sample .top .sample_holder').css('display','none');
		$('.sample .top .sample_click').css('display','');

		$(this).css('display','none');
		$('.sample_holder', $(this).parent()).css('display','');
	}); 
	
	$('.sample_wide .top .sample_click').click(function()
	{
		$('.sample_wide .top .sample_holder').css('display','none');
		$('.sample_wide .top .sample_click').css('display','');

		$(this).css('display','none');
		$('.sample_holder', $(this).parent()).css('display','');
	}); 

});


// Download page hack
$(function()
{
	$('#content.download #loginform .login-username label').text('Email:');
	$('#content.download #loginform .login-password label').text('Password:');
});

// Documentation page hack
$(function()
{
	$('#features_sidebar a[title="Documentation"]').attr('target', '_blank');
});




