﻿///<reference path="/TESCO.js" />
///<reference path="/event.js" />
///<reference path="/eventManager.js" />
///<reference path="/uri.js" />

TESCO.$("UI.Window").Relationship = (function() {

    var _query = "rel=";
    var _relationships = {};
    var _populated = false;
    var _window = {
        "url": new TESCO.system.URI(location.toString()),
        "rel": null
    }

    if (_window.url.search) {
        _window.rel = (("?" + _window.url.search).getValueFromQuery("rel"));
    }
    
    function _getRelNode(node) {
        return TESCO.system.DOM.node.getAncestorByAttributeRegExp(node, "rel", new RegExp(""), 3);
    }

    TESCO.system.event.document.addEventListener("load",
        function() {
            if (_populated) {
                var _links = document.getElementsByTagName("a");
                if (_links) {
                    for (var i = 0, L = _links.length; i < L; i++) {
                        var _rel = _links[i].attributes["rel"];
                        if (_rel) {
                            //    check if this link is in a pop up of this relationship type
                            if (_window.rel != _rel.nodeValue) {
                                if (_relationships[_rel.nodeValue]) {
                                    var _msg = TESCO.locale.window[_rel.nodeValue];
                                    _links[i].setAttribute("title", _msg);
                                    _links[i].appendChild(TESCO.locale.window.node(_msg));
                                    TESCO.system.DOM.node.addClassName(_links[i], _rel.nodeValue + "Link");
                                }
                            }
                        }
                    }
                }
            }
        }
    );

    TESCO.system.event.document.addEventListener("beforeload",
        function() {
            TESCO.system.event.attach(document.body, "click",
                function(e) {
                    var _relationship;
                    var _node = _getRelNode(e.target);
                    if (_relationship = _constructor.getFromNode(_node)) {
                        if (_node.href) {
                            var _url = new TESCO.system.URI(_node.href);
                            if (!_url.search) {
                                _url.search = _query + _relationship.rel;
                            } else {
                                _url.search += "&" + _query + _relationship.rel;
                            }
                            _relationship.popup.url = _url.toString();
                            _relationship.popup.open();
                            e.prevent();
                        }
                    }
                }
            );
        }
    );

    function _constructor(rel, options) {
        if (!_relationships[rel]) {
            _relationships[rel] = {
                "popup": new TESCO.system.Window(null, rel, options),
                "rel": rel
            }
            _populated = true;
        }
        return this;
    }

    _constructor.prototype.NAME = "TESCO.UI.Window.Relationship";
    _constructor.prototype.VERSION = "1.0.0";

    _constructor.getFromNode = function(node) {
        return node ? _relationships[node.getAttribute("rel")] : null;
    }

    return _constructor;
})();

