var arrDownSrc 		= '/newStyle/img/arrow_down.png';
var arrLeftSrc 		= '/newStyle/img/arrow_left.png';
var arrWhiteDownSrc = '/newStyle/img/arrow_down_white.png';
var arrWhiteLeftSrc = '/newStyle/img/arrow_left_white.png';
var keypadName		= 'Keypad_';
var keypadOverName	= 'keypad_active_';

jQuery(document).ready(function(){
	
    //right modules
    $('.subOpener').click(function(){
        var parent = $('#'+this.id).parent();
        var subUl = $('#'+parent.attr('id') + ' + .moduleSub');
        handleModuleSub(subUl, $('#'+this.id + ' .openArrow'));
    });
	
    $('.subOpenerTop').click(function(){
        var parent = $('#'+this.id).parent().parent();
		
        var white = false;
        if (parent.parent().hasClass('blue')) {
            //if module is blue, use white arrow image
            white = true;
        }
		
        var subUl = $('#'+parent.attr('id') + ' + .moduleSub');
        handleModuleSub(subUl, $('#'+this.id + ' .openArrow'), white);
    });
	
    //keypad mouseover
    $('.letterHolder img').mouseover(function(){
        keypadOver(this);
    });
	
    //show hidden archive
    $('a.showHiddenArchive').click(function(){
        var containerId = 'hidden-' + $(this).attr('id');
        if (!$(this).hasClass('open')) { //archived is closed, so open it
            // load archived content here and replace dummy html
			
            // example of how to load correct content:
            var clickedId = $(this).attr('id');
            if (clickedId == 'projects') {
            //load projects
            } else if (clickedId == 'topics') {
            //load topics
            //etc.
            }
			
            //do all the following when content has loaded:
			
            var html = ''; //your loaded html content here
			
            //$('#'+containerId).html(html); //uncomment this to add loaded content to div before opening it
			
            $('#'+containerId).slideDown('slow');
            $(this).css('background-image', 'url(/newStyle/img/open_archive_bg.png)');
			
        } else { //archived is open, so close it
            $('#'+containerId).slideUp('slow');
            $(this).css('background-image', 'url(/newStyle/img/open_archive_right.png)');
        }
		
        $(this).toggleClass('open');
    });

    //show hidden archive
    $('a.showHiddenSiteIndex').click(function(){
        var containerId = 'hidden-' + $(this).attr('id');
        if (!$(this).hasClass('open')) { //archived is closed, so open it
            // load archived content here and replace dummy html

            // example of how to load correct content:
            var clickedId = $(this).attr('id');
            if (clickedId == 'projects') {
            //load projects
            } else if (clickedId == 'topics') {
            //load topics
            //etc.
            }

            //do all the following when content has loaded:

            var html = ''; //your loaded html content here

            //$('#'+containerId).html(html); //uncomment this to add loaded content to div before opening it

            $('#'+containerId).slideDown('slow');

        } else { //archived is open, so close it
            $('#'+containerId).slideUp('slow');
        }

        $(this).toggleClass('open');
    });
});

function handleModuleSub(subObj, imgObj, white) {
    var leftArrow = arrLeftSrc;
    var downArrow = arrDownSrc;
    if (white) {
        leftArrow = arrWhiteLeftSrc;
        downArrow = arrWhiteDownSrc;
    }
    if (subObj.is(':visible')) {
        subObj.slideUp();
        imgObj.attr('src', leftArrow);
    } else {
        subObj.slideDown();
        imgObj.attr('src', downArrow);
    }
}

function keypadOver(ths) {
    var id	 	= $(ths).attr('id');
    var newPath	= '/newStyle/img/keypad/' + keypadOverName + id + '.png';
    $(ths).attr('src', newPath);
	
    $(ths).mouseout(function(){
        var id	 	= $(this).attr('id');
        var newPath	= '/newStyle/img/keypad/' + keypadName + id + '.png';
        $(this).attr('src', newPath);
    });
}
