﻿$(function() {
    var Class;
    registerNS("ChristmasImprints.JS");
    Class = ChristmasImprints.JS.CardInfo = function(insertAfter, cardManager) {
        // Class entry point. Fired when instantiated.
        this.initialise(insertAfter, cardManager);
    }

    function initialiseStatic() {
        // Initialise any static variables here...
    }

    Class.prototype = {

        // Initialise any member variables here...
        initialise: function(insertAfter, cardManager) {
            var tmpElement;
            this.cardManager = cardManager;
            this.root = document.createElement("div");
            this.image = document.createElement("img");
            this.cardId = document.createElement("span");
            this.cardTitle = document.createElement("span");
            this.cardSize = document.createElement("span");
            this.cardClose = document.createElement("span");
            this.currentCardId = 0;
            this.arena = null;
            this.margin = { x: 0, y: 0 };
            this.imageObj = null;
            this.enabled = true;

            YAHOO.util.Dom.addClass(this.root, "wndPhotoInfo");
            YAHOO.util.Dom.addClass(this.cardId, "cardId");

            this.image.setAttribute("alt", "large card");

            this.root.appendChild(this.image);
            this.root.appendChild(tmpElement = document.createElement("h4"));
            tmpElement.appendChild(this.cardId);
            tmpElement.appendChild(this.cardTitle);
            this.root.appendChild(tmpElement = document.createElement("h5"));

            tmpElement.appendChild(this.cardSize);
            tmpElement.appendChild(this.cardClose);

            this.cardClose.innerHTML = "(<a href='javascript:void(0);'>close</a>)";
            YAHOO.util.Dom.addClass(this.cardSize, "size");
            YAHOO.util.Dom.addClass(this.cardClose, "close");

            YAHOO.util.Event.addListener(tmpElement, 'click', gUtils.callback(this, this.hideAllCards, new Array()));

            YAHOO.util.Dom.insertAfter(this.root, insertAfter);
            YAHOO.util.Dom.setStyle(this.root, "display", "none");
            YAHOO.util.Dom.setStyle(this.root, "opacity", "0");
        },

        onImageLoaded: function(location) {
            var card = this.cardManager.getCard(this.currentCardId);
            var x;
            var y;
            var attempt = 0;
            var windowDim;

            this.image.setAttribute("alt", card.title);
            this.cardId.innerHTML = card.id;
            this.cardTitle.innerHTML = " - " + card.title;
            this.cardSize.innerHTML = card.size;

            if (this.masterAlphaTween != null)
                this.masterAlphaTween.stop(false);
            YAHOO.util.Dom.setStyle(this.root, "display", "block");
            YAHOO.util.Dom.setStyle(this.cardClose, "margin-left", (this.image.width-117) + "px"); // Bodge, I'm sorry! Deadlines...
            windowDim = YAHOO.util.Dom.getRegion(this.root);

            if (typeof (location.x) == "undefined" && typeof (location.y) == "undefined") {
                var elmRegion = YAHOO.util.Dom.getRegion(location);

                do {
                    switch (attempt++) {
                        case 0:
                            windowDim.x = (elmRegion.x + elmRegion.width) + this.margin.x;
                            windowDim.y = elmRegion.y + this.margin.y;
                            break;
                        case 1:
                            windowDim.x = elmRegion.x - (windowDim.width + this.margin.x);
                            windowDim.y = elmRegion.y + this.margin.y;
                            break;
                        case 2:
                            windowDim.x = (elmRegion.x + elmRegion.width) + this.margin.x;
                            windowDim.y = elmRegion.y - ((elmRegion.y + windowDim.height) - (this.arena.y + this.arena.height));
                            break;
                        case 3:
                            //alert("Attemping 4");
                            windowDim.x = elmRegion.x - (windowDim.width + this.margin.x);
                            windowDim.y = elmRegion.y - ((elmRegion.y + windowDim.height) - (this.arena.y + this.arena.height));
                            break;
                    }

                } while (isOOB(this, windowDim) && attempt < 4)
                // passed in an element reference...
            }
            else {
                // passed in a point object...
                x = location.x;
                y = location.y;
            }


            this.masterAlphaTween = this.alphaTween(this.root, 1, 0.5);

            YAHOO.util.Dom.setX(this.root, windowDim.x);
            YAHOO.util.Dom.setY(this.root, windowDim.y);

            function isOOB(me, dimensions) {
                if (typeof (me.arena) == "undefined")
                    return false;

                if ((typeof (me.arena.width) != "undefined") && (windowDim.x + windowDim.width > me.arena.x + me.arena.width))
                    return true;

                if ((typeof (me.arena.height) != "undefined") && (windowDim.y + windowDim.height > me.arena.y + me.arena.height))
                    return true;

                if ((typeof (me.arena.x) != "undefined") && (windowDim.x < me.arena.x))
                    return true;

                if ((typeof (me.arena.y) != "undefined") && (windowDim.y < me.arena.y))
                    return true;

                return false;
            }
        },

        setEnabled: function(enabled) {
            this.enabled = enabled;
            if (!this.enabled)
                hideAllCards();
        },
        
        getEnabled: function(enabled) {
            return this.enabled;
        },

        showCard: function(id, location) {
            this.currentCardId = id;
            var card = this.cardManager.getCard(id);
            if (this.enabled) {
                this.image.onload = gUtils.callback(this, this.onImageLoaded, new Array(location))
                this.image.src = card.img;
            }
        },

        hideCard: function(id) {
            if (this.currentCardId == id) {
                if (this.masterAlphaTween != null)
                    this.masterAlphaTween.stop(false);
                this.masterAlphaTween = this.alphaTween(this.root, 0, 0.5);
            }
        },

        hideAllCards: function() {
            if (this.masterAlphaTween != null)
                this.masterAlphaTween.stop(false);
            this.masterAlphaTween = this.alphaTween(this.root, 0, 0.5);
        },

        onTweenComplete: function(id) {
            this.masterAlphaTween = null;
        },

        alphaTween: function(control, alpha, duration, id, autoAlpha) {
            var tween = new YAHOO.util.Anim(control);
            tween.attributes.opacity = { to: alpha };
            tween.duration = duration;
            tween.method = YAHOO.util.Easing.easeOutStrong;

            if (id != null) {
                this.isTweenComplete[id] = false;
                tween.onComplete.subscribe(gUtils.callback(this, this.onTweenComplete, new Array(id)));
            }

            if (alpha > 0) {
                if (YAHOO.util.Dom.getStyle(control, "display") == "none") {
                    YAHOO.util.Dom.setStyle(control, "opacity", 0);
                    YAHOO.util.Dom.setStyle(control, "display", "block");
                }
                if (alpha == 1) {
                    // Remove transparency properties on completion for 100% alpha tweens, this solves
                    // any funny rendering by some browsers...
                    if (YAHOO.env.ua.ie) {
                        tween.onComplete.subscribe(function() { YAHOO.util.Dom.setStyle(control, "opacity", 1); });
                    } else {
                        tween.onComplete.subscribe(function() { YAHOO.util.Dom.setStyle(control, "opacity", ""); });
                    }
                }
            }
            else if (autoAlpha == null || autoAlpha == true) {
                // Take components out of the display list when they are not visible...
                tween.onComplete.subscribe(function() { YAHOO.util.Dom.setStyle(control, "display", "none"); });
            }

            tween.animate();
            return tween;
        }


    };
    initialiseStatic();
});