﻿var charid = null;

function getId(elem) {
    var name = $(elem).html(),
        data = JSON.parse(localStorage.getItem("playerItems")),
        id = null;
    $.each(data.items.item, function (i, v) {
        if (v.ItemName == name) {
            id = v.OwnershipID;
        }
    });
    equipItem(id);
}

function setSlots() {
    var offSetY = 40, offSetX = 40;
    var aYStand = $("#armorStand").height(), aXStand = $("#armorStand").width(), hYPos = 40 - offSetY, hXPos = (aXStand / 2) - offSetX, cYPos = 160 - offSetY, cXPos = (aXStand / 2) - offSetX,
        lYPos = ((aYStand / 3) * 2) - offSetY, lXPos = (aXStand / 2) - offSetX, handYPos = ((aYStand / 3) * 2) - 20 - offSetY, handXPos = (aXStand / 2) + 85 - offSetX;
    $("#head").css({ top: hYPos, left: hXPos });
    $("#chest").css({ top: cYPos, left: cXPos });
    $("#legs").css({ top: lYPos, left: lXPos });
    $("#hand").css({ top: handYPos, left: handXPos });
    var postD = { CharID: charid };
    $.ajax({
        async: true,
        type: "POST",
        url: "api/chara/getItems.php",
        data: postD,
        cache: false,
        processData: true,
        success: function (data) {
            var results = JSON.parse(data);
            console.log(results);
            if (results.success) {
                $.each(results.items.item, function (index) {
                    switch (results.items.item[index].SlotID) {
                        case "1":
                            $("#head").css('background-image', 'url(Resources/image/items/' + results.items.item[index].ImageURL + ')').html(results.items.item[index].ItemName);
                            console.log("entered3");
                            break;
                        case "2":
                            $("#chest").css('background-image', 'url(Resources/image/items/' + results.items.item[index].ImageURL + ')').html(results.items.item[index].ItemName);
                            console.log("entered2");
                            break;
                        case "3":
                            $("#hand").css('background-image', 'url(Resources/image/items/' + results.items.item[index].ImageURL + ')').html(results.items.item[index].ItemName);
                            console.log("entered");
                            break;
                        case "4":
                            $("#legs").css('background-image', 'url(Resources/image/items/' + results.items.item[index].ImageURL + ')').html(results.items.item[index].ItemName);
                            console.log("entered4");
                            break;
                        default:
                            console.log("error");
                            break;
                    }
                });
            } else {
                //send to error handler
            }
        }
    });
}

function equipItem(itemId) {

    var postD = { Token: localStorage.getItem('userToken'), OwnershipID: itemId, CharID: charid };
    console.log(postD);
    $.ajax({
        async: true,
        type: "POST", 
        url: "api/item/equip.php",
        data: postD,
        cache: false,
        processData: true,
        success: function (data) {
            var results = JSON.parse(data);
            console.log(results);
            if (results.success) {
                console.log("item equiped");
            } else {
                errorReporting(results.error_code, itemId);
            }
        }
    });
}

function errorReporting(eCode, itemId) {
    switch (eCode) {
        case 3:
            console.log("This Item is already equiped.");
            break;
        case 4:
            console.log("This item cannot be equiped because you are too newbie.");
            break;
        case 5:
            console.log("There is an item already equiped in that slot will replace.");
            deEquip(itemId);
    }
}

function deEquip(itemId) {
    data = JSON.parse(localStorage.getItem('playerItems'));
    $.each(data.items.item, function (index) {
        if (data.items.item[index].OwnershipID == itemId) {
            var postD = { Token: localStorage.getItem('userToken'), EquipID: data.items.item[index].OwnershipID };
            $.ajax({
                async: true,
                type: "POST",
                url: "api/item/deEquip.php",
                data: postD,
                cache: false,
                processData: true,
                success: function (data) {
                    var results = JSON.parse(data);
                    console.log(results);
                    if (results.success) {
                        equipItem(itemId);
                    } else {
                        console.log("Was unable to deequip item");
                    }
                }
            });
        }
    });
}
@@ -3,24 +3,40 @@
 ar charid = 
+null, equipedId=
 null;%0A%0Afunct
@@ -1281,32 +1281,67 @@
 ults.success) %7B%0A
+                equipedId=results;%0A
                 
@@ -3981,110 +3981,119 @@
     
-data = JSON.parse(localStorage.getItem('playerItems'));%0A    $.each(data.items.item, function (index) %7B
+$.each(data.items.item, function (index) %7B%0A        console.log(equipedId.items.item%5Bindex%5D.EquipID);%0A        /*
 %0A   
@@ -4226,20 +4226,25 @@
 quipID: 
-data
+equipedId
 .items.i
@@ -4250,31 +4250,27 @@
 item%5Bindex%5D.
-Ownersh
+Equ
 ipID %7D;%0A    
@@ -4860,26 +4860,28 @@
    %7D);%0A        %7D
+*/
 %0A    %7D);%0A%7D

test changes