// JavaScript Document
var base = "http://www.meeet.at/";
var $channelval = "Raumname oder Thema";
var $nickval = "Dein Spitzname";
var nick_okay = false;
var $mt;
var $nick;
var working = false;
var s_over = false;
/* IMGS */
var LOADING 		= "<img src='http://www.meeet.at/img/arrows.gif' border='0'>";
var ERROR 			= "<img src='http://www.meeet.at/img/error_32.png' border='0'>";
var GOOD 			= "<img src='http://www.meeet.at/img/good.png' width='16' height='16' border='0'>";
var LOADING_IMG 	= "http://www.meeet.at/img/arrows.gif";
var ERROR_IMG 		= "http://www.meeet.at/img/error_32.png";
var GOOD_IMG 		= "http://www.meeet.at/img/good.png";

function kill_smilies(){
	if (!s_over) $('.chatactions .smiliebox').hide();
}
function init_chat(){
	var $channel = $("#channel").val();
		if ($channel != $channelval) {
			$.post(base + "funcs/eee-ajax.php?f=sweet_channel_name", { channel: $channel },
					function(data){ 
						
							window.location = base + data.sweet;	
						
					}, "json" );		
		}
}

function _dialog($msg){
	$(".thedialog").dialog({ 
								 resizable: false, 
								 draggable: false, 
								 title: 'Deine Eingabe',
								 modal: true, 
								 width: 300, 
								 height: 280, 
								 buttons: { "OK >": function() {}}
								}).find("h2").text($msg);
}
function clear_nick($this){if($this.val() == $nickval) $this.val("");}
	
