﻿/***************************/
//@Author: Adrian "yEnS" Mato Gondelle & Ivan Guardado Castro
//@website: www.yensdesign.com
//@email: yensamg@gmail.com
//@license: Feel free to use it, but keep this credits please!					
/***************************/

$(document).ready(function(){

	//global vars
	var inputUser = $("#nick");
	var inputMessage = $("#message");
	var loading = $("#loading");
	var messageList = $(".content > ul");
	
	//functions
	function updateShoutbox(){
		//just for the fade effect
		messageList.hide();
	//	loading.fadeIn();
		//send the post to shoutbox.php
		$.ajax({
			type: "POST", url: "object/shoutbox.php", data: "action=update",
			complete: function(data){
	//			loading.fadeOut();
				messageList.html(data.responseText);
				messageList.fadeIn(1000);
			}
		});
	}
	//check if all fields are filled
	function checkForm(){
		if(inputUser.attr("value") && inputMessage.attr("value"))
			return true;
		else
			return false;
	}
	
	//Load for the first time the shoutbox data
	updateShoutbox();
	
	//on submit event
	$("#form").submit(function(){
		if(checkForm()){
			var nick = inputUser.attr("value");
			var message = inputMessage.attr("value");
			//we deactivate submit button while sending
			$("#send").attr({ disabled:true, value:"Sending..." });
			$("#send").blur();
			//send the post to shoutbox.php
			$.ajax({
				type: "POST", url: "object/shoutbox.php", data: "action=insert&nick=" + nick + "&message=" + message,
				complete: function(data){
					messageList.html(data.responseText);
					updateShoutbox();
					//reactivate the send button
					$("#send").attr({ disabled:false, value:"Shout it!" });
				}
			 });
		}
		else alert("Please fill all fields!");
		//we prevent the refresh of the page after submitting the form
		return false;
	});
	
	//------------------------------------------shoutbox end---------------------------------------------------
	
	var inputPool = $("#opt");
	var target = $("#pool");
	
	function checkPool(){
		if(inputPool.attr("value"))
			return true;
		else
			return false;
	}
	
	$("#pool").submit(function(){
		if(checkPool()){
			var data = inputPool.attr("value");
			$("#pilih").attr({ disabled:true, value:"Save..." });
			$.ajax({
				type: "POST", url: "object/pool.php", data: "a=insert&d=" + data,
				complete: function(data){
					
					target.hide();
					target.fadeIn();
					target.html('<img src="object/pie.php">' + '<div id="legend">' + data.responseText + '</div>');
				}
			 });
			 	
		}
		else alert("Harus memilih poling!");
		
		return false;
	});
	
	//------------------------------------------shoutbox end---------------------------------------------------
	
	$(".galery").click(function(){
		var canvas = $(".canvas span");
		var images = this.alt;
		
		canvas.hide();
		canvas.html('<img src="file/galery/' + images + '">');
	
		canvas.fadeIn();
		return false;
	});
	
	//------------------------------------------------------carousel---------
	
	
	
});
