function show_section(section_id) {
    for (var i=0; i<sections.length; i++) {
        if (sections[i] != section_id){
            hide_element(sections[i]);
        }
    }

    show_element(section_id);
}   

function toggle_display(name, id_no) {
    var type;
    if($('#' + name + "_" + id_no).css('display') == 'none') {
        type = 'show';
    } else {
        type = 'hide';
    }

    if(type == 'show'){
        $('#' + name + 'OpenArrow_' + id_no).css('display', 'block');
        $('#' + name + 'OpenArrow_' + id_no).css('visibility', 'visible');
        $('#' + name + 'ClosedArrow_' + id_no).css('display', 'none');
        $('#' + name + 'ClosedArrow_' + id_no).css('visibility', 'hidden');
        show_element(name + "_" + id_no);
    } else {
        $('#' + name + 'OpenArrow_' + id_no).css('display', 'none');
        $('#' + name + 'OpenArrow_' + id_no).css('visibility', 'hidden');
        $('#' + name + 'ClosedArrow_' + id_no).css('display', 'block');
        $('#' + name + 'ClosedArrow_' + id_no).css('visibility', 'visible');
        hide_element(name + "_" + id_no);
    }
}

function hide_element(id) {
    if (document.getElementById) {

        $('#' + id).slideUp('slow', function () {});

        if($('#openArrow_' + id)){
            $('#openArrow_' + id).css('display', 'none');
            $('#openArrow_' + id).css('visibility', 'hidden');
            $('#closedArrow_' + id).css('display', 'block');
            $('#closedArrow_' + id).css('visibility', 'visible');
        }
    }
}

function show_element(id) {
    if (document.getElementById) {
        $('#' + id).slideDown('slow', function () {});

        if($('#openArrow_' + id)){
            $('#openArrow_' + id).css('display', 'block');
            $('#openArrow_' + id).css('visibility', 'visible');
            $('#closedArrow_' + id).css('display', 'none');
            $('#closedArrow_' + id).css('visibility', 'hidden');
        }
    }
}

function show_alert() {
    alert("Success! Thanks for sending us a picture! We'll approve it and then it will show up under this location.");
}

function initSearch(locationNo) {
    var url = 'shared/ajax_search.html?search_type=location;location_no=' + locationNo;

    $('#search_results').html(loading(1));
    $('#search_results').load(url, function () {
        refreshMap('location');
    });
}

function search(type) {
    var url = 'shared/ajax_search.html?search_type=' + type;
    var search_param;

    if ($("#" + type + "_name_foo_textbox").val()){
        search_param = escape($('#' + type + "_name_foo_textbox").val());
        url += ";box_search=1;" + type + "_name=" + search_param;
    } else {
        var search_param = $('#' + type + '_no').val();

        if(type == 'location'){
            url += ';location_no=' + search_param;
        } else {
            url += ';machine_no=' + search_param;
        }
    }

    $('#search_results').html(loading(1));
    $('#search_results').load(url, function () {
        refreshMap(type);
    });
}

function submitForm(){
    $('#file_upload_form').submit();
}

function enterNewMachine(location_no){
    var machine_no = $('#machine_drop_down_' + location_no).val();
    var machine_name = $('#machine_name_' + location_no + '_textbox').val();

    machine_name = machine_name.replace(/&/g, "%26");

    var url = 'shared/ajax_enter_new_machine.html?machine_name=' + machine_name + ';location_no=' + location_no + ';machine_no=' + machine_no;

    $.ajax({ url: url, success: function(){
        displayMachines(location_no);
    }});
}

function displayMachines(location_no){
    $('#showMachines_' + location_no).html(loading(0));
    $('#showMachines_' + location_no).load('shared/ajax_display_machines.html?location_no=' + location_no + ';random=' + Math.random()*99999);
}

function displayHighScore(xref_no){
    $('#showScores_' + xref_no).html(loading(0));
    $('#showScores_' + xref_no).load('shared/ajax_display_high_score.html?xref=' + xref_no);
}

function refreshMap(type){
    var url = 'shared/ajax_map.html?type=' + type;

    var search_param;

    if ($('#' + type + "_name_foo_textbox").val()){
        search_param = escape($('#' + type + "_name_foo_textbox").val());
        url += ";box_search=1;" + type + "_name=" + search_param;
    } else {
        search_param  = $('#' + type + '_no').val();

        if(type == 'location'){
            url += ';location_no=' + search_param;
        } else {
            url += ';machine_no=' + search_param;
        }
    }

    $('#mapDiv').html(loading(0));
    $('#mapDiv').load(url);
}

