/* 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');
        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' && !def.match(/\.at\./)) {
            var onsrc = def.replace(/\.([^.]+)$/, '.hov.$1');
            this.imgFactory(def);
            this.imgFactory(onsrc);
        }
    },

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

};


/* note: following brilliant equalizeCols() jQuery plugin ripped from code at
 * http://www.tomdeater.com/jquery/equalize_columns/ and used in FCEContact.js
 * right now for LP only. */
/**
 *
 * Copyright (c) 2007 Tom Deater (http://www.tomdeater.com)
 * Licensed under the MIT License:
 * http://www.opensource.org/licenses/mit-license.php
 *
 */
(function($) {
        /**
         * equalizes the heights of all elements in a jQuery collection
         * thanks to John Resig for optimizing this!
         * usage: $("#col1, #col2, #col3").equalizeCols();
         */

        $.fn.equalizeCols = function(){
                var height = parseInt(this.css('min-height')) || 0 ;
                //console.log('min: %d', height);

                return this
                        .css("height", "auto")
                        .each(function() {
                                height = Math.max(height, this.offsetHeight);
                                //console.log('%s : %dpx', $(this).attr('id'), height);
                        })
                        .css("height", height)
                        .each(function() {
                                var h = this.offsetHeight;
                                if (h > height) {
                                        $(this).css("height", height - (h - height));
                                };
                        });

        };

})(jQuery);
/** end equalizeCols **/


/**
 * fake a :hover selector in CSS for elements that can't have it (IOW anything
 * other than an <a> in IE6). Will add the specified className to the element
 * when the mouse is over it, and remove when not.
 *
 *
 * usage: $("#crazyButton").hoverClass('over');
 *
 */
(function($) {
        $.fn.hoverClass = function(className){
        return this.bind('mouseover', function() { $(this).addClass(className) })
                   .bind('mouseout', function() { $(this).removeClass(className) });
        };
})(jQuery);


/* vertCenterContainer(): for lofts, used to vertically center the Floor Plate
 * image's container, by setting a negative margin-top equal to 1/2 the floor
 * plate image height.  Could be used for most any element for which height()
 * returns something useful. */
(function($) {
        $.fn.vertCenterContainer = function() {
            //console.log('vertCenterContainer() %x: ', $(this).height());
            return $(this).parent().css('margin-top', $(this).height()/2 * -1).addClass('loaded'); 
        };
})(jQuery);


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

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

});

