﻿document.onkeyup = KeyCheck

function KeyCheck(e) {
    var KeyID = (window.event) ? event.keyCode : e.keyCode;
    
    switch (KeyID) {
        case 27:
            if (document.MCPDPopup != null)
                document.MCPDPopup.close();
    }
}

/**********************************************************
* Class: MagicCardProductDetails                          *
* Desc :                                                  *
**********************************************************/
function MagicCardProductDetails(cardname) {
    this.cardname = cardname;
    this.imageURL = '';
    this.itemNumber = '';
    this.price = '';
    this.quantity = '';
    var cb = null;

    var endGetDetails = function(req) {
        var doc = req.responseXML;
        var e = doc.getElementsByTagName("ImageURL");

        req.item.imageURL = getProperty(doc, 'ImageURL');
        req.item.itemNumber = getProperty(doc, 'ItemNumber');
        req.item.price = getProperty(doc, 'Price');
        req.item.quantity = getProperty(doc, 'Quantity');

        cb();
    };

    var getProperty = function(doc, property) {
        var e = doc.getElementsByTagName(property);

        if (e.length == 1) {
            if (typeof e[0].textContent != "undefined")
                return e[0].textContent;
            else
                return e[0].text;
        }
    }

    this.beginGetDetails = function(callback) {
        var req = new HTTPRequest("/js/webfunctions/getmagiccardproductdetails.ashx?cardname=" + cardname, endGetDetails);
        cb = callback;
        
        req.request.item = this;
        req.send();
    };
}

function showMCPDPopup(cardname) {
    if (document.MCPDPopup == null)
        document.MCPDPopup = new MCPDPopup();

    document.MCPDPopup.show(cardname);
}

function MCPDPopup() {
    var details = null;
    var dialog;
        
    var init = function() {
        beginDownloadForm();
    };

    var beginDownloadForm = function() {
        var req = new HTTPRequest("/js/forms/MagicCardProductDetailsPopup.html", endDownloadForm);
        req.send();
    };

    var endDownloadForm = function(req) {
        document.body.innerHTML += req.responseText;
        dialog = document.getElementById("MCPDPopup")
    };

    var displayData = function() {
        var price = document.getElementById("MCPDPopupPrice");
        var image = document.getElementById("MCPDPopupImage");
        var quantity = document.getElementById("MCPDPopupQuantity");
        var buyLink = document.getElementById("MCPDPopupBuyLink");
        var moreLink = document.getElementById("MCPDPopupMoreLink");
        //var title = document.getElementById("MCPDPopupTitle");

        price.innerHTML = details.price;
        quantity.innerHTML = details.quantity;
        image.src = details.imageURL;
        moreLink.href = "/store/magic/viewcard.aspx?card_name=" + details.cardname;
        buyLink.href = "javascript:buyCard('" + details.itemNumber + "');";

        //title.innerHTML = details.cardname
        dialog.style.display = "block";
        dialog.style.top = document.yMousePos + "px";
        dialog.style.left = document.xMousePos + "px";
        dialog.style.position = "fixed";
    }

    this.getData = function(cardname) {
        this.details = MagicCardProductDetails(cardname);
    };

    this.close = function() {
        dialog.style.display = 'none';
    }

    this.show = function(cardname) {
        details = new MagicCardProductDetails(cardname);
        details.beginGetDetails(displayData);
    }

    init();
}

function closeMCPDPopup() {
    document.MCPDPopup.close();
}

function buyCard(itemNumber) {
    var req = new HTTPRequest("/js/cart/GetCartDetails.aspx?SaleType=1&Q=1&I=" + itemNumber, cardAdded);
    req.send();
}

function cardAdded(req) {
    fade("MCPDPopupBuyLinkBox");

    setTimeout("fade('MCPDPopupBuyLinkBox')", TimeToFade);
}
