/*
 * base.js
 * Amiel Martin
 * 2009-10-14
 */



var Base = (function(){
	var base = _js_vars || {};


	if (base.DEBUG && window.console) {
		base.console = window.console;
	} else {
		var f = function(){};
		base.console = { log:f, debug:f, info:f, warn:f, error:f, assert:f, dir:f, dirxml:f, trace:f, group:f, groupEnd:f, time:f, timeEnd:f, profile:f, profileEnd:f, count:f };
	}

	return base;
})();
/**
 * Boxy 0.1.4 - Facebook-style dialog, with frills
 *
 * (c) 2008 Jason Frame
 * Licensed under the MIT License (LICENSE)
 */

/*
 * jQuery plugin
 *
 * Options:
 *   message: confirmation message for form submit hook (default: "Please confirm:")
 *
 * Any other options - e.g. 'clone' - will be passed onto the boxy constructor (or
 * Boxy.load for AJAX operations)
 */
jQuery.fn.boxy = function(options) {
    options = options || {};
    return this.each(function() {
        var node = this.nodeName.toLowerCase(), self = this;
        if (node == 'a') {
            jQuery(this).click(function() {
                var active = Boxy.linkedTo(this),
                    href = this.getAttribute('href'),
                    localOptions = jQuery.extend({actuator: this, title: this.title}, options);

                if (href.match(/(&|\?)boxy\.modal/)) localOptions.modal = true;

                if (active) {
                    active.show();
                } else if (href.indexOf('#') >= 0) {
                    var content = jQuery(href.substr(href.indexOf('#'))),
                        newContent = content.clone(true);
                    content.remove();
                    localOptions.unloadOnHide = false;
                    new Boxy(newContent, localOptions);
                } else if (href.match(/\.(jpe?g|png|gif|bmp)($|\?)/i)) {
                    localOptions.unloadOnHide = true;
                    Boxy.loadImage(this.href, localOptions);
                } else { // fall back to AJAX; could do with a same-origin check
                    if (!localOptions.cache) localOptions.unloadOnHide = true;
                    Boxy.load(this.href, localOptions);
                }

                return false;
            });
        } else if (node == 'form') {
            jQuery(this).bind('submit.boxy', function() {
                Boxy.confirm(options.message || 'Please confirm:', function() {
                    jQuery(self).unbind('submit.boxy').submit();
                });
                return false;
            });
        }
    });
};


function Boxy(element, options) {

    this.boxy = jQuery(Boxy.WRAPPER);
    jQuery.data(this.boxy[0], 'boxy', this);

    this.visible = false;
    this.options = jQuery.extend({}, Boxy.DEFAULTS, options || {});

    if (this.options.modal) {
        this.options = jQuery.extend(this.options, {center: true, draggable: false});
    }

    if (this.options.actuator) {
        jQuery.data(this.options.actuator, 'active.boxy', this);
    }

    this.setContent(element || "<div></div>");
    this._setupTitleBar();

    this.boxy.css('display', 'none').appendTo(document.body);
    this.toTop();

    if (this.options.fixed) {
        if (Boxy.IE6) {
            this.options.fixed = false; // IE6 doesn't support fixed positioning
        } else {
            this.boxy.addClass('fixed');
        }
    }

    if (this.options.center && Boxy._u(this.options.x, this.options.y)) {
        this.center();
    } else {
        this.moveTo(
            Boxy._u(this.options.x) ? Boxy.DEFAULT_X : this.options.x,
            Boxy._u(this.options.y) ? Boxy.DEFAULT_Y : this.options.y
        );
    }

    if (this.options.show) this.show();

};

Boxy.EF = function() {};