$(document).ready(function(){
	if (window.navigator.userAgent.indexOf("Gecko") > -1) { var corners = true; } else { var corners = false; }
	
	if (corners){
	$("#main, #openchat, .greenbox, .responsetext").corner();
	$(".chatbox").corner("tl tr br");
	$(".top-navi").corner("5px");
	$("#openchat a, #signin_popup a").corner("5px right");
	$(".main-container1").corner("top");
	$(".main-container2").corner("bottom");
	}
	$("#signin_popup input").removeAttr("disabled");
	if ($("#instantconnect").val() != "true") $("input#nickname").val($nickval);
	// $("input#nickname").keyup(function(){check_nickname($(this).val());});
	$("input#nickname").click(function(){clear_nick($(this));});
	
	$("#openchat #channel").val($channelval)
	$("#openchat #channel").focus(function(){ if($("#channel").val()==$channelval) $("#channel").val(""); });
	$("#openchat #channel").blur(function(){ if($("#channel").val()=="") $("#channel").val($channelval); });
	
	$("#openchat a").click(function(){init_chat();});
	$("#signin_popup a").hover(
		function(){
			if (nick_okay) $(this).css({"background-position" : "0 -5px"});
			
		},
		function(){$(this).css({"background-position" : "0 0"});}
	);
			
	
	$('#openchat #channel').keypress( function(e) {
					var key = e.charCode || e.keyCode || 0
					if (key == 13) {
						init_chat();
					};
				});
			
	$("a.open_embed").click(function(){$(this).hide();$("#embed_script").fadeIn("fast");});	
	$(".item.close").live("click",function(){$(this).parents(".privatemsg").remove();private_windows[$(this).parents(".privatemsg").attr("id").substr(8)] = false;});
	$(".item.minimize").live("click",function(){
		$(this).toggleClass("maximize").toggleClass("minimize").parents(".privatemsg").animate({"width": "200px", "height" : "22px"}, "fast");
		private_windows[$(this).parents(".privatemsg").attr("id").substr(8)] = "minimized";
		$(this).parents(".privatemsg").find(".chathistory, .chatmessage, .chatactions").hide();
		});
	$(".item.maximize").live("click",function(){
	    var owidth = "495px";
		var oheight = "475px";
		if ($("#main").hasClass("embed")){owidth = "330px"; oheight = "330px";}
		$(this).toggleClass("maximize").toggleClass("minimize").parents(".privatemsg").css({"width": owidth, "height" : oheight});
		private_windows[$(this).parents(".privatemsg").attr("id").substr(8)] = "maximized";
		$(this).parents(".privatemsg").find(".chathistory, .chatmessage, .chatactions").show().find("textarea").focus();
		
		});
	
	$('.chatbox_input.private').live("keypress", function(e) {
					
					var key = e.charCode || e.keyCode || 0
					if (key == 13) {
					var msg = $(this).val();
					
					var to = $(this).parents(".privatemsg").attr("id").substr(8);
					private_chat(to,msg);
					return false;
					
					};
				});
	$('.privatemsg .chatmessage a').live("click", function() {
					
					var text = $(this).parents(".privatemsg").find("textarea");
					var msg = text.val();
					
					var to = $(this).parents(".privatemsg").attr("id").substr(8);
					private_chat(to,msg);
					text.val("");
					text.focus();
					return false;
				});
	$('.privatemsg').live("click", function() {
					$(this).find(".private_menu").css({"background-image" : "url("+PRIVICON+")"}).parent().find("textarea").focus();
					focus_at = $(this).attr("id").substr(8);
				});
	$(".chatbox").live("click", function() {focus_at = "main";});
	
	$('.user_entry a').live("click",
		function(){
			$(this).next("*").fadeIn();
		}
	);
	
	$('.user_entry .close').live("click",
		function(){
			$(this).parent().hide();
		}
	);
	
	$('.user_entry ul li a.promote').live("click",
		function(){
			var who = $(this).parents(".user_entry").find("a:first").text();
			if (confirm("Bist du sicher, dass du "+who+" befördern willst? Er/Sie kann dann eigenständig Leute rauswerfen oder das Channelthema ändern.")){
				init_mode(who,"+h");
				$(this).parents(".umenu").hide();
				$(this).removeClass("promote").addClass("demote").text("Degradieren");
			}
		}
	);
	
	$('.user_entry ul li a.demote').live("click",
		function(){
			var who = $(this).parents(".user_entry").find("a:first").text().substr(1);
			if (confirm("Bist du sicher, dass du "+who+" degradieren willst?")){
				init_mode(who,"-h");
				$(this).parents(".umenu").hide();
				$(this).removeClass("demote").addClass("promote").text("Befördern");
			}
		}
	);
	
	$('.user_entry ul li a.kick').live("click",
		function(){
			var who = $(this).parents(".user_entry").find("a:first").text();
			if (confirm("Bist du sicher, dass du "+who+" rauswerfen willst?")){
				init_kick(who,"");
				$(this).parents(".umenu").hide();
			}
		}
	);
	
	$('.user_entry ul li a.private').live("click",
		function(){
			var who = $(this).parents(".user_entry").find("a:first").text();
			init_priv(who.replace("@",""));
			$(this).parents(".umenu").hide();
		}
	);
	
	$('.user_entry a').live("dblclick",
		function(){
			init_priv($(this).text().replace("@",""));
		}
	);
	
	$("#reg input#nick").blur(
		function(){
			var $nick = $(this).val();
			if ($nick != ""){
				$("img#responseimg,div#responsetext").remove();
				$("<img id='responseimg' />").attr("src",LOADING_IMG).addClass("responseimg").insertAfter($(this));
					 
						$.post(base + "funcs/eee-ajax.php?f=check_nickname", { nick: $nick },
								function(j){ 
									
									if (j.rdy == "rdy") {
										$("img#responseimg").attr("src",GOOD_IMG).addClass("big");
									} else {
										$("img#responseimg").attr("src",ERROR_IMG).addClass("big");
										$("<div id='responsetext' />").addClass("responsetext fade").corner().html(j.error).insertBefore("#reg").fadeIn("fast");
										
									}
									
								}, "json" );
			}
		
				
		}
		
	);
	
	$("#reg input#email").blur(
		function(){
			var $email = $(this).val();
			if ($email != ""){
				$("img#responseimg2,div#responsetext").remove();
				$("<img id='responseimg2' />").attr("src",LOADING_IMG).addClass("responseimg").insertAfter($(this));
					 
						$.post(base + "funcs/eee-ajax.php?f=check_email", { email: $email },
								function(j){ 
									
									if (j.rdy == "rdy") {
										$("img#responseimg2").attr("src",GOOD_IMG).addClass("big");
									} else {
										$("img#responseimg2").attr("src",ERROR_IMG).addClass("big");
										$("<div id='responsetext' />").addClass("responsetext fade").corner().text(j.error).insertBefore("#reg").fadeIn("fast");
										
									}
									
								}, "json" );
			}
		
				
		}
		
	);
	
	$("#reg-channel input#channel").blur(
		function(){
			var $channel = $(this).val();
			if ($channel != ""){
				$("img#responseimg,div#responsetext").remove();
				$("<img id='responseimg' />").attr("src",LOADING_IMG).addClass("responseimg").insertAfter($(this));
					 
						$.post(base + "funcs/eee-ajax.php?f=check_channel", { channel: $channel },
								function(j){ 
									
									if (j.rdy == "rdy") {
										$("img#responseimg").attr("src",GOOD_IMG).addClass("big");
									} else {
										$("img#responseimg").attr("src",ERROR_IMG).addClass("big");
										$("<div id='responsetext' />").addClass("responsetext fade").corner().text(j.error).insertBefore(".greenbox").fadeIn("fast");
										
									}
									
								}, "json" );
			}
		
				
		}
		
	);
	
	
	$('.chatactions a.opensmiliebox').live("mouseover",
		function(){
			$(this).siblings(".smiliebox").show();
			s_over = true;
		}
	);
	$('.chatactions .smiliebox').live("mouseover",
		function(){
			$(this).siblings(".smiliebox").show();
			s_over = true;
		}
	);
	$('.chatactions .smiliebox, .chatactions a.opensmiliebox').live("mouseout",
		function(){
			s_over = false;
			setTimeout("kill_smilies()",300);
		}
	);
	
	$('.smiliebox a').live("click",
		function(){
			var code = $(this).attr("class");
			var msg = $(".chatmessage textarea").val();
			msg = msg + " " + code+" ";
			$(this).parents(".chatactions").siblings(".chatmessage").find("textarea").val(msg);
			$(this).parent().hide();
			
		}
	);
	
	$('.chatactions a.cmd_me').click(
		function(){
			var msg = $("#chatbox_input").val();
			$("#chatbox_input").val("/me "+msg).focus();
			
			
		}
	);
	$('.chatactions a.cmd_topic').click(
		function(){
			var msg = $("#chatbox_input").val();
			$("#chatbox_input").val("/topic "+msg).focus();
			
			
		}
	);
	
	
	
});