var img_root;

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}
var sfHover = function() {
	var sfEls = document.getElementById("nav").getElementsByTagName("LI");
	for (var i=0; i<sfEls.length; i++) {
		sfEls[i].onmouseover=function() {
			this.className+=" sfhover";
		}
		sfEls[i].onmouseout=function() {
			this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
		}
	}
}
if (window.attachEvent) window.attachEvent("onload", sfHover);

function toggle_index_graph(index) {
    var toggle_img = document.getElementById("index_toggle_" + index);
    var graph_img  = document.getElementById("index_graph_"  + index);

    if (graph_img.src == "") {
        graph_img.src = img_root + "/chart.gif"; // XXX
        toggle_img.src = img_root + "/uparrow.gif";
        return;
    }

    if (graph_img.style.display == "none") {
        graph_img.style.display = "";
        toggle_img.src = img_root + "/uparrow.gif";
    } else {
        graph_img.style.display = "none";
        toggle_img.src = img_root + "/downarrow.gif";
    }
}

function switch_panel(panel_array, panel_id) {
    for (var i = 0; i < panel_array.length; i++) {
        var p = panel_array[i];
        var p_id;
        var block_ids;
        if ( p.constructor == Array ) {
            p_id = p[0];
            block_ids = p;
        }
        else {
            p_id = p;
            block_ids = new Array( p );
        }

        for (var j = 0; j < block_ids.length; j++) {
            switch_block (block_ids[j], p_id == panel_id);
        }
    }
}

function switch_block(block_id, on) {
    document.getElementById(block_id).style.display = on ? 'block' : 'none';
}

// this function requires prototype.js to be included 
// source = id or element (input field we're using)
// target = id or element (element to mash results into)
//      can be null - if not supplied they'll just get appended 
//      after the input field
// type = security type to filter on (eg stock or bond)
//      can be null
// callback - if you're doing something else in your javascript
//      like validation or anything, provide a valid callback here.
function nzxcode_autocomplete(source, target, type, callback) {

    var source = $(source);

    if (target) {
        target = $(target);
    }
    else {
        target = new Element('div', {'id': 'nzxcode-auto-result'});
        source.parentNode.insert(target);
    }
    
    source.observe('keyup', function(event) {
        var lookup = _nzxcode_lookup.curry(source, target, type, callback);
        lookup(event);
        if (callback) {
            callback(event);
        }
    });
}

//
// private - this is the callback for the event handler on the keypress
function _nzxcode_lookup(source, target, type, callback, event) {

    code = event.element().value;
    if (code == '') {
        target.update(' ');
        return;
    }
    if (event.which == Event.KEY_RETURN 
        || event.which == Event.KEY_TAB) {
        target.update(' ');
        return;
    }
    new Ajax.Request(
        '/nzxcode.json?code=' + code + ((type) ? '&type=' + type : ''),
        {
            'method': 'get', 
            'onSuccess': function(transport) {
                if (!transport.responseText.isJSON()) {
                    return;
                }
                target.update('');
                var table = new Element('table');
                var tbody = new Element('tbody');
                table.insert(tbody);
                var cellcount = 1;
                var row = new Element('tr');
                tbody.insert(row);
                transport.responseText.evalJSON().each(function(item) {
                    var elem = new Element('a', {'id': 'nzxresult-' + item.nzxcode, 'class': 'nzxresult'});
                    elem.insert(item.nzxcode);
                    elem.observe('click', function(clickevent) {
                        source.value = clickevent.element().firstChild.nodeValue;
                        target.update('');
                        if (callback) {
                            callback(event);
                        }
                    });
                    elem.insert(' ');
                    var cell = new Element('td');
                    cell.insert(elem);
                    if (cellcount == 18) {
                        row = new Element('tr');
                        tbody.insert(row);
                    }
                    row.insert(cell);
                    cellcount++;
                    //target.insert(elem);
                });
                tbody.insert(row);
                var header = new Element('strong', {'class': 'gold'});
                header.insert('Suggested Security Codes');
                target.insert(header);
                target.insert(new Element('div', {'id': 'divider'}));
                target.insert(table);
            },
            'onFailure': function() {
                target.update("Something went wrong! Failed to retrieve list of matching securities");
            }
        }
    );
}

/*
 *
 * hidefirst = class of elements to all get hidden
 * klass = class of element to show
 * imgid = id of image to switch plus icon to
 * showplus class of images that have been previously hidden to reshow
 *
 */
function toggle_display (hidefirst, klass, imgid, showplus) {
    $$('.' + hidefirst).each(function(item) {
        item.hide();
    });
    $$('.' + showplus).each(function(item) {
        item.show();
    });
    var togglees = $$("." + klass);
    togglees.each(function(E) { E.toggle(); });
    var oldsrc = $(imgid).src;
    if (togglees[0].visible()) {
        //$(imgid).src = oldsrc.gsub(/(plus|minus).gif$/, 'minus.gif');
        $(imgid).hide();
    } else {
        $(imgid).src = oldsrc.gsub(/(plus|minus).gif$/, 'plus.gif');
    }
}

// This is to show a description on the kiwisaver page
// keeping it simple and atomic, don't trust toggle
function show_kiwisaver(id) {
    $('desc_'+id).removeClassName('hide');
    $('less_'+id).removeClassName('hide');
    $('more_'+id).addClassName('hide');

    $('td_'+id).removeClassName('nopad');
    $('td_'+id).addClassName('content');
}

// This is to hide a description on the kiwisaver page
// keeping it simple and atomic, don't trust toggle
function hide_kiwisaver(id) {
    $('desc_'+id).addClassName('hide');
    $('less_'+id).addClassName('hide');
    $('more_'+id).removeClassName('hide');

    $('td_'+id).removeClassName('content');
    $('td_'+id).addClassName('nopad');
}

