/*
THIS IS A CODE FILE. UPDATES SHOULD BE MADE TO THIS CODE AND COMPRESSED INTO THE LIVE FILE.
*/

//stores the active popup so we know if there is one
var _popup = null,
    _popupbackground = null,
    closeCurrentPopup = null;

//function opens the popup with fade in. takes the content from div 'popupcontent'
function openpopup(id) {
    var _document = document,
        _form = document.body,
        _body = document.getElementById("aspnetForm"),
        _window = window,
        _contentstore = _document.getElementById("popupcontent"),
        _content = _document.getElementById(id),
        _container = _document.createElement("table"),
        _clickdiv = _document.createElement("div"),
        _startcss = "background-image:url(/images/popup/",
        _endcss = ".png);height:10px;width:10px;",
        _bimagecss = _startcss + "b.png);",
        _absolute = "position:absolute;",
        _style = "style",
        _csstext = "cssText",
        _posx = _body.offsetWidth / 2,
        _posy = (_form.scrollTop || _form.parentNode.scrollTop) + 100,
        _close = document.createElement("img");

    //if theres a popup then remove it
    if (_popup) {
        _body.removeChild(_popup);
        _body.removeChild(_popupbackground);
        _body.removeChild(_close);
        _popup = null;
    }

    //create the table with 3 rows and 3 columns
    for (var i = 0; i < 3; i++) {
        var _row = _container.insertRow(0);

        for (var j = 0; j < 3; j++)
            _row.insertCell(0);
    }

    _container.cellPadding = 0;
    _container.cellSpacing = 0;

    //store variables for the rows to keep file size down
    var _rows = _container.rows,
        _row0 = _rows[0],
        _row1 = _rows[1],
        _row2 = _rows[2];

    //set the css and background images for the cells
    _row0.cells[0][_style][_csstext] = _startcss + "tl" + _endcss;
    _row0.cells[2][_style][_csstext] = _startcss + "tr" + _endcss;
    _row2.cells[0][_style][_csstext] = _startcss + "bl" + _endcss;
    _row2.cells[2][_style][_csstext] = _startcss + "br" + _endcss;
    _row0.cells[1][_style][_csstext] = _bimagecss;
    _row1.cells[0][_style][_csstext] = _bimagecss;
    _row1.cells[2][_style][_csstext] = _bimagecss;
    _row2.cells[1][_style][_csstext] = _bimagecss;
    _row1.cells[1].appendChild(_content);

    //start the content off with an opacity of 0
    _content[_style].opacity = 0;
    _content[_style].filter = "alpha(opacity=0)";
    _content[_style].zindex = 99;

    //Special case for Iframes as they don't hide properly
    var _iframes = _content.getElementsByTagName("iframe");
    for (var i in _iframes) if (typeof (_iframes[i].style) != 'undefined') _iframes[i].style.display = "";

    //append then style the divs
    _body.appendChild(_clickdiv);
    _clickdiv[_style][_csstext] = "position:fixed;_" + _absolute + "height:100%;_height:200%;width:100%;top:0px;left:0px;z-index:99;background-color:#000000;filter:alpha(opacity=60);opacity:0.6;cursor:pointer;";
    _body.appendChild(_container);
    _container[_style][_csstext] = "position:absolute;top:" + _posy + "px;left:" + (_posx - (_content.offsetWidth / 2)) + "px;z-index:100;";

    //Draw the close button
    _close[_style][_csstext] = "position:absolute;top:" + (_posy - 3) + "px;left:" + (_posx + (_content.offsetWidth / 2)) + "px;z-index:9999;cursor:pointer;height:27px;width:26px";
    _close.src = "http://www.postcodeanywhere.co.uk/assets/icons/small/modal-cross.png";
    _close.onclick = fade;
    _body.appendChild(_close);

    //store a reference to the popup
    _popup = _container;
    _popupbackground = _clickdiv;

    //closes the popup
    function close() {
        if (_popup) {
            //Special case for Iframes as they don't hide properly
            var _iframes = _content.getElementsByTagName("iframe");
            for (var i in _iframes) if (typeof (_iframes[i].style) != 'undefined') _iframes[i].style.display = "none";
            _contentstore.appendChild(_content);
            _body.removeChild(_container);
            _body.removeChild(_clickdiv);
            _body.removeChild(_close);
            _popup = null;
        }
    }

    //animates the appearance of the popup (IE is happy with 10 frames of animation)
    function appear() {
        var _current = 0;

        function animate() {
            _current += 10;
            _content.style.opacity = (_current / 100);
            _content.style.filter = "alpha(opacity=" + _current + ")";

            if (_current < 100)
                _window.setTimeout(animate, 25);
        }

        animate();
    }

    //animates the popup fade (IE can only cope with 5 frames now)
    function fade() {
        var _current = 100;

        function animate() {
            _current -= 20;
            _container.style.opacity = (_current / 100);
            _container.style.filter = "alpha(opacity=" + _current + ")";

            if (_current > 0)
                _window.setTimeout(animate, 50);
            else
                close();
        }

        animate();
    }

    //set the page fade div click to close the popup
    _clickdiv.onclick = fade;

    //set a reference to allow external controls to close the popup
    closeCurrentPopup = fade;

    //show the popup
    appear();
}

function animatebanner(elements) {
    var _lowlight = 0,
        _highlight = 100,
        _seqitem = 0,
        _sequence = [
                function () { },
                function () { },
                function () { fade([elements[0]], _highlight, _lowlight); fade([elements[1]], _lowlight, _highlight) },
                function () { },
                function () { fade([elements[1]], _highlight, _lowlight); fade([elements[2]], _lowlight, _highlight) },
                function () { },
                function () { fade([elements[2]], _highlight, _lowlight); fade([elements[0]], _lowlight, _highlight); restart() }
            ];

    function setOpacity(image, value) {
        image.style.opacity = (value / 100);
        image.style.filter = "alpha(opacity=" + value + ")";
    }

    function fade(ittelements, start, finish) {
        var _ittalpha = start,
            _ittdelta = start < finish ? 5 : -5;

        function itterate() {
            _ittalpha += _ittdelta;

            for (var i in ittelements)
                setOpacity(ittelements[i], _ittalpha);

            if (start < finish ? _ittalpha < finish : _ittalpha > finish)
                window.setTimeout(itterate, 25);
        }

        itterate();
    }

    function restart() {
        _seqitem = 0;
    }

    function runSequence() {
        if (_seqitem < _sequence.length) {
            _sequence[_seqitem]();
            _seqitem++
            window.setTimeout(runSequence, 4000);
        }
    }

    //Initialise elements to low light
    for (var i = 1; i < elements.length; i++)
        setOpacity(elements[i], _lowlight); ;

    runSequence();
}

//Helper functions
function textboxHi(which) {
    which.style.backgroundColor = '#FFF';
}

function textboxLo(which) {
    which.style.backgroundColor = '#F4F4F4';
}

function openPrivacy() {
    window.open('/privacy.aspx', 'privacy', "toolbar=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=1,width=600,height=500");
}

function openPrivacyCookies() {
    window.open('/privacy.aspx#cookies', 'privacy', "toolbar=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=1,width=600,height=500");
}

function openFeedbackForm(id, subject) {
    var subjectField = document.getElementById('hdnSubjectId').value;
    document.getElementById(subjectField).value = subject;
    if (document.getElementById('lgdFeedback')) {
        document.getElementById('lgdFeedback').innerHTML = subject + ' - Your feedback';
    }
    openpopup(id);
}