function enterCondition(url) {
    $.ajax({ url: url, success: function(cond_xref_no){
        var xref_no = trim(cond_xref_no);
        var replaceText = $('#condition_' + xref_no).val();

        $('#condition_rg_display_section_' + xref_no).html(replaceText);
        $('#condition_rg_' + xref_no).css('display', '');
        $('#condition_hv_' + xref_no).css('display', 'none');
    }});
}

function deleteMachine(xref_no) {

    if(confirm("This action will DELETE this machine from this location. You're cool with that?")) {
        $.ajax({ url: "shared/ajax_manage_machines.html?type=remove;xref_no=" + xref_no, success: function(response){
            var xref_no = trim(response);
            $('#info_' + xref_no).fadeOut('slow', function() {});
        }});
    } else {
        alert("Alright then...we'll leave it alone.");
    }
}

function enterHighScore(xref_no) {
    var score = $('#score_' + xref_no).val();

    if(parseInt(score) != score-0){
        $('#score_' + xref_no).val('');
        alert("Please only enter numeric characters for your high score.");
        return;
    }

    var url = 'shared/ajax_enter_high_score.html?xref_no=' + xref_no + ';initials=' + $('#initials_' + xref_no).val() + ';score=' + score + ';rank=' + $('#rank_' + xref_no).val();

    $.ajax({ url: url, success: function(){
        $('#show_high_scores_' + xref_no).css('visibility', 'visible');
        $('#show_high_scores_' + xref_no).css('display', 'block');

        toggle_display("showScores", xref_no);
        displayHighScore(xref_no);
    }});
}

function loading(with_filler)
{
    var loading_html = "";
    if(with_filler){
        loading_html = "<div class='loading' style='height: 500px;'><img class='spinner' src='imgs/spinner_blue.gif' /> Loading <div>";
    } else {
        loading_html = "<div class='loading'><img class='spinner' src='imgs/spinner_blue.gif' /> Loading <div>";
    }

    return loading_html;
}

function trim(str) {
    return str.replace(/^\s+|\s+$/g,"");
}

function createEditDiv(key, column, obj_type, value) {
    var obj_type_hidden;
    var box = $('#' + obj_type + '_edit_box_' + key + '_' + column);

    var newInput = document.createElement('input');
    newInput.setAttribute('name', 'field_' + key + '_' + column);
    newInput.setAttribute('size', 18);
    newInput.setAttribute('type', 'text');
    newInput.setAttribute('value', value);

    if ($('#obj_' + key).length > 0) {
    } else {
        obj_type_hidden = document.createElement('input');
        obj_type_hidden.setAttribute('type', 'hidden');
        obj_type_hidden.setAttribute('value', obj_type);
        obj_type_hidden.setAttribute('name', 'obj_' + key);
        $(box).append(obj_type_hidden);
    }

    $(box).append(newInput);
}

function createDropdownDiv(key, column, obj_type, value, existingObjArrayName, existingObjArrayKey) {
    var obj_type_hidden;
    var box = $('#edit_dropdown_' + key + '_' + column);

    var newSelect = document.createElement('select');
    newSelect.setAttribute('name', 'field_' + key + '_' + column);

    for ( var i in existingObjArrayName) {
        var newOption = document.createElement('option');
        newOption.text = existingObjArrayName[i];
        newOption.value = existingObjArrayKey[i];
        if (existingObjArrayKey[i] == value) {
            newOption.selected = true;
        }
        newSelect.add(newOption, null);
    }

    if ($('#obj_' + key).length > 0) {
    } else {
        obj_type_hidden = document.createElement('input');
        obj_type_hidden.setAttribute('type', 'hidden');
        obj_type_hidden.setAttribute('value', obj_type);
        obj_type_hidden.setAttribute('name', 'obj_' + key);
        $(box).append(obj_type_hidden);
    }

    $(box).append(newSelect);
}

startList = function() {
    if (document.all&&document.getElementById) {
        navRoot = $("#header");
        for (i=0; i<navRoot.childNodes.length; i++) {
            node = navRoot.childNodes[i];
            if (node.nodeName=="LI") {
                node.onmouseover=function() {
                    this.className+=" over";
                }
                node.onmouseout=function() {
                    this.className=this.className.replace(" over", "");
                }
            }
        }
    }
}
window.onload=startList;
