//document.write('<link rel="stylesheet" type="text/css" href="css/system/museum_explorer_enhanced.css" media="screen" />');

var MuseumExplorer = {
    initialize: function() {

        MuseumExplorer.addStylesheet();

        if (document.getElementsByTagName("worldmap")) {
            var divs = document.getElementsByTagName("div");
            for (var i = 0; i < divs.length; i++) {
                if (divs[i].className.match("radio")) {
                    divs[i].onmousedown = MuseumExplorer.down;
                    divs[i].onmouseover = MuseumExplorer.over;
                    divs[i].onmouseout = MuseumExplorer.out;
                    divs[i].onmouseup = MuseumExplorer.on;
                    window.onmouseup = MuseumExplorer.clear;
                }
            }
            document.getElementById("explorerReset").onmouseup = MuseumExplorer.reset;



            // clear the inputs if they are selected, if a user has already used the form once
            // and has now used the back button, the browser will remember the last selection,
            // therefore we clear any selected elements on load.
            $("#worldmap fieldset.place input:checked").each(function() {
                $(this).attr('checked', false);
            });

            $("fieldset.subject input:checked").each(function() {
                $(this).attr('checked', false);
            });
        }
    },


    addStylesheet: function() {
        var l = document.createElement("link");
        l.setAttribute("type", "text/css");
        l.setAttribute("rel", "stylesheet");
        l.setAttribute("href", "templates/default/css/museum_explorer/museum_explorer_enhanced.css");
        l.setAttribute("media", "screen");
        document.getElementsByTagName("head")[0].appendChild(l);
    },

    reset: function() {

        // Build an array of divs with a class name of 'radio' and remove 'selected' from each
        var icons = document.getElementsByTagName("div");
        for (var i = 0; i < icons.length; i++) {
            if (icons[i].className.match("radio")) {
                var curr_icon = icons[i];
                if (curr_icon.className == "radio birds birds_selected") {
                    //removeClassName(icons[i], "birds_selected");
                    $(icons[i]).removeClass("birds_selected");
                } else if (curr_icon.className == "radio tools tools_selected") {
                    //removeClassName(icons[i], "tools_selected");
                    $(icons[i]).removeClass("tools_selected");
                } else if (curr_icon.className == "radio death death_selected") {
                    //removeClassName(icons[i], "death_selected");
                    $(icons[i]).removeClass("death_selected");
                } else if (curr_icon.className == "radio gods gods_selected") {
                    //removeClassName(icons[i], "gods_selected");
                    $(icons[i]).removeClass("gods_selected");
                } else if (curr_icon.className == "radio life life_selected") {
                    //removeClassName(icons[i], "life_selected");
                    $(icons[i]).removeClass("life_selected");
                } else if (curr_icon.className == "radio leaders leaders_selected") {
                    //removeClassName(icons[i], "leaders_selected");
                    $(icons[i]).removeClass("leaders_selected");
                } else if (curr_icon.className == "radio dress dress_selected") {
                    //removeClassName(icons[i], "dress_selected");
                    $(icons[i]).removeClass("dress_selected");
                } else if (curr_icon.className == "radio warfare warfare_selected") {
                    //removeClassName(icons[i], "warfare_selected");
                    $(icons[i]).removeClass("warfare_selected");
                }
            }
        }

        // Loop through all the map overlays and hide them
        $("#worldmap div.place").each(function() {
            $(this).hide();
        });

        $("#worldmap fieldset.place div.selected").each(function() {
            $(this).removeClass('selected');
        });
    },

    over: function() {
        // ---------------------------------------------
        //	TOPICS - MouseOver Event
        // ---------------------------------------------


        if (this.className == "radio birds") {
            this.className = "radio birds birds_over";
        }
        else if (this.className == "radio tools") {
            this.className = "radio tools tools_over";
        }
        else if (this.className == "radio death") {
            this.className = "radio death death_over";
        }
        else if (this.className == "radio gods") {
            this.className = "radio gods gods_over";
        }
        else if (this.className == "radio life") {
            this.className = "radio life life_over";
        }
        else if (this.className == "radio leaders") {
            this.className = "radio leaders leaders_over";
        }
        else if (this.className == "radio dress") {
            this.className = "radio dress dress_over";
        }
        else if (this.className == "radio warfare") {
            this.className = "radio warfare warfare_over";
        }


        // ---------------------------------------------
        //	PLACES - MouseOver Event
        // ---------------------------------------------

        else if (this.className == "radio africa") {
            document.getElementById('africa_label').className = "underline";
            swapMap('africa_map', 'over');
        }
        else if (this.className == "radio americas") {
            document.getElementById('americas_label').className = "underline";
            swapMap('americas_map', 'over');
        }
        else if (this.className == "radio anglo") {
            document.getElementById('anglo_label').className = "underline";
            swapMap('anglo_map', 'over');
        }
        else if (this.className == "radio asia") {
            document.getElementById('asia_label').className = "underline";
            swapMap('asia_map', 'over');
        }
        else if (this.className == "radio egypt") {
            document.getElementById('egypt_label').className = "underline";
            swapMap('egypt_map', 'over');
        }
        else if (this.className == "radio europe") {
            document.getElementById('europe_label').className = "underline";
            swapMap('europe_map', 'over');
        }
        else if (this.className == "radio greece") {
            document.getElementById('greece_label').className = "underline";
            swapMap('greece_map', 'over');
        }
        else if (this.className == "radio roman") {
            document.getElementById('roman_label').className = "underline";
            swapMap('roman_map', 'over');
        }
    },

    out: function() {
        // ---------------------------------------------
        //	TOPICS - MouseOut Event
        // ---------------------------------------------

        if (this.className == "radio birds birds_over") {
            this.className = "radio birds";
        }
        else if (this.className == "radio tools tools_over") {
            this.className = "radio tools";
        }
        else if (this.className == "radio death death_over") {
            this.className = "radio death";
        }
        else if (this.className == "radio gods gods_over") {
            this.className = "radio gods";
        }
        else if (this.className == "radio life life_over") {
            this.className = "radio life";
        }
        else if (this.className == "radio leaders leaders_over") {
            this.className = "radio leaders";
        }
        else if (this.className == "radio dress dress_over") {
            this.className = "radio dress";
        }
        else if (this.className == "radio warfare warfare_over") {
            this.className = "radio warfare";
        }


        // ---------------------------------------------
        //	PLACES - MouseOut Event
        // ---------------------------------------------

        else if (this.className == "radio africa") {
            document.getElementById('africa_label').className = "";
            swapMap('africa_map', 'out');
        }
        else if (this.className == "radio americas") {
            document.getElementById('americas_label').className = "";
            swapMap('americas_map', 'out');
        }
        else if (this.className == "radio anglo") {
            document.getElementById('anglo_label').className = "";
            swapMap('anglo_map', 'out');
        }
        else if (this.className == "radio asia") {
            document.getElementById('asia_label').className = "";
            swapMap('asia_map', 'out');
        }
        else if (this.className == "radio egypt") {
            document.getElementById('egypt_label').className = "";
            swapMap('egypt_map', 'out');
        }
        else if (this.className == "radio europe") {
            document.getElementById('europe_label').className = "";
            swapMap('europe_map', 'out');
        }
        else if (this.className == "radio greece") {
            document.getElementById('greece_label').className = "";
            swapMap('greece_map', 'out');
        }
        else if (this.className == "radio roman") {
            document.getElementById('roman_label').className = "";
            swapMap('roman_map', 'out');
        }
    },

    down: function() {
        // ---------------------------------------------
        //	TOPICS - MouseDown Event
        // ---------------------------------------------

        if (this.className == "radio birds birds_over") {
            this.className = "radio birds birds_selected";
        }
        else if (this.className == "radio tools tools_over") {
            this.className = "radio tools tools_selected";
        }
        else if (this.className == "radio death death_over") {
            this.className = "radio death death_selected";
        }
        else if (this.className == "radio gods gods_over") {
            this.className = "radio gods gods_selected";
        }
        else if (this.className == "radio life life_over") {
            this.className = "radio life life_selected";
        }
        else if (this.className == "radio leaders leaders_over") {
            this.className = "radio leaders leaders_selected";
        }
        else if (this.className == "radio dress dress_over") {
            this.className = "radio dress dress_selected";
        }
        else if (this.className == "radio warfare warfare_over") {
            this.className = "radio warfare warfare_selected";
        }


        // ---------------------------------------------
        //	PLACES - MouseDown Event
        // ---------------------------------------------

        else if (this.className == "radio africa") {
            //this.style.backgroundPosition = "2px -58px";
        }
        else if (this.className == "radio americas") {
            //this.style.backgroundPosition = "2px -63px";
        }
        else if (this.className == "radio anglo") {
            //this.style.backgroundPosition = "2px -63px";
        }
        else if (this.className == "radio asia") {
            //this.style.backgroundPosition = "2px -58px";
        }
        else if (this.className == "radio egypt") {
            //this.style.backgroundPosition = "2px -58px";
        }
        else if (this.className == "radio europe") {
            //this.style.backgroundPosition = "2px -58px";
        }
        else if (this.className == "radio greece") {
            //this.style.backgroundPosition = "2px -58px";
        }
        else if (this.className == "radio roman") {
            //this.style.backgroundPosition = "2px -64px";
        }
    },


    on: function() {
        selector = this.getElementsByTagName("input")[0];
        selector.checked = true;


        // ---------------------------------------------
        //	TOPICS - Selected
        // ---------------------------------------------

        if (this.className == "radio birds") {
            this.className = "radio birds birds_selected";
        }
        else if (this.className == "radio tools") {
            this.className = "radio tools tools_selected";
        }
        else if (this.className == "radio death") {
            this.className = "radio death death_selected";
        }
        else if (this.className == "radio gods") {
            this.className = "radio gods death_selected";
        }
        else if (this.className == "radio life") {
            this.className = "radio life life_selected";
        }
        else if (this.className == "radio leaders") {
            this.className = "radio leaders leaders_selected";
        }
        else if (this.className == "radio dress") {
            this.className = "radio dress dress_selected";
        }
        else if (this.className == "radio warfare") {
            this.className = "radio warfare warfare_selected";
        }


        // ---------------------------------------------
        //	PLACES - Selected
        // ---------------------------------------------

        else if (this.className == "radio africa") {
            this.className = "radio africa selected";
            mapOn('africa_map');
        }
        else if (this.className == "radio americas") {
            this.className = "radio americas selected";
            mapOn('americas_map');
        }
        else if (this.className == "radio anglo") {
            this.className = "radio anglo selected";
            mapOn('anglo_map');
        }
        else if (this.className == "radio asia") {
            this.className = "radio asia selected";
            mapOn('asia_map');
        }
        else if (this.className == "radio egypt") {
            this.className = "radio egypt selected";
            mapOn('egypt_map');
        }
        else if (this.className == "radio europe") {
            this.className = "radio europe selected";
            mapOn('europe_map');
        }
        else if (this.className == "radio greece") {
            this.className = "radio greece selected";
            mapOn('greece_map');
        }
        else if (this.className == "radio roman") {
            this.className = "radio roman selected";
            mapOn('roman_map');
        }



        /* Switches off all other topic graphics */

        inputs = document.getElementsByTagName("input");
        for (i = 0; i < inputs.length; i++) {

            if (inputs[i].getAttribute("name") == selector.getAttribute("name")) {
                if (inputs[i] != selector) {


                    // ---------------------------------------------
                    //	TOPICS
                    // ---------------------------------------------

                    if (inputs[i].parentNode.className == "radio birds birds_selected") {
                        inputs[i].parentNode.className = "radio birds";
                    }
                    else if (inputs[i].parentNode.className == "radio tools tools_selected") {
                        inputs[i].parentNode.className = "radio tools";
                    }
                    else if (inputs[i].parentNode.className == "radio death death_selected") {
                        inputs[i].parentNode.className = "radio death";
                    }
                    else if (inputs[i].parentNode.className == "radio gods gods_selected") {
                        inputs[i].parentNode.className = "radio gods";
                    }
                    else if (inputs[i].parentNode.className == "radio life life_selected") {
                        inputs[i].parentNode.className = "radio life";
                    }
                    else if (inputs[i].parentNode.className == "radio leaders leaders_selected") {
                        inputs[i].parentNode.className = "radio leaders";
                    }
                    else if (inputs[i].parentNode.className == "radio dress dress_selected") {
                        inputs[i].parentNode.className = "radio dress";
                    }
                    else if (inputs[i].parentNode.className == "radio warfare warfare_selected") {
                        inputs[i].parentNode.className = "radio warfare";
                    }


                    // ---------------------------------------------
                    //	PLACES
                    // ---------------------------------------------

                    else if (inputs[i].parentNode.className == "radio africa selected") {
                        inputs[i].parentNode.className = "radio africa";
                    }
                    else if (inputs[i].parentNode.className == "radio americas selected") {
                        inputs[i].parentNode.className = "radio americas";
                    }
                    else if (inputs[i].parentNode.className == "radio anglo selected") {
                        inputs[i].parentNode.className = "radio anglo";
                    }
                    else if (inputs[i].parentNode.className == "radio asia selected") {
                        inputs[i].parentNode.className = "radio asia";
                    }
                    else if (inputs[i].parentNode.className == "radio egypt selected") {
                        inputs[i].parentNode.className = "radio egypt";
                    }
                    else if (inputs[i].parentNode.className == "radio europe selected") {
                        inputs[i].parentNode.className = "radio europe";
                    }
                    else if (inputs[i].parentNode.className == "radio greece selected") {
                        inputs[i].parentNode.className = "radio greece";
                    }
                    else if (inputs[i].parentNode.className == "radio roman selected") {
                        inputs[i].parentNode.className = "radio roman";
                    }
                }
            }
        }


    },

    clear: function() {
        divs = document.getElementsByTagName("div");
        for (var i = 0; i < divs.length; i++) {
            if (divs[i].className == "checkbox" || divs[i].className == "radio") {
                removeClassName(divs[i], 'selected');
            } else if (divs[i].className == "checkbox selected" || divs[i].className == "radio selected") {
                removeClassName(divs[i], 'selected');
            }
        }
    }
}

