﻿DomBuilder = {
    apply: function(o) {
        o = o || {};
        var els = ("p|div|span|strong|em|img|table|tr|td|th|thead|tbody|tfoot|pre|code" +
					   "|h1|h2|h3|h4|h5|h6|ul|ol|li|form|input|textarea|legend|fieldset|" +
					   "select|option|blockquote|cite|br|hr|dd|dl|dt|address|a|button|abbr|acronym|" +
					   "script|link|style|bdo|ins|del|object|param|col|colgroup|optgroup|caption|" +
					   "label|dfn|kbd|samp|var").split("|");
        var el, i = 0;
        while (el = els[i++]) o[el.toUpperCase()] = DomBuilder.tagFunc(el);
        return o;
    },
    tagFunc: function(tag) {
        return function() {
            var a = arguments, at, ch; a.slice = [].slice; if (a.length > 0) {
                if (a[0].nodeName || typeof a[0] == "string") ch = a; else { at = a[0]; ch = a.slice(1); }
            }
            return DomBuilder.elem(tag, at, ch);
        }
    },
    elem: function(e, a, c) {
        a = a || {}; c = c || [];
        var el = document.createElement(e);
        for (var i in a) if (typeof a[i] != 'function') el.setAttribute(i, a[i]);
        for (var i = 0; i < c.length; i++) {
            if (typeof c[i] == 'string') c[i] = document.createTextNode(c[i]);
            el.appendChild(c[i]);
        }
        return el;
    }
}
function findPosX(obj) {
    var curleft = 0;
    if (obj.offsetParent) {
        while (obj.offsetParent) {
            curleft += obj.offsetLeft
            obj = obj.offsetParent;
        }
    }
    else if (obj.x)
        curleft += obj.x;
    return curleft;
}

function gup( name )
{
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null )
    return "";
  else
    return results[1];
}

function findPosY(obj) {
    var curtop = 0;
    if (obj.offsetParent) {
        while (obj.offsetParent) {
            curtop += obj.offsetTop
            obj = obj.offsetParent;
        }
    }
    else if (obj.y)
        curtop += obj.y;
    return curtop;
}
var on = 0;
function activateGLossaryLinks() {
    var elements = document.getElementsByTagName("a");
    for (var i = 0; i < elements.length; i++) {
        if (elements[i].className == "Glossary") {
            elements[i].setAttribute("desc", elements[i].getAttribute("title"));
            elements[i].setAttribute("hreflang", elements[i].getAttribute("hreflang")); 
            
            // Need to get the locale to set the correct title
            var locale = "en";
            var glossaryTitle = "Glossary";
            
            if (gup('locale') != '')
            {
                locale = gup('locale');
            }
            
            if (locale == "en")
            {           
                elements[i].setAttribute("title", "Click for definition");
            }
            else
            {
                elements[i].setAttribute("title", "Cliciwch ar y diffiniad"); 
                glossaryTitle = "Geirfa";               
            }
            
            elements[i].onclick = function() {
                if (on == 0) {
                    var left = findPosX(this);
                    var top = findPosY(this) + 18;
                    var glossaryTerm = this.getAttribute("desc");
                    var glossaryName = this.innerHTML;
                    var html = DomBuilder.apply();
                    var para = html.DIV({ 'id': 'GlossaryTip', style: 'top:' + top + 'px !important;left:' + left + 'px !important;' },
              html.H6(glossaryTitle),
              html.P(
                  html.STRONG(glossaryName),
                  " " + glossaryTerm
              )
          );
                    document.body.appendChild(para);
                    if (document.all) {
                        para.style.top = top;
                        para.style.left = left;
                    }
                    para.onclick = function() {
                        this.parentNode.removeChild(this);
                        on = 0;
                    }
                }
                on = 1;
                return false;
            }
        }
    }
}
window.onload = function() {
    activateGLossaryLinks();
}
