﻿    //CONSOLE.JS - Sharp MUD - Adam Rochford
    //SERHIY VERNEI'S jQuery MAP STUFF Serhiy 12_14_08
    timertime = 750;    //Initial Request Speed For New Data
    sessionid = 0;      //Holds your current sessionid
    //If you are trying to hack the game good luck to you, the server side takes care of everything
    //If the server sees you are using a false id then it will kick you.
    //PUBLIC VARS
    var lastX = 98765;
    var lastY = 98765;
    var laztZ = 98765;
    var roomID = 98765;
    var zIndex = 0;
    var strDebug = '';
    
    $(document).ready(function () {
        drawWorld();
    });
    
    function datasend(data){
        $.ajax({
            type: "POST",
            url: "data.aspx",
            data: "send=" + data + "&sessionid=" + sessionid,
            dataType: "xml",
            success: function(msg) {
                printData($(msg).find('output').text(), $(msg).find('ft').text());
                sessionid = $(msg).find("sessionid").text();
            }
        });
    }
    
	function getData(){
	    $.ajax({
            type: "POST", 
            url: "data.aspx", 
            data: "receive=" + sessionid,
            dataType: "xml",
            success: function(msg){
                    printData($(msg).find('output').text(), $(msg).find('ft').text());
                    //sessionid = $(msg).find("sessionid").text(); 
                }
            });
    }
    
    function printData(data, ft)
    {
        if (data != "") {
            $("#DISP").append(data);
        }
	    
        var ftint = parseInt(ft);
        //console.log( data );
        
        if (ftint == 100)
        { $("#FIGHTTIME").text("You are not fighting."); }
        else if (ftint <= 0)
        { $("#FIGHTTIME").html("<span style=\"color: lime;\">You can attack now!</span>"); }
        else if (ftint > 0)
        { $("#FIGHTTIME").html("You can attack in <span style=\"color: yellow; font-weight: bold;\">" + ft.toString() + "</span> seconds!"); }
        else
        { $("#FIGHTTIME").text("Please log in."); }
	        
	        //$("#FIGHTTIME").text("'" + parseInt(ft).toString() + "'");
	        
	    scrolldown();
    }
    
    function buttonsend()
    {
        timertime = 750;
        $("#txtInput").focus().select();

        $.ajax({
            type: "POST",
            url: "data.aspx",
            data: "send=" + $('#txtInput').val() + "&sessionid=" + sessionid,
            dataType: "xml",
            success: function(msg) {
                printData($(msg).find('output').text(), $(msg).find('ft').text());
                sessionid = $(msg).find("sessionid").text();

                //COUNT FOR THE FIRST ROOM
                var theRoom = 0;
                //LOOPING THROUGH THE ROOMS
                $(msg).find("rms").find("rm").each(function() {
                    newX = $(this).attr('x');
                    newY = $(this).attr('y');
                    newZ = $(this).attr('z');
                    bgType = $(this).attr('t');

                    //IF THIS IS THE ROOM YOU ARE IN
                    if (theRoom == 0) {
                        roomID = $(this).attr('id');
                        //INCREMENT THE ROOM ID
                        theRoom++;
                        //CHECK WHERE YOU MOVE FROM
                        if (lastX != 98765) {
                            if ((newX - lastX) == 1) { move('e'); }
                            else if ((newX - lastX) == -1) { move('w'); }

                            if ((newY - lastY) == 1) { move('n'); }
                            else if ((newY - lastY) == -1) { move('s'); }
                        }
                        //THE LAST ROOM IS NOW WHAT THE NEW ROOM WAS
                        lastX = newX;
                        lastY = newY;

                        //FOR FUTURE DEV
                        if ($('#room' + newX + '_' + newY).length > 0) {
                            //UPDATE CODE
                        }
                        else {
                            drawRoom(roomID, newX, newY, newZ, ';border:none;', bgType);
                        }
                    }
                    else {
                        //THIS IS FOR THE ROOMS AROUND YOU

                        //THE ROOM YOU ARE IN
                        var lastRoom = 'none';

                        if ((newX - lastX) == 1) { lastRoom = 'left' }
                        else if ((newX - lastX) == -1) { lastRoom = 'right' }

                        if ((newY - lastY) == 1) { lastRoom = 'bottom'; }
                        else if ((newY - lastY) == -1) { lastRoom = 'top'; }


                        if (lastRoom == 'none') {
                            lastRoom = ';';
                        }
                        else {
                            lastRoom = ';border-' + lastRoom + ':solid 1px #f5f5f5;';
                        }

                        //FUTURE DEV
                        if ($('#room' + newX + '_' + newY).length > 0) {
                            //ROOM UPDATE
                        }
                        else {
                            drawRoom(roomID, newX, newY, newZ, lastRoom, bgType);
                        }


                    }

                    //DEBUGGING
                    if (theRoom == 0) {
                        strDebug += "<br/>you";
                    }
                    strDebug += roomID + ' = x' + newX + ' * y' + newY + ' ^^ lastx' + lastX + ' ^ lasty' + lastY + ' room ' + theRoom + '<br/>';

                    //var now = new Date();
                    //$('#pDebug').empty();
                    //$('#pDebug').prepend(strDebug);
                    //END DEBUG
                });
            }
        });
    }
    
    function scrolldown()
    { 
        var objDiv = document.getElementById("DISP");
        objDiv.scrollTop = objDiv.scrollHeight;
    }   
    
    function datareceiver()
    {
        $.timer(timertime, function (timer) {
   	    getData();
   	    //scrolldown();
   	    timertime = timertime + 75;
   	    timer.reset(timertime);
   	    
   	    //Set Session Limit
   	    if (timertime > 15000)
   	    { 
   	        datasend("quit");
   	        timer.stop(); 
   	    }
   	    
   	    });
    }
    
    datasend("logingreeting");
    datareceiver();
    
    //SERHIY VERNEI'S jQuery MAP STUFF Serhiy 12_14_08
    function drawWorld()
    {
        drawChar(0, 0, 0);
    }
    function drawChar(x, y, z)
    {
        $('#you').css('left', (x + 3 + 65)).css('top', (y + 3 + 65)).css("z-index", '1000');
    }

    function drawRoom(id, x, y, z, lastRoom, background){
        var room = '<div id="room' + x + '_' + y + '" class="room" style="' +
        'background-color:' + roomTypeToColor(background) +
        ';left:' + ((x * 14)) + 'px;' + 'top:' + ((y * -14)) + 'px;' + ' z-index:' + zIndex + lastRoom +
        ';">';
        
        '</div>';
        zIndex++;
       
        $('#world').css("border" , "none");
        $('#world').height  (y * 14 + 1 + 400);
        $('#world').width(x * 14 + 1 + 400)
        $('#world').append(room);
    }
    function roomTypeToColor(type) {
        if (type == '1') {
            return '#B0B0B0';
        }
        if (type == '19') {
            return 'red';
        }
        else {
            return '#F0F0F0';
        }
    }
    function move(direction)
    {
        if(direction == 'w')

        {
            $("#world").animate({
                left: "+=14px"
            }, 1000);
        }
        else if(direction == 'e')

        {
            $("#world").animate({
                left: "-=14px"
            }, 1000);
        }
        else if(direction == 'n')

        {
            $("#world").animate({
                top: "+=14px"
            }, 1000);
        }
        else if(direction == 's')

        {
            $("#world").animate({
                top: "-=14px"
            }, 1000);
        }
    }