jQuery.extend(Boxy, {

    WRAPPER:    "<table cellspacing='0' cellpadding='0' border='0' class='boxy-wrapper'>" +
                "<tr><td class='boxy-top-left'></td><td class='boxy-top'></td><td class='boxy-top-right'></td></tr>" +
                "<tr><td class='boxy-left'></td><td class='boxy-inner'></td><td class='boxy-right'></td></tr>" +
                "<tr><td class='boxy-bottom-left'></td><td class='boxy-bottom'></td><td class='boxy-bottom-right'></td></tr>" +
                "</table>",

    DEFAULTS: {
        title:                  null,           // titlebar text. titlebar will not be visible if not set.
        closeable:              true,           // display close link in titlebar?
        draggable:              true,           // can this dialog be dragged?
        clone:                  false,          // clone content prior to insertion into dialog?
        actuator:               null,           // element which opened this dialog
        center:                 true,           // center dialog in viewport?
        show:                   true,           // show dialog immediately?
        modal:                  false,          // make dialog modal?
        fixed:                  true,           // use fixed positioning, if supported? absolute positioning used otherwise
        closeText:              '[close]',      // text to use for default close link
        unloadOnHide:           false,          // should this dialog be removed from the DOM after being hidden?
        clickToFront:           false,          // bring dialog to foreground on any click (not just titlebar)?
        behaviours:             Boxy.EF,        // function used to apply behaviours to all content embedded in dialog.
        afterDrop:              Boxy.EF,        // callback fired after dialog is dropped. executes in context of Boxy instance.
        afterShow:              Boxy.EF,        // callback fired after dialog becomes visible. executes in context of Boxy instance.
        afterHide:              Boxy.EF,        // callback fired after dialog is hidden. executed in context of Boxy instance.
        beforeUnload:           Boxy.EF,        // callback fired after dialog is unloaded. executed in context of Boxy instance.
        hideFade:               false,
        hideShrink:             'vertical'
    },

    IE6:                (jQuery.browser.msie && jQuery.browser.version < 7),
    DEFAULT_X:          50,
    DEFAULT_Y:          50,
    MODAL_OPACITY:      0.7,
    zIndex:             1337,
    dragConfigured:     false, // only set up one drag handler for all boxys
    resizeConfigured:   false,
    dragging:           null,

    load: function(url, options) {

        options = options || {};

        var ajax = {
            url: url, type: 'GET', dataType: 'html', cache: false, success: function(html) {
                html = jQuery(html);
                if (options.filter) html = jQuery(options.filter, html);
                new Boxy(html, options);
            }
        };

        jQuery.each(['type', 'cache'], function() {
            if (this in options) {
                ajax[this] = options[this];
                delete options[this];
            }
        });

        jQuery.ajax(ajax);

    },

    loadImage: function(url, options) {
        var img = new Image();
        img.onload = function() {
            new Boxy($('<div class="boxy-image-wrapper"/>').append(this), options);
        };
        img.src = url;
    },

    get: function(ele) {
        var p = jQuery(ele).parents('.boxy-wrapper');
        return p.length ? jQuery.data(p[0], 'boxy') : null;
    },

    linkedTo: function(ele) {
        return jQuery.data(ele, 'active.boxy');
    },

    alert: function(message, callback, options) {
        return Boxy.ask(message, ['OK'], callback, options);
    },

    confirm: function(message, after, options) {
        return Boxy.ask(message, ['OK', 'Cancel'], function(response) {
            if (response == 'OK') after();
        }, options);
    },

    ask: function(question, answers, callback, options) {

        options = jQuery.extend({modal: true, closeable: false},
                                options || {},
                                {show: true, unloadOnHide: true});

        var body = jQuery('<div></div>').append(jQuery('<div class="question"></div>').html(question));

        var buttons = jQuery('<form class="answers"></form>');
        buttons.html(jQuery.map(Boxy._values(answers), function(v) {
            return "<input type='button' value='" + v + "' />";
        }).join(' '));

        jQuery('input[type=button]', buttons).click(function() {
            var clicked = this;
            Boxy.get(this).hide(function() {
                if (callback) {
                    jQuery.each(answers, function(i, val) {
                        if (val == clicked.value) {
                            callback(answers instanceof Array ? val : i);
                            return false;
                        }
                    });
                }
            });
        });

        body.append(buttons);

        new Boxy(body, options);

    },

    isModalVisible: function() {
        return jQuery('.boxy-modal-blackout').length > 0;
    },

    _u: function() {
        for (var i = 0; i < arguments.length; i++)
            if (typeof arguments[i] != 'undefined') return false;
        return true;
    },

    _values: function(t) {
        if (t instanceof Array) return t;
        var o = [];
        for (var k in t) o.push(t[k]);
        return o;
    },

    _handleResize: function(evt) {
        jQuery('.boxy-modal-blackout').css('display', 'none')
                                      .css(Boxy._cssForOverlay())
                                      .css('display', 'block');
    },

    _handleDrag: function(evt) {
        var d;
        if (d = Boxy.dragging) {
            d[0].boxy.css({left: evt.pageX - d[1], top: evt.pageY - d[2]});
        }
    },

    _nextZ: function() {
        return Boxy.zIndex++;
    },

    _viewport: function() {
        var d = document.documentElement, b = document.body, w = window;
        return jQuery.extend(
            jQuery.browser.msie ?
                { left: b.scrollLeft || d.scrollLeft, top: b.scrollTop || d.scrollTop } :
                { left: w.pageXOffset, top: w.pageYOffset },
            !Boxy._u(w.innerWidth) ?
                { width: w.innerWidth, height: w.innerHeight } :
                (!Boxy._u(d) && !Boxy._u(d.clientWidth) && d.clientWidth != 0 ?
                    { width: d.clientWidth, height: d.clientHeight } :
                    { width: b.clientWidth, height: b.clientHeight }) );
    },

    _setupModalResizing: function() {
        if (!Boxy.resizeConfigured) {
            var w = jQuery(window).resize(Boxy._handleResize);
            if (Boxy.IE6) w.scroll(Boxy._handleResize);
            Boxy.resizeConfigured = true;
        }
    },

    _cssForOverlay: function() {
        if (Boxy.IE6) {
            return Boxy._viewport();
        } else {
            return {width: '100%', height: jQuery(document).height()};
        }
    }

});

