var didYouKnowDelay = 8000;


/* video */

function playVideo() {
	$("div.ivideo_big").fadeIn(300);

	return false;	
}

function closeVideo() {
	$("div.ivideo_big").fadeOut(200);

	return false;
}

/* screenshots */

function lightboxIn() {

	return false;
}

function lightboxOut() {

	return false;
}

/* didyouknow */

var currentFact = 0;

var factColors = new Array (
	"#7ad8ff",
	"#0a94ff",
	"#43bc13",
	"#97dd00",
	"#d9ae00",
	"#d98000",
	"#ea277a",
	"#bf03a7",
	"#c662ff"
);

function pushDidYouKnow() {
	var newFact = facts[(currentFact++) % facts.length];
	var newColor = factColors[Math.floor(Math.random()*factColors.length)];
	var newDidYouKnow = $("<div>"+newFact+"</div>").css("color", newColor);
	
	$("div.didyouknow div").fadeOut(300);
	$("div.didyouknow").append(newDidYouKnow.fadeIn(500));
}

/* LIGHTBOX */

function showpopup() {
	$('#overlay').css('height', $(document).height()+'px');
	$('#overlay').fadeIn(200, function(){$('#popup').fadeIn(300)});
	return false;
}

function hidepopup() {
	$('#popup').fadeOut(100, function(){$('#overlay').fadeOut(200)});
	return false;
}

function syncBigImage() {
	$("#popup img.big_pic").attr("src", $("#popup p a.on img").attr("alt"));
}

function popupSetup() {

	$("div.screens a").click(showpopup);
	$("#overlay").click(hidepopup);

	$("#popup p a").click(
		function() {
			$("#popup p a").removeClass("on");
			$(this).addClass("on");
			syncBigImage();
			
			return false;
		}
	);
	
	$("#popup a.next").click(
		function () {
			var current = $("#popup p a.on");
			var newSelection = current.next();
			if (newSelection.size() == 0)
				newSelection = $("#popup p").children().eq(0);

			current.removeClass("on");
			newSelection.addClass("on");
			syncBigImage();

			return false;
		}
	);

	$("#popup a.prev").click(
		function () {
			var current = $("#popup p a.on");
			var newSelection = current.prev();
			if (newSelection.size() == 0)
				newSelection = $("#popup p").children().eq($("#popup p").children().size()-1);
			
			current.removeClass("on");
			newSelection.addClass("on");
			syncBigImage();

			return false;
		}
	);

}

$(document).ready(
	function () {
	
		$("div.ivideo div.content a").click(playVideo);
		$("div.ivideo_big a.close").click(closeVideo);
	
		$("div.screens").hover(
			function () {
				$(this).find("a").fadeIn(300);
			},
			function () {
				$(this).find("a").fadeOut(300);
			}
		);
		
		$("div.screens a").click(lightboxIn);
		
		if ($("div.didyouknow").size()) {
			pushDidYouKnow();
			setInterval("pushDidYouKnow()", didYouKnowDelay);
		}
		
		$("ul.questions li").mouseover(
			function (e) {
				$(this).find("span.answer").fadeIn(300);
			}
		);
				
		popupSetup();


	    /* Questions form */
	    
	    $("form#words").submit(
	    	function() {
	    		var lang = $("select#lang").val();
	    		var question = escape($("input#question").val());
	    		var theme = escape($("input#theme").val());
	    		var answer = escape($("input#answer").val());
	    		var username = escape($("input#username").val());
	    	
	    		$("div#qpostresult").text("Posting the question…");
	    		$("div#qpostresult").load("/questions_post.php?lang="+lang+"&theme="+theme+"&question="+question+"&answer="+answer+"&username="+username,
	    			function () {
	    				if ($("div#qpostresult").find('span.yes').size() > 0) {
	    					$("input#question").val('').focus();
	    					$("input#theme").val('');
	    					$("input#answer").val('');
	    				}
	    			}	    		
	    		);
	    	
	    	
	    		return false;
	    	}
	    );
	
	}
);