$(document).ready(function(){

	//global vars
  var inputUser = $("#nick");
	var inputMessage = $("#message");
  var inputTime = $("#timer");
  var loading = $("#loading");
  var loading2 = $("#loading2");
	var msgList = $("#shoutlist");
	var chatList = $("#chat");
	//var userList = $("#online_users");
	var userList = $("#online_user_count");
	var update_interval;
	var update_interval_chat;
  var res;
  var currentTab = 0; 
  
  function openTab(clickedTab) {
    var thisTab = $(".tabbed-box .tabs a").index(clickedTab);
    $(".tabbed-box .tabs li a").removeClass("active");
    $(".tabbed-box .tabs li a:eq("+thisTab+")").addClass("active");
    $(".tabbed-box .tabbed-content").hide();
    $(".tabbed-box .tabbed-content:eq("+thisTab+")").show();
    currentTab = thisTab;
  }

  // Tabbed box
  $(".tabs li:eq(0) a").css("border-left", "none");
  $(".tabbed-box .tabs li a").click(function() {
    openTab($(this)); return false;
  });
  $(".tabbed-box .tabs li a:eq(0)").click()

	//functions

	function startShoutbox(){
		loading.fadeIn();
		$.ajax({
			type: "POST", url: "http://www.dvsckezilabda.hu/shoutbox.php", data: 'ms=' + new Date().getTime() + '&action=start',
			complete: function(data){
				loading.fadeOut();

				  var res_array = data.responseText.split("|");
				  inputTime.attr({value:res_array[0]});
				  $("#shoutlist").prepend(res_array[1]);
          $("#shoutlist").fadeIn();
			}
		});
	}
	
	function updateShoutbox(){
	   loading.fadeIn();
		$.ajax({
			type: "POST", url: "http://www.dvsckezilabda.hu/shoutbox.php", data: "action=update",
			complete: function(data){
			   loading.fadeOut();
				msgList.html(data.responseText);
				msgList.fadeIn(1000);
			}
		});
	}
	
	function updateChatbox(){
		//just for the fade effect
		//msgList.hide();
		
		loading2.fadeIn();
		$.ajax({
			type: "POST", url: "http://dvsckezilabda.hu/shoutbox.php", data: "action=chat",
			complete: function(chat){
				loading2.fadeOut();
				chatList.html(chat.responseText);
				chatList.fadeIn(500);
			}
		});
		
	}
	
	function updateUserbox(){
		loading2.fadeIn();
		$.ajax({
			type: "POST", url: "http://dvsckezilabda.hu/shoutbox.php", data: "action=user",
			complete: function(user){
				loading2.fadeOut();
				userList.hide();
				userList.html(user.responseText);
				userList.fadeIn(1000);
			}
		});
	}

	function stepBox(){
		//just for the fade effect
		//msgList.hide();
		loading.fadeIn();
		//send the post to shoutbox.php

		$.ajax({
			type: "POST", url: "http://dvsckezilabda.hu/shoutbox.php", data: 'ms=' + new Date().getTime() + '&action=step&time=' + inputTime.attr("value") + "",
			complete: function(data){
				loading.fadeOut();
        
				if(data.responseText!=9) {

				  var res_array = data.responseText.split("|");
				  inputTime.attr({value:res_array[0]});

				  $("#shoutlist").prepend(res_array[1]);

				  var active = "#post" + res_array[2];

				  $(active).effect("highlight", {color:"#ffaa00"}, 1000);

				}
			}
		});
	}
	
	//check if all fields are filled
	function checkForm(){
		if(inputUser.attr("value") && inputUser.attr("value")!='név' && inputMessage.attr("value") && inputMessage.attr("value")!='üzenet')
			return true;
		else
			return false;
	}


		
	//CHAT on submit event
	/*	*/
	$("#chatform").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:"Pillanat..." });
			$("#send").blur();
			//send the post to shoutbox.php
			$.ajax({
				type: "POST", url: "http://dvsckezilabda.hu/shoutbox.php", data: "action=insert&nick=" + nick + "&message=" + message,
				complete: function(chat){
					//chatList.html(chat.responseText);
					inputMessage.attr({value:""});
					updateChatbox();
					//reactivate the send button
					$("#send").attr({ disabled:false, value:"Beszólok" });
				}
			 });
		}
		return false;
	});

	$("#nick").click(function(){
		if(inputUser.attr("value")=='név') {
		  inputUser.attr({value:""});
		}
		return false;
	});

	
	$("#message").click(function(){
		if(inputMessage.attr("value")=='üzenet') {
		  inputMessage.attr({value:""});
		}
		return false;
	});

		
	//GÓLTOTÓ on submit event
	/*	*/
	$("#totoform").submit(function(){
		if(checkForm()){}
			$("#totosend").attr({ disabled:true, value:"Pillanat..." });
			$("#totosend").blur();
			$.ajax({
				type: "POST", url: "http://dvsckezilabda.hu/toto.php", data: "f1=" + $("#f1").attr("value") + "&f2=" + $("#f2").attr("value") + "v1=" + $("#v1").attr("value") + "&v2=" + $("#v2").attr("value"),
				complete: function(toto){
				  $("#totoresponse").html(toto.responseText);
					$("#totosend").attr({ disabled:false, value:"OK" });
				}
			 });
		
		return false;
	});
	
  // Modal

	$('a[name=modal]').click(function(e) {//select all the a tag with name equal to modal
		
		e.preventDefault();//Cancel the link behavior
		var id = $(this).attr('href');//Get the A tag
		var maskHeight = $(document).height();//Get the screen height and width
		var maskWidth = $(window).width();
		$('#mask').css({'width':maskWidth,'height':maskHeight});//Set heigth and width to mask to fill up the whole screen
		$('#mask').fadeIn(500);//transition effect	
		$('#mask').fadeTo("slow",0.8);	
		var winH = $(window).height();//Get the window height and width
		var winW = $(window).width();
		$(id).css('top',  winH/2-$(id).height()/2);//Set the popup window to center
		$(id).css('left', winW/2-$(id).width()/2);
		$(id).fadeIn(1000); //transition effect
	});
	
	//if close button is clicked
	$('.window .close').click(function (e) {
		e.preventDefault();//Cancel the link behavior
		$('#mask').hide();
		$('.window').hide();
	});		
	
	$('#mask').click(function () {//if mask is clicked
		$(this).hide();
		$('.window').hide();
	});

	//Load for the first time the shoutbox data
  //updateShoutbox();
	//update_interval = setInterval(updateShoutbox,10000);
	inputTime.attr({value:0});
	//updateChatbox();
	//updateUserbox();
	startShoutbox();
	//update_interval = setInterval(stepBox,10000); //6000
	update_interval = setInterval(updateShoutbox,6290); //6000
	/// update_interval_chat = setInterval(updateChatbox,5300); //4000
	//// update_interval_user = setInterval(updateUserbox,120000); //10000 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
});

 $(document).keyup(function(e) {  
   if(e.keyCode == 13) {  
     $('#mask').hide();  
     $('.window').hide();  
   }  
 }); 

