var p;
var next;
function fetch_comments_cb(comments)
{
	document.getElementById("comment-list").innerHTML = comments;
}

function fetch_comments()
{
	x_fetch_comments(next,fetch_comments_cb);
}

function fetch_ideas_cb(ideas)
{
  document.getElementById("idea-list").innerHTML = ideas;
}

function fetch_ideas()
{
  x_fetch_ideas(next,fetch_ideas_cb);
}

function idea_cb(returns)
{
	if (/Error:/.exec(returns))
		alert(returns);
	else
		alert("Thank you!");
}

function idea()
{
	var porn;
	var real;
	porn = document.getElementById("porn-world").value;
	real = document.getElementById("real-world").value;
	name = document.getElementById("idea-user-name").value;
	email = document.getElementById("idea-user-email").value;
	if (porn == "" || real == "")
	{
		alert("Missing fields");
		return false;
	}
	sajax_request_type = "POST";
	x_add_idea(porn,real,name,email,idea_cb);
	document.getElementById("real-world").value = "";
	document.getElementById("porn-world").value = "";
}

function comment_cb(returns)
{
	if (/Error:/.exec(returns))
	{
		alert(returns);
	}
	else
	{
		document.getElementById("user-comment").value = "";
		fetch_comments();
		alert("Thanks!");
	}
}

function comment()
{
	var comment;
	var name;
	var email;
	var error = "";
	email = document.getElementById("user-email").value;
	name = document.getElementById("user-name").value;
	comment = document.getElementById("user-comment").value;
	if (comment == "")
		error += "You need to add a comment!\n";
	if (name == "")
		error += "Please include your Name.\n";
	if (email == "")
		error += "Please include your Email address.\n";
	if (error != "")
	{
		alert(error);
		return false;
	}
	sajax_request_type = "POST";
	x_add_line(email,name,comment,next,comment_cb);

}