//window.onload = MuseumExplorer.initialize;
DOMAssistant.DOMReady(MuseumExplorer.initialize);



var map_on;
function swapMap(map, state) {
	var maps = new Array('africa_map', 'americas_map', 'anglo_map', 'asia_map', 'egypt_map', 'europe_map', 'greece_map', 'roman_map');

	if (state == 'over') {
		document.getElementById(map).style.display = 'block';
	} else if (state == 'out') {
		document.getElementById(map).style.display = 'none';
	}
}


function mapOn(map) {
	var maps = new Array('africa_map', 'americas_map', 'anglo_map', 'asia_map', 'egypt_map', 'europe_map', 'greece_map', 'roman_map');

	if (document.getElementById('africa_map'))
		document.getElementById('africa_map').style.display = 'none';
	if (document.getElementById('americas_map'))
		document.getElementById('americas_map').style.display = 'none';
	if (document.getElementById('anglo_map'))
		document.getElementById('anglo_map').style.display = 'none';
	if (document.getElementById('asia_map'))
		document.getElementById('asia_map').style.display = 'none';
	if (document.getElementById('egypt_map'))
		document.getElementById('egypt_map').style.display = 'none';
	if (document.getElementById('europe_map'))
		document.getElementById('europe_map').style.display = 'none';
	if (document.getElementById('greece_map'))
		document.getElementById('greece_map').style.display = 'none';
	if (document.getElementById('roman_map'))
		document.getElementById('roman_map').style.display = 'none';

	if (document.getElementById('africa_label'))
		document.getElementById('africa_label').className = '';
	if (document.getElementById('americas_label'))
		document.getElementById('americas_label').className = '';
	if (document.getElementById('anglo_label'))
		document.getElementById('anglo_label').className = '';
	if (document.getElementById('asia_label'))
		document.getElementById('asia_label').className = '';
	if (document.getElementById('egypt_label'))
		document.getElementById('egypt_label').className = '';
	if (document.getElementById('europe_label'))
		document.getElementById('europe_label').className = '';
	if (document.getElementById('greece_label'))
		document.getElementById('greece_label').className = '';
	if (document.getElementById('roman_label'))
		document.getElementById('roman_label').className = '';


	if (map == 'africa_map') {
		document.getElementById('africa_map').style.display = 'block';
		document.getElementById('africa_label').className = 'underline';
		map_on = 'africa_map';
	}
	else if (map == 'americas_map') {
		document.getElementById('americas_map').style.display = 'block';
		document.getElementById('americas_label').className = 'underline';
		map_on = 'americas_map';
	}
	else if (map == 'anglo_map') {
		document.getElementById('anglo_map').style.display = 'block';
		document.getElementById('anglo_label').className = 'underline';
		map_on = 'anglo_map';
	}
	else if (map == 'asia_map') {
		document.getElementById('asia_map').style.display = 'block';
		document.getElementById('asia_label').className = 'underline';
		map_on = 'asia_map';
	}
	else if (map == 'egypt_map') {
		document.getElementById('egypt_map').style.display = 'block';
		document.getElementById('egypt_label').className = 'underline';
		map_on = 'egypt_map';
	}
	else if (map == 'europe_map') {
		document.getElementById('europe_map').style.display = 'block';
		document.getElementById('europe_label').className = 'underline';
		map_on = 'europe_map';
	}
	else if (map == 'greece_map') {
		document.getElementById('greece_map').style.display = 'block';
		document.getElementById('greece_label').className = 'underline';
		map_on = 'greece_map';
	}
	else if (map == 'roman_map') {
		document.getElementById('roman_map').style.display = 'block';
		document.getElementById('roman_label').className = 'underline';
		map_on = 'roman_map';
	}

}
