// Simple JavaScript abstraction layer
// (C) 2009 Blue Cube Media Ltd


function bcmLayer ()
{
    var httpReq = false;
    var httpResponseText = false;


    // private method gets the XMLHttpRequest object

    function getHttpReq ()
    {
        if (window.XMLHttpRequest)
            httpReq = new XMLHttpRequest ();
        else if (window.ActiveXObject)
        {
            if (!(httpReq = new ActiveXObject ('Microsoft.XMLHTTP')))
                httpReq = new ActiveXObject ('Msxml2.XMLHTTP');
        }
    }


    // add user-defined ready function if given & send AJAX request

    this.httpGetRequest = function (strUrl, fnReadyFunction)
    {
        if (!httpReq)
            getHttpReq ();

        if (httpReq)
        {
            if (arguments.length == 2 && typeof (fnReadyFunction) != 'undefined')
            {
                httpReq.onreadystatechange = function ()
                {
                    if (httpReq.readyState == 4 && httpReq.status == 200)
                    {
                        httpResponseText = httpReq.responseText;
                        fnReadyFunction ();
                    }
                }
            }

            httpReq.open ('GET', strUrl, true);
            httpReq.send (null);
        }
    }


    // make response text available to user

    this.getHttpResponseText = function ()
    {
        return httpResponseText;
    }
}


// safely add a window onload handler

bcmLayer.prototype.addWindowOnload = function (fnNewOnload)
{
    if (typeof (window.addEventListener) != 'undefined')
        window.addEventListener ('load', fnNewOnload, false);
    else if (typeof (window.attachEvent) != 'undefined')
        window.attachEvent ('onload', fnNewOnload);
    else
    {
        if (window.onload != null)
        {
            var fnOldOnload = window.onload;
            window.onload = function () { fnOldOnload (); fnNewOnload (); };
        }
        else
            window.onload = fnNewOnload;
    }
}


// this is actually supported in FF3 already

bcmLayer.prototype.getElementsByClassName = function (strClassName, objNode)
{
	if (arguments.length == 1 && typeof (objNode) == 'undefined')
		var objNode = document.getElementsByTagName('body')[0];

	var arrReturnElements = [];
	var strRegEx = new RegExp ('\\b' + strClassName + '\\b');
	var arrElements = objNode.getElementsByTagName ('*');

	for (var i = 0; i < arrElements.length; ++i)
	{
		if (strRegEx.test (arrElements[i].className))
			arrReturnElements.push (arrElements[i]);
	}

	return arrReturnElements;
}


// get all links by "rel" attribute

bcmLayer.prototype.getLinksByRel = function (strRel)
{
    var arrReturnLinks = [];
    var arrLinks = document.getElementsByTagName ('a');

    for (var i = 0; i < arrLinks.length; ++i)
    {
        if (arrLinks[i].getAttribute('rel') && arrLinks[i].getAttribute('rel') == strRel)
            arrReturnLinks.push (arrLinks[i]);
    }

    return arrReturnLinks;
}


// get an element's style regardless of where it was set

bcmLayer.prototype.getElementStyle = function (objElement, strSelector)
{
    var strStyle = false;

    if (arguments.length == 2)
    {
        if (objElement.style[strSelector])
            strStyle = objElement.style[strSelector];
        else if (objElement.currentStyle)
        {
            var intHyphenIndex = strSelector.indexOf ('-');

            if (intHyphenIndex !== false)
            {
                strSelector = strSelector.replace (/-/, '');
                strSelector = strSelector.substr(0, intHyphenIndex) + strSelector.charAt(intHyphenIndex).toUpperCase() + strSelector.substr(intHyphenIndex + 1);
            }

            strStyle = objElement.currentStyle[strSelector];
        }
        else if (window.getComputedStyle)
            strStyle = document.defaultView.getComputedStyle(objElement, null).getPropertyValue(strSelector);

        if (!strStyle)
            strStyle = false;
    }

    return strStyle;
}


// set an element's style (for the sake of completeness)

bcmLayer.prototype.setElementStyle = function (objElement, strSelector, strStyle)
{
    if (arguments.length == 3)
    {
        var intHyphenIndex = strSelector.indexOf ('-');

        if (intHyphenIndex !== false)
        {
            strSelector = strSelector.replace (/-/, '');
            strSelector = strSelector.substr(0, intHyphenIndex) + strSelector.charAt(intHyphenIndex).toUpperCase() + strSelector.substr(intHyphenIndex + 1);
        }

        objElement.style[strSelector] = strStyle;
    }
}


// get an element's opacity

bcmLayer.prototype.getElementOpacity = function (objElement)
{
    if (arguments.length == 1)
    {
        var intOpacity = this.getElementStyle (objElement, 'opacity')

        if (intOpacity)
            intOpacity *= 100;
        else
        {
            var strFilter = this.getElementStyle (objElement, 'filter');

            if (strFilter)
                return parseInt (strFilter.substr (strFilter.indexOf ('opacity=') + 8));
            else
                intOpacity = 100;
        }

        return intOpacity;
    }

    return false;
}


// set an element's opacity

bcmLayer.prototype.setElementOpacity = function (objElement, intOpacity)
{
    if (arguments.length == 2)
    {
        objElement.style.opacity = intOpacity / 100;

        if (objElement.filters && objElement.filters[0] && typeof (objElement.filters[0].opacity) == 'number')
        {
            objElement.style.zoom = 1;
            objElement.filters[0].opacity = intOpacity;
        }
        else
        {
            objElement.style.zoom = 1;
            objElement.style.filter = 'alpha(opacity=' + intOpacity + ')';
        }
    }
}


// get selected text

bcmLayer.prototype.getSelectedText = function ()
{
    var strText = '';

    if (window.getSelection)
        strText = window.getSelection();
    else if (document.getSelection)
        strText = document.getSelection();
    else if (document.selection)
        strText = document.selection.createRange().text;

    if (strText == '')
    {
        var arrTextareas = document.getElementsByTagName ('textarea');

        for (var i = 0; i < arrTextareas.length && strText == ''; ++i)
            strText = (arrTextareas[i].value).substring (arrTextareas[i].selectionStart, arrTextareas[i].selectionEnd);
    }

    return strText;
}


// set selected text in a textarea

bcmLayer.prototype.setSelectedText = function (strText, objTextarea)
{
    var strBefore = '';
    var strAfter = '';

    if (arguments.length == 1 && typeof (objTextarea) == 'undefined')
    {
        var arrTextareas = document.getElementsByTagName ('textarea');

        for (var i = 0; i < arrTextareas.length && typeof (objTextarea) == 'undefined'; ++i)
        {
            if (arrTextareas[i].selectionStart != arrTextareas[i].selectionEnd)
                var objTextarea = arrTextareas[i];
        }
    }

    if (typeof (objTextarea) != 'undefined')
    {
        strBefore = objTextarea.value.substring (0, objTextarea.selectionStart);
        strAfter = objTextarea.value.substring (objTextarea.selectionEnd);

        objTextarea.value = strBefore + strText + strAfter;
    }
}


bcm = new bcmLayer ();