Boxy.prototype = {

    estimateSize: function() {
        this.boxy.css({visibility: 'hidden', display: 'block'});
        var dims = this.getSize();
        this.boxy.css('display', 'none').css('visibility', 'visible');
        return dims;
    },

    getSize: function() {
        return [this.boxy.width(), this.boxy.height()];
    },

    getContentSize: function() {
        var c = this.getContent();
        return [c.width(), c.height()];
    },

    getPosition: function() {
        var b = this.boxy[0];
        return [b.offsetLeft, b.offsetTop];
    },

    getCenter: function() {
        var p = this.getPosition();
        var s = this.getSize();
        return [Math.floor(p[0] + s[0] / 2), Math.floor(p[1] + s[1] / 2)];
    },

    getInner: function() {
        return jQuery('.boxy-inner', this.boxy);
    },

    getContent: function() {
        return jQuery('.boxy-content', this.boxy);
    },

    setContent: function(newContent) {
        newContent = jQuery(newContent).css({display: 'block'}).addClass('boxy-content');
        if (this.options.clone) newContent = newContent.clone(true);
        this.getContent().remove();
        this.getInner().append(newContent);
        this._setupDefaultBehaviours(newContent);
        this.options.behaviours.call(this, newContent);
        return this;
    },

    moveTo: function(x, y) {
        this.moveToX(x).moveToY(y);
        return this;
    },

    moveToX: function(x) {
        if (typeof x == 'number') this.boxy.css({left: x});
        else this.centerX();
        return this;
    },

    moveToY: function(y) {
        if (typeof y == 'number') this.boxy.css({top: y});
        else this.centerY();
        return this;
    },

    centerAt: function(x, y) {
        var s = this[this.visible ? 'getSize' : 'estimateSize']();
        if (typeof x == 'number') this.moveToX(x - s[0] / 2);
        if (typeof y == 'number') this.moveToY(y - s[1] / 2);
        return this;
    },

    centerAtX: function(x) {
        return this.centerAt(x, null);
    },

    centerAtY: function(y) {
        return this.centerAt(null, y);
    },

    center: function(axis) {
        var v = Boxy._viewport();
        var o = this.options.fixed ? [0, 0] : [v.left, v.top];
        if (!axis || axis == 'x') this.centerAt(o[0] + v.width / 2, null);
        if (!axis || axis == 'y') this.centerAt(null, o[1] + v.height / 2);
        return this;
    },

    centerX: function() {
        return this.center('x');
    },

    centerY: function() {
        return this.center('y');
    },

    resize: function(width, height, after) {
        if (!this.visible) return;
        var bounds = this._getBoundsForResize(width, height);
        this.boxy.css({left: bounds[0], top: bounds[1]});
        this.getContent().css({width: bounds[2], height: bounds[3]});
        if (after) after(this);
        return this;
    },

    tween: function(width, height, after) {
        if (!this.visible) return;
        var bounds = this._getBoundsForResize(width, height);
        var self = this;
        this.boxy.stop().animate({left: bounds[0], top: bounds[1]});
        this.getContent().stop().animate({width: bounds[2], height: bounds[3]}, function() {
            if (after) after(self);
        });
        return this;
    },

    isVisible: function() {
        return this.visible;
    },

    show: function() {
        if (this.visible) return;
        if (this.options.modal) {
            var self = this;
            Boxy._setupModalResizing();
            this.modalBlackout = jQuery('<div class="boxy-modal-blackout"></div>')
                .css(jQuery.extend(Boxy._cssForOverlay(), {
                    zIndex: Boxy._nextZ(), opacity: Boxy.MODAL_OPACITY
                })).appendTo(document.body);
            this.toTop();
            if (this.options.closeable) {
                jQuery(document.body).bind('keypress.boxy', function(evt) {
                    var key = evt.which || evt.keyCode;
                    if (key == 27) {
                        self.hide();
                        jQuery(document.body).unbind('keypress.boxy');
                    }
                });
            }
        }
		this.getInner().stop().css({width: '', height: ''});
        this.boxy.stop().css({opacity: 1}).show();
        this.visible = true;
        this.boxy.find('.close:first').focus();
        this._fire('afterShow');
        return this;
    },

    hide: function(after) {

		if (!this.visible) return;

		var self = this;

		if (this.options.modal) {
            jQuery(document.body).unbind('keypress.boxy');
            this.modalBlackout.animate({opacity: 0}, function() {
                jQuery(this).remove();
            });
        }

		var target = { boxy: {}, inner: {} },
			tween = 0,
			hideComplete = function() {
				self.boxy.css({display: 'none'});
				self.visible = false;
				self._fire('afterHide');
				if (after) after(self);
				if (self.options.unloadOnHide) self.unload();
			};

		if (this.options.hideShrink) {
			var inner = this.getInner(), hs = this.options.hideShrink, pos = this.getPosition();
			tween |= 1;
			if (hs === true || hs == 'vertical') {
				target.inner.height = 0;
				target.boxy.top = pos[1] + inner.height() / 2;
			}
			if (hs === true || hs == 'horizontal') {
				target.inner.width = 0;
				target.boxy.left = pos[0] + inner.width() / 2;
			}
		}

		if (this.options.hideFade) {
			tween |= 2;
			target.boxy.opacity = 0;
		}

		if (tween) {
			if (tween & 1) inner.stop().animate(target.inner, 300);
			this.boxy.stop().animate(target.boxy, 300, hideComplete);
		} else {
			hideComplete();
		}

		return this;

	},

    toggle: function() {
        this[this.visible ? 'hide' : 'show']();
        return this;
    },

    hideAndUnload: function(after) {
        this.options.unloadOnHide = true;
        this.hide(after);
        return this;
    },

    unload: function() {
        this._fire('beforeUnload');
        this.boxy.remove();
        if (this.options.actuator) {
            jQuery.data(this.options.actuator, 'active.boxy', false);
        }
    },

    toTop: function() {
        this.boxy.css({zIndex: Boxy._nextZ()});
        return this;
    },

    getTitle: function() {
        return jQuery('> .title-bar h2', this.getInner()).html();
    },

    setTitle: function(t) {
        jQuery('> .title-bar h2', this.getInner()).html(t);
        return this;
    },


    _getBoundsForResize: function(width, height) {
        var csize = this.getContentSize();
        var delta = [width - csize[0], height - csize[1]];
        var p = this.getPosition();
        return [Math.max(p[0] - delta[0] / 2, 0),
                Math.max(p[1] - delta[1] / 2, 0), width, height];
    },

    _setupTitleBar: function() {
        if (this.options.title) {
            var self = this;
            var tb = jQuery("<div class='title-bar'></div>").html("<h2>" + this.options.title + "</h2>");
            if (this.options.closeable) {
                tb.append(jQuery("<a href='#' class='close'></a>").html(this.options.closeText));
            }
            if (this.options.draggable) {
                tb[0].onselectstart = function() { return false; };
                tb[0].unselectable = 'on';
                tb[0].style.MozUserSelect = 'none';
                if (!Boxy.dragConfigured) {
                    jQuery(document).mousemove(Boxy._handleDrag);
                    Boxy.dragConfigured = true;
                }
                tb.mousedown(function(evt) {
                    self.toTop();
                    Boxy.dragging = [self, evt.pageX - self.boxy[0].offsetLeft, evt.pageY - self.boxy[0].offsetTop];
                    jQuery(this).addClass('dragging');
                }).mouseup(function() {
                    jQuery(this).removeClass('dragging');
                    Boxy.dragging = null;
                    self._fire('afterDrop');
                });
            }
            this.getInner().prepend(tb);
            this._setupDefaultBehaviours(tb);
        }
    },

    _setupDefaultBehaviours: function(root) {
        var self = this;
        if (this.options.clickToFront) {
            root.click(function() { self.toTop(); });
        }
        jQuery('.close', root).click(function() {
            self.hide();
            return false;
        }).mousedown(function(evt) { evt.stopPropagation(); });
    },

    _fire: function(event) {
        this.options[event].call(this);
    }

};

$(document).ready(function() {
	$('#quick_links a, #our_sponsors, #vaude_texting a').fadeTo('fast', 0.5).hover(function() {
		$(this).stop().fadeTo(400, 1);
	}, function() {
		$(this).stop().fadeTo(400, 0.5);
	});

	Boxy.MODAL_OPACITY = 0.3;
	$('#say_hello a, #newsletter a').boxy({modal:true});

	if ($('#flash_panel').length > 0) {
		new Boxy($('#flash_panel').html(), { title: 'Notice', modal:true });
	}

	$('#quick_links').hover(function() {
		$(this).stop().animate({ bottom: '0px' });
	}, function() {
		$(this).stop().animate({ bottom: '-100px' });
	}).hover();
});
