//    Site-wide Javascript functions

//    Note: If you plan to use jQuery,
// it must be wrappered inside an onReady function declaration

jq(document).ready(function() {
    // jQuery code goes here

    // Hide the button on the programme selector
    jq("#programme_selector_button").css({display: 'none'});

    // Auto submit form on change
    jq("#programme_selector").bind("change", function(e) {
        if (jq(this).val() > 0) {
            jq(this).parent('form').get(0).submit();
        }
    });

    //    Some CSS corrections
    jq("input[type=submit]").addClass('button');
    jq("input[type=checkbox]").css({background: "#fff", border: "0px"});

    //    Tooltip support

    //    This function gets the tooltip to track the movement of the mouse
    jq.fn.tooltipmove = function(e) {
//        var xMouseSpace = 15;
//        var tipWidth = 250;
//        var windowWidth = jq(window).width();
//        var compareWidth = e.pageX + xMouseSpace + tipWidth + 20; // 20 - counteracts addition of scrollbar
//
//        if (compareWidth > windowWidth) {
//            jq("#helptexthover").css("left",  xMouseSpace - tipWidth);
//            jq("#helptexthover").css("top", e.pageY + 20);
//        } else {
//            jq("#helptexthover").css("left", xMouseSpace);
//            jq("#helptexthover").css("top", e.pageY + 20);
//        }
    };

    //    These set up the display and tracking for all the elements that have help text
    jq("form *[help_text]").bind('mouseover', function(e) {
        pos = jq(this).offset();
        jq("#helptexthover").css("top", pos.top + jq(this).height() + 5);
        jq("#helptexthover").css("left", pos.left +250);
        jq("#helptexthover").html(jq(this).attr('help_text')).show();
        //jq(this).addClass("hasFocus");
        //jq("#helptexthover").html(jq(this).attr('help_text')).show();
    });
    jq("form *[help_text]").bind('mouseout', function() {
        if (jq(this).hasClass("hasFocus")) {
        //moving focus was causing problems with selects
        //            jq(this).focus();
        } else {
            jq('#helptexthover').hide();
        }
    });
    jq("form *[help_text]").bind('mousemove', function(e) {
        jq.fn.tooltipmove(e);
    });
    jq("form *[help_text]").bind('focus', function(e) {
        // position under the field we're providing help for
        pos = jq(this).offset();
        jq("#helptexthover").css("top", pos.top + jq(this).height() + 5);
        jq("#helptexthover").css("left", pos.left +250);
        jq("#helptexthover").html(jq(this).attr('help_text')).show();
        jq(this).addClass("hasFocus");
    });
    jq("form *[help_text]").bind('blur', function(e) {
        jq("#helptexthover").hide();
        jq(this).removeClass("hasFocus")
    });

    jq('select#section').bind('change', null, function (e) {

        jq.getJSON(baseUrl+"/publications/publication/ajaxgetsubsections",
            {id: jq(this).val()},
            function(f){
                var options = '';
                var sCurrentValue = jq.trim(
                	jq("select#sub_section_id :selected").text().split(':')[1]
                	);

                for (key in f) {
                    options +=
                    	'<option value="'
                    	+ key
                    	+ '"'
                    	+ ((f[key]==sCurrentValue)? ' selected="selected"':'')
                    	+ '>'
                    	+ f[key]
                    	+ '</option>'
                    	;
                }

                jq("select#sub_section_id").html(options);  
        });

        jq.getJSON(baseUrl+"/publications/publication/ajaxgetpublicationsbysection",
            {id: jq(this).val()},
            function(f){
                var options = '';

                for (key in f) {
                    options += '<option value="' + key + '">' + f[key] + '</option>';
                }

                jq("select#item_id").html(options);  
        });
    });

    jq('select#sub_section_id').bind('change', null, function (e) {

        jq.getJSON(baseUrl+"/publications/publication/ajaxgetpublications",
            {id: jq(this).val()},
            function(f){
                var options = '';

                for (key in f) {
                    options += '<option value="' + key + '">' + f[key] + '</option>';
                }

                jq("select#item_id").html(options);  
        });
    });

    jq('select#previous_address').bind('change', null, function (e) {

        jq.getJSON(baseUrl+"/publications/order/ajaxGetPreviousAddress",
            {id: jq(this).val()},
            function(f){

                for (key in f) {
                    jq('#' + key).val( f[key]);
                }
        });
    });

    // Populate the rest of the dependant HTML.
    jq('select#section').trigger('change'); 
});

window.onload = initPage;

function initPage() {
    if(document.getElementById('browsealoud')) {
        document.getElementById('browsealoud').onmouseover = switch_ba_image;
        document.getElementById('browsealoud').onmouseout = reset_ba_image;
    }
}

function switch_ba_image() {
    this.src = this.src.replace('browse-aloud', 'browse-aloud-hover');
}

function reset_ba_image() {
    this.src = this.src.replace('browse-aloud-hover', 'browse-aloud');
} 

