


$(document).ready(function(){
    updateCheckBoxes();
    setupHideShow();
})




/**
*   cycle through the page customisation checkboxes and disable child
*   checkboxes for the RSS feed if the panel is disabled
*/
function updateCheckBoxes(evtTrigger) {
    var checked = null;
    $('#customise_page form :checkbox').each(function(i) {
        var id = $(this).attr('id');

        if (id != null) {

            if (id.indexOf('panel_') == 0)
            checked = $(this).attr('checked');

            if (id.indexOf('feed_') == 0) {
                var val = checked ? null : 'disabled';
                $(this).attr('disabled', val);
            }

        }
    });

    if (evtTrigger) {

        var cbObj = document.getElementById(evtTrigger);
        var labelObj = cbObj.parentNode.getElementsByTagName('a')[0];

        labelObj.className = cbObj.checked != true ? 'disabled' : '';

    }

}



// called from the page customisation dialog
function ajaxCustomisePage ()
{
    var id = '#customise_page';

    // ask the user to wait
    $(id + ' .result').html("<img src='" + pathMod + "imgs/loading.gif'>");

    var name = $(id + ' #myname').val();
    var theme = $(id + ' #theme').val();
    var currentpassword = $(id + ' #currentpassword').val();
    var newpassword = $(id + ' #newpassword').val();
    var confirmpassword = $(id + ' #confirmpassword').val();


    // these variables will be undefined if the user is not logged in so
    // we need to null them
    if (name == undefined) name = '';
    if (theme == undefined) theme = '';
    if (currentpassword == undefined) currentpassword = '';
    if (newpassword == undefined) newpassword = '';
    if (confirmpassword == undefined) confirmpassword = '';

    // work out which panels are turned on or off by recurrsing though the collection of checkboxes
    var panelPrefs = new Array();
    var i = 0;
    $(id + ' form input:checkbox').each(function(){
        if ($(this).attr('id').indexOf('panel_') > -1)
        {
            var panel = $(this).attr('id').replace('panel_','');

            // save a value like 'bbcnews=0' if the user has un-checked the checkbox
            panelPrefs[i] =  panel + '=' + ($(this).is(':checked') ? '1' : '0');
            i++;
        }
    });

    // work out which feeds are turned on or off
    var feeds = new Array();
    var i = 0;
    $(id + ' form input:checkbox').each(function(){
        if ($(this).attr('id').indexOf('feed_') > -1)
        {
            var feed = $(this).attr('id').replace('feed_','');

            // save a value like 'bbcnews=0' if the user has un-checked the checkbox
            feeds[i] = feed + '=' + ($(this).is(':checked') ? '1' : '0');
            i++;
        }
    });

    panelPrefs = panelPrefs.toString();
    feeds = feeds.toString();


    var url = pathMod + "customise_page.php";
    var script = true;
    $.getJSON(url, {name: name, currentpassword: currentpassword, newpassword: newpassword, confirmpassword: confirmpassword, theme: theme, panels: panelPrefs, feeds : feeds, script : script}, function(data){
        ajaxDefaultCallBack(id, data, 'default');
    });

    return false;
}

// generic callback frunction to inform the user of the AJAX result
function ajaxDefaultCallBack(id, data, behaviour)
{
    // find DIV.result and show the JSON result of the AJAX call
    var myStringArray = data['general_message'].split("||");
    $(id).find('.result').html(myStringArray[0]);
    $(id).find('.stuff').html(myStringArray[1]);

    if (data['success']) {

        refreshPage();

        //if this is the account settings page, and password has been changed, reset fields to default 'password saved' state
        if (id == "#customise_page" && document.getElementById('pass_fields').style.display == "block") changePass();

    }

}


function setupHideShow ()
{
    // hide the answers
    hideOptions ();
}

function hideOptions ()
{
    // hide the answers
    var answers = document.getElementsByTagName("div");
    for (var i = 0, answer; answer = answers[i]; i++)
    {
        // check if it's a FAQ link
        if (answer.className == 'optionsBox')
        {
            answer.style.display = 'none';
        }
    }

    var links = document.getElementsByTagName("input");
    for (var i = 0, link; link = links[i]; i++)
    {
        // check if it's a FAQ link
        if ( link.id.indexOf('showOptions') > -1 )
        {
            link.value = 'options';
        }
    }

    // make the clickyness work
    doTheDo ();
}

function doTheDo ()
{
    var links = document.getElementsByTagName("a");

    for (var i = 0, link; link = links[i]; i++)
    {
        // open/close buttons
        if ( link.id.indexOf('showOptions') > -1 )
        {
            link.onmouseup = function ()
            {
                hideOptions();

                this.innerHTML = 'feeds&#8593';
                this.onmouseup = function ()
                {
                    this.innerHTML = 'feeds&#8595';
                    hideOptions();
                }

                var idNum = this.id.substr( this.id.indexOf('_') + 1 );

                var answer = document.getElementById('contentBox_' + idNum);
                answer.style.display = 'block';
            }
        }
    }
}