/**
 * @package Archaic_Horizon
 * @copyright Copyright (c) 2009, Dan Bettles 
 * @author Dan Bettles <danbettles@yahoo.co.uk>
 */

var archaichorizon = {

    /**
     * Positions the first element relative to the second
     * 
     * The position is determined by the position parameter, which can be either "l", for left, or "r", for right
     * 
     * @param {jQuery|DomElement} p_oEl
     * @param {jQuery|DomElement} p_oRefEl
     * @param {String} p_position
     */
    alignTo: function (p_oEl, p_oRefEl, p_position) {
        var oElRegion = {},
            oRefElRegion = p_oRefEl.position(),
            spacing = 12,  /*@todo This should be passed as an offset*/
            position = p_position.split('');

        oRefElRegion.width = p_oRefEl.outerWidth();
        oRefElRegion.height = p_oRefEl.outerHeight();

        oElRegion.top = oRefElRegion.top + ((oRefElRegion.height - p_oEl.outerHeight()) / 2);

        switch (position[0]) {
            case 'l':
                oElRegion.left = oRefElRegion.left - spacing - p_oEl.outerWidth();
            break;

            case 'r':
                oElRegion.left = oRefElRegion.left + oRefElRegion.width + spacing;
            break;
        }

        switch (position[1]) {
            case 't':
                oElRegion.top = oRefElRegion.top;
            break;

            case 'c':
                oElRegion.top = oRefElRegion.top + ((oRefElRegion.height - p_oEl.outerHeight()) / 2);
            break;
        }

        p_oEl.hide().css({ left: oElRegion.left, top: oElRegion.top }).show();
    },

    /**
     * @param {String} p_imageUrl
     * @param {jQuery|DomElement|String} p_el
     */
    displayImageBehind: function (p_imageUrl, p_el) {
        var oTargetEl = jQuery(p_el),
            oCssRuleset = {
                'float': oTargetEl.css('float'),
                width: oTargetEl.outerWidth(true),
                height: oTargetEl.outerHeight(true),
                background: 'transparent url(' + p_imageUrl + ') center center no-repeat'
            };

        oTargetEl.wrap(jQuery('<div />').css(oCssRuleset));
    },

    /**
     * Applies a lightbox effect to all links referencing images directly 
     */
    hijaxImageLinks: function () {
        jQuery("a[href$='.jpg'],a[href$='.png']").not('.cover-current a').fancybox();
    },

    /**
     * Fixes the height of the specified element to its height at the time of calling this method
     * 
     * So if, for example, an element is 100 pixels tall after loading, the height of the element will be fixed at 100
     * pixels
     * 
     * @param {String|jQuery|DomElement} p_element
     */
    fixElementHeight: function (p_element) {
        var oEl = jQuery(p_element);
        oEl.height(oEl.height());
    }
};

jQuery(function () {
    //Do this first to allow the page to settle down before measuring containers, or whatnot
    jQuery('body').addClass('hijaxed');

    archaichorizon.hijaxImageLinks();

    jQuery('input.text,textarea')
        .focus(function () {
            jQuery(this).addClass('focussed');
        })
        .blur(function () {
            jQuery(this).removeClass('focussed');
        });
});
