/* common classes/functions */

/* jquery style nav setup - set all A.navMO under #nav2 to add class 'navHighlight' onmouseover */
var syxmover = {

    plcache: new Array(),

    init: function() {
        var navBoxen = $('#nav2 > div > a');
        //console.log(navBoxen);
        var nonSel = navBoxen.filter( function(e) { return ! this.className.match(/\bnavSel\b/); } );

        nonSel.mouseover( function(e){ $(this).addClass('navHighlight'); });
        nonSel.mouseout( function(e){ $(this).removeClass('navHighlight'); });

        nonSel.each( function() { syxmover.preload($(this)); } );

        //navBoxen.filter('.navSel').click( function(e){ return false; });

        var subnav = $('#subnav2');
        if (subnav) {
            var subnavBoxen = $('#subnav2 > ul > li > a');
            var subnonSel = subnavBoxen.filter( function(e) { return ! this.className.match(/\bnavSel\b/); } );

            subnonSel.mouseover( function(e){ $(this).addClass('navHighlight'); });
            subnonSel.mouseout( function(e){ $(this).removeClass('navHighlight'); });

            subnonSel.each( function() { syxmover.preload(this); } );

            //subnavBoxen.filter('.navSel').click( function(e){ return false; });
        }

        syxmover.preload($('#btnLogin'))
        $('#btnLogin').mouseover( function(e){ $(this).addClass('navHighlight'); });
        $('#btnLogin').mouseout( function(e){ $(this).removeClass('navHighlight'); });

    },

    /* hack to create URLs out of background-image prop of each elem, and then
     * preloading them by creating a permanent Image() out of them, as well as
     * the mouseover version given by XXXXXXXX_on.gif */
    preload: function (elem) {
        var def = $(elem).css('background-image').replace(/url\(['"]?([^)"']+)['"]?\)/, '$1');
        if (def != 'none') {
            var onsrc = def.replace(/\.([^.]+)$/, '_on.$1');
            this.imgFactory(def);
            this.imgFactory(onsrc);
        }
    },

    imgFactory: function(src) {
        var i = new Image();
        i.src = src;
        this.plcache[this.plcache.length] = i;
    }

};



$(document).ready(function(){
    syxmover.init();

    /* open all external links in new window (class 'ext') */
    $('a.ext').attr('target', '_new');

    /* get rid of the focus indicator in the nav, by req */
    $('#nav2, #subnav2').find('a').click( function() { $(this).blur(); });

    /* OFT - all elements with class 'clickstate' have their background image swapped out when they are clicked */
    $('div.clickstate').bind('click', function(e) {
          $(this).css('backgroundImage', $(this).css('backgroundImage').replace(/\.([a-z]+["']?\))$/, '.clicky.$1'));
    });

    /* but you won't see the new image because it doesn't have time to load
     * before the page changes, maybe. So we pre-load all of them by building a
     * little cache of Image() objects. Now they will show up instantly. */
    document.imageCache = new Array();
    $('div.clickstate').each( function(e) {
          var newsrc = $(this).css('backgroundImage').replace(/url\(["']?(.*?)\.([a-z]+)["']?\)$/, '$1.clicky.$2');
          if (newsrc) {
              var img = new Image;
              img.src = newsrc;
              document.imageCache[document.imageCache.length] = img;
          }
    });


});
