var undefined;
var lastCmd = [];
var lastCmdPtr = -1;
var unreadMsg = 0;

$(document).ready(function() {
	$('#wdevice_form').submit(function(){
		$('#wdevice_submit').attr("disabled","disabled");
		return false;
	});

	$('#wdevice_form').ajaxForm({
		beforeSubmit: function() { $('#wdeviceSpinner').show(); },
		success: wdevice_success
	});
	
	$("#wdevice_input").keydown(wdevice_keydown);
	
	$('div#wdevice_notification h4').click(minimize);
	$('#wdevice_minimize').click(minimize);

	$('#wdevice_whatis').click(function(){
		if ($('#wdevice_thisis').is(':hidden')){
			if($('#wdevice_webprompt').is(':hidden')){
				$('#wdevice_thisis').show();
				$('#wdevice_webprompt').slideDown();
				$('#wdevice').addClass('opened');
			}
			else{
				$('#wdevice_thisis').slideDown();
			}
		}
		else $('#wdevice_thisis').slideUp();
	});
	
	if (checkPMSecs != undefined && checkPMSecs > 0)
		setTimeout ('wdevice_checkpm();', checkPMSecs*1000);
});

function wdevice_success(responseText, statusText){
	var inputText = $("#wdevice_input").val();
	addToLastCmd($("#wdevice_input").val());
	$("#wdevice_input").val("");
	$("#wdeviceSpinner").hide();
	$('#wdevice_submit').removeAttr("disabled");

	var dialogCnt = $("li.wdevice_dialog").length - 2;
	var newMsgElementID = "wdevice_dialog_" + dialogCnt;
	var newMsgElement = $("#wdevice_conv_template").clone().attr("id", newMsgElementID);
	if (dialogCnt % 2) newMsgElement.addClass("alt");
	
	newMsgElement.children().children(".wdevice_input").text(inputText);
	newMsgElement.children().children(".wdevice_dialogdate").text(getTimeNow());
	newMsgElement.children(".wdevice_messageproper").html(responseText);
	newMsgElement.appendTo("#wdevice_dialogs");
	newMsgElement.hide();
	newMsgElement.slideDown("slow");
	$("#wdevice_dialogs").scrollTo( '+=999px', 1000, {queue:'true'} );
}

function wdevice_keydown(e){
	if (e.which == 38){
		if (lastCmdPtr >= 0){
			lastCmdPtr--;
			if (lastCmdPtr < 0) lastCmdPtr = 0;
			$("#wdevice_input").val(lastCmd[lastCmdPtr]);
		}
		return false;
	}
	else if (e.which == 40){
		if (lastCmdPtr <= lastCmd.length){
			lastCmdPtr++;
			if (lastCmdPtr > lastCmd.length-1) lastCmdPtr = lastCmd.length -1;
			$("#wdevice_input").val(lastCmd[lastCmdPtr]);
		}
		return false;
	}
	return true;
}

function minimize() {
	$('#wdevice_webprompt').slideToggle('normal', function(){
		if ($('#wdevice_thisis').is(":visible"))
			$('#wdevice_thisis').hide(); 
	});
	$('#wdevice').toggleClass('opened');
	$("#wdevice h4 span").text('Widgeous Device');
	unreadMsg = 0;
}

function wdevice_checkpm(){
	$.ajax({
		type: "GET",
		url: "/channels/chkmsg",
		dataType: "xml",
		cache: false,
		success: function(xml){
			var count = parseInt($(xml).find("messages").attr('count'));
			if (count > 0){
				if (!$('#wdevice').hasClass('opened')) {
					unreadMsg += 1;
					if (unreadMsg == 1) {
						$("#wdevice h4 span").text('One new message');
					} else {
						$("#wdevice h4 span").text(unreadMsg + ' new messages');
					}
				}

				$(xml).find('item').each(function(){
					from_type = $(this).children('from_type').text();
					name = $(this).children('from_name').text();
					avatar = $(this).children('from_avatar').text();
					message = $(this).children('message').text();
					datetime = $(this).children('datetime').text();
					
					var dialogCnt = $("li.wdevice_dialog").length - 2;
					var newMsgElementID = "wdevice_dialog_" + dialogCnt;
					var newMsgElement = $("#wdevice_message_template").clone().attr("id", newMsgElementID);
					if (dialogCnt % 2) newMsgElement.addClass("alt");
					
					newMsgElement.children().children(".wdevice_authorname").text(name);
					newMsgElement.children().children(".wdevice_dialogdate").text(getTimeNow());
					newMsgElement.children(".wdevice_messageproper").html(message);
					newMsgElement.children(".device_avatar").attr('src', '/img/avatars_' + from_type + '/16/' + avatar);
					newMsgElement.appendTo("#wdevice_dialogs");
					newMsgElement.hide();
					newMsgElement.slideDown("slow");
					$("#wdevice_dialogs").scrollTo( '+=999px', 1000, {queue:'true'} );
				});
			}
		}
	});

	setTimeout ('wdevice_checkpm();', checkPMSecs*1000);
}

function addToLastCmd(val){
	lastCmd.push(val);
	lastCmdPtr = lastCmd.length;
}

function getTimeNow(){
	var now = new Date();
	return checkTime(now.getHours()) + ":" + checkTime(now.getMinutes());
}

function checkTime(i){
	if (i<10){
		i="0" + i;
	}
	return i;
}