$(document).ready(function()
{
	var slide;
	var simage;
	var image_url = new Array();
	var previous_Image = new Image();
	var next_Image = new Image();
	var cards = new Image();
	var card = new Array();

	init();
	function init()
	{
		p = 0;
		next = window.location.hash;
		$("#idea-submit").hide();
		$("#about").hide();
		$("#tshirt").hide();
		$("#submissions").hide();

		image_url[0] = "cards/facial.png";
		image_url[1] = "cards/clitoris.png";
		image_url[2] = "cards/hair.png";
		image_url[3] = "cards/anal.png";
		image_url[4] = "cards/positions.png";
		image_url[5] = "cards/spit.png";
		image_url[6] = "cards/talk.png";
		image_url[7] = "cards/finger.png";
		image_url[8] = "cards/gag1.png";
		image_url[9] = "cards/heart.png";

		var i = 0;
		for(i=0; i<image_url.length; i++)
		{
			a = /cards\/(\w+).png/;
			a = a.exec(image_url[i]);
			card[i] = a[1];
		}
		i = 0;
		for(i=0; i<image_url.length; i++)
		{
			cards.src = image_url[i];
		}

		if ( next == "" )
		{
			next = card[p];
		}
		else
		{
			var re = /#/;
			next = next.replace(re,"");
			for(i=0; i<card.length; i++)
			{
				if (next == card[i])
					p = i;
			}
		}

		simage = document.getElementById('slide-image');
		$(simage).attr('src',image_url[p])
		$(simage).css( "margin-left", "60").hide().animate({ marginLeft: 10, opacity: 'show'}, "slow");
		$("#comment").hide().animate({ opacity: 'show'}, 2000);
		document.title = "Make Love Not Porn :: Porn World vs. Real World :: "+card[p];
		fetch_comments();
		return false;
	}
	$("#slide-image").click(function()
	{
		if ( p >= image_url.length-1 )
			p = 0;
		else
			p++;

		next = card[p];
		window.location.hash = next;
		document.title = "Make Love Not Porn :: Porn World vs. Real World :: "+next;
		$(simage).attr('src',image_url[p]).css( "margin-left", "60").hide()
		$.ajax({
			complete: function() {
				$(simage).animate({ marginLeft: 10, opacity: 'show'}, "slow"); 
				$("#comment").hide();
				fetch_comments();
				$("#comment").animate({ opacity: 'show'}, 2000);
				next_Image.src = image_url[p >= (image_url.length-1)? 0 : (p+1)];
				previous_Image.src = image_url[p <= (0)? (image_url.length-1) : (p-1)];
			}
		});
		return false;
	});
	$("#prev-arrow").click(function()
	{
		if ( p <= 0 )
			p = (image_url.length-1);
		else
			p--;

		next = card[p];
		window.location.hash = next;
		document.title = "Make Love Not Porn :: Porn World vs. Real World :: "+next;
		$(simage).attr('src',image_url[p]).css( "margin-left", "-60").hide()
		$.ajax({
			complete: function() {
				$(simage).animate({ marginLeft: 10, opacity: 'show'}, "slow"); 
				$("#comment").hide();
				fetch_comments();
				$("#comment").animate({ opacity: 'show'}, 2000);
				next_Image.src = image_url[p >= (image_url.length-1)? 0 : (p+1)];
				previous_Image.src = image_url[p <= (0)? (image_url.length-1) : (p-1)];
			}
		});
		return false;
	});
	$("#next-arrow").click(function()
	{
		if ( p >= image_url.length-1 )
			p = 0;
		else
			p++;

		next = card[p];
		window.location.hash = next;
		document.title = "Make Love Not Porn :: Porn World vs. Real World :: "+next;
		$(simage).attr('src',image_url[p]).css( "margin-left", "60").hide()
		$.ajax({
			complete: function() {
				$(simage).animate({ marginLeft: 10, opacity: 'show'}, "slow"); 
				$("#comment").hide();
				fetch_comments();
				$("#comment").animate({ opacity: 'show'}, 2000);
				next_Image.src = image_url[p >= (image_url.length-1)? 0 : (p+1)];
				previous_Image.src = image_url[p <= (0)? (image_url.length-1) : (p-1)];
			}
		});
		return false;
	});
	$("#idea-box").click(function()
	{
		$("#image").hide()
		$("#comment").hide();
		$("#idea-box").hide();
		$("#navigation").hide();
		$("#about").hide();
		$("#tshirt").hide();
		$("#submissions").hide();
		$("#ts-sticker").hide();
		document.getElementById("comment-list").innerHTML = "";
		$("#idea-submit").animate({ opacity: 'show'}, 2000);
	});
	$("#nav-idea-box").click(function()
	{
		$("#image").hide()
		$("#comment").hide();
		$("#idea-box").hide();
		$("#navigation").hide();
		$("#about").hide();
		$("#tshirt").hide();
		$("#submissions").hide();
		$("#ts-sticker").hide();
		document.getElementById("comment-list").innerHTML = "";
		$("#idea-submit").animate({ opacity: 'show'}, 2000);
	});
	$("#nav-about").click(function()
	{
		$("#image").hide()
		$("#comment").hide();
		$("#idea-box").hide();
		$("#navigation").hide();
		$("#idea-submit").hide();
		$("#tshirt").hide();
		$("#submissions").hide();
		$("#ts-sticker").hide();
		document.getElementById("comment-list").innerHTML = "";
		$("#about").animate({ opacity: 'show'}, 2000);
	});
	$("#nav-submissions").click(function()
	{
		$("#image").hide()
		$("#comment").hide();
		$("#idea-box").hide();
		$("#navigation").hide();
		$("#about").hide();
		$("#tshirt").hide();
		$("#idea-submit").hide();
		$("#ts-sticker").hide();
		document.getElementById("comment-list").innerHTML = "";
		$("#submissions").animate({ opacity: 'show'}, 2000);
	});
	$("#nav-tshirt").click(function()
	{
		$("#image").hide()
		$("#comment").hide();
		$("#idea-box").hide();
		$("#navigation").hide();
		$("#about").hide();
		$("#idea-submit").hide();
		$("#submissions").hide();
		$("#ts-sticker").hide();
		document.getElementById("comment-list").innerHTML = "";
		$("#tshirt").animate({ opacity: 'show'}, 2000);
	});
	$("#ts-sticker").click(function()
	{
		$("#image").hide()
		$("#comment").hide();
		$("#idea-box").hide();
		$("#navigation").hide();
		$("#about").hide();
		$("#idea-submit").hide();
		$("#submissions").hide();
		$("#ts-sticker").hide();
		document.getElementById("comment-list").innerHTML = "";
		$("#tshirt").animate({ opacity: 'show'}, 2000);
	});
	$("#from-submit").click(function()
	{
		fetch_comments();
		$("#image").animate({ opacity: 'show'}, 2000);
		$("#comment").animate({ opacity: 'show'}, 2000);
		$("#idea-box").animate({ opacity: 'show'}, 2000);
		$("#navigation").animate({ opacity: 'show'}, 2000);
		$("#idea-submit").hide()
	});
});