// JavaScript Document

function check_user(){
if($.cookie("Iwish_chat_mode")!= undefined)
{
	//console.log($.cookie("Iwish_chat_mode"));
	$(".before-login").hide();$(".after-login").show();if(startpointer)chat(""); startpointer =false;stopforever = false;

}
else
{
	$(".after-login").hide();$(".before-login").show(); stopforever = true;
}

setTimeout(check_user,1000);
}

function scroll_end(jqt){
	
	jqt.scrollTop(1E10);
}


function write_chat(arr){
var tricky = "";
for (i = arr.length-1; i>=0; i--)
{
	if(arr[i]["way"]==1)temper = "tmdm1";else if(arr[i]["way"]==2)temper = "tmdm2";
tricky+= '<div class="row">'+
                            '<div class="col-lg-12">'+
                                '<div class="media">'+
                                    
                                    '<div class="media-body">'+
                                        
                                        '<div class="'+temper+'" data-toggle="tooltip" data-placement="top" title="'+arr[i]["date_time"]+'"><p>'+arr[i]["msg"]+'</p></div>'+
                                        
                                    '</div>'+
                                '</div>'+
                            '</div>'+
                        '</div> ';
}

						if(tricky!="")
						return tricky;
						else
						return false;

}




var timestavar = 0;var abort_chat=null;startpointer = true;stopforever = false; /*urlin = "http://chat.localhost/";*/urlin = "http://chat.localhost/";
function chat(chatvar){
	
	abort_chat = $.ajax({
	
		type: "GET",
		url: urlin+"chat.php",
		beforeSend: function(){if(abort_chat!=null)abort_chat.abort();},
		data:{chatting:chatvar,timestamp:timestavar},
		dataType:'jsonp',
		jsonp:"doing-good",
		cache:false,
		async: true,
		success: function(data){
		//todo
		if(data["timestamp"]!=undefined)
		timestavar = data["timestamp"];
		
		chattti = write_chat(data["msgs"]);
		
		if(chattti)
		$(".tmdr").html(chattti);
		
			scroll_end($(".tmdr"));
			$(".tmdm1,.tmdm2").tooltip();
			if(!stopforever)
			setTimeout(chat,1000,"");
		},
		error: function(XMLHttpRequest,textStatus,errorThrown){
		//todo
			setTimeout(chat,1000,"");
		}
	
	
	});
	
	}
	
	
	function chat_write(chator){
	
	abort_chat = $.ajax({
	
		type: "GET",
		url: urlin+"chat.php",
		//beforeSend: function(){if(abort_chat!=null)abort_chat.abort();},
		data:{chatting:chator,timestamp:timestavar},
		dataType:'jsonp',
		jsonp:"doing-good",
		cache:false,
		async: true,
		success: function(data){
		
		
		
		chattti = write_chat(data["msgs"]);
		
		if(chattti)
		$(".tmdr").html(chattti);
		
			
		},
		error: function(XMLHttpRequest,textStatus,errorThrown){
		//todo
			
		}
	
	
	});
	
	}


$(document).ready(function(){
	check_user();
	
	
	$(".portlet-body").scrollTop(1E10);
	
	
	//$.ajaxSetup({beforeSend:function(){ console.log(abort_chat.abort()); if(abort_chart!=null)abort_chat.abort();}});
	
$.ajax({
	
		type: "GET",
		url: urlin+"validp.php",
		//beforeSend: function(){if(abort_chat!=null)abort_chat.abort();},
		data:{"param":"check_user"},
		dataType:'jsonp',
		jsonp:"doing-good",
		cache:false,
		async: true,
		success: function(data){
		
			if(data["ck"]=="unset")
			$.removeCookie('Iwish_chat_mode',{path:"/"});
			$(".tmdr").empty();
			
		},
		error: function(XMLHttpRequest,textStatus,errorThrown){
		//todo
			
		}
	
	
	});
	
	
	
	
$(".chat_form").submit(function(){
	
	
	
	
	
		$.ajax({
	
		type: "GET",
		url: urlin+"index.php",
		//beforeSend: function(){if(abort_chat!=null)abort_chat.abort();},
		dataType:'jsonp',
		jsonp:"doing-good",
		data:$(this).serialize(),
		
		
		
		success: function(data){
				
				if(data["Iwish_chat_mode"]!=undefined)
				$.cookie("Iwish_chat_mode",data["Iwish_chat_mode"],{path:"/"});
				
				}
	
	
	});
		return false;

});


$(".chat-form-text").bind("keypress",function(e){
	if(e.keyCode==13){
	if($(".chat-form-text").val()!="")
	chat_write($(".chat-form-text").val());
	
	$(".chat-form-text").val("");
	scroll_end($(".tmdr"));
	return false;
	}
});







});


$(window).on('beforeunload', function(){  if(abort_chat!=null)abort_chat.abort();});