﻿//initExpandPanel($('#SearchPanel'), $('#SearchButton'), new function () { onSelect(); }, new function () { onCancel(); });
Function.prototype.startsWith = function () { return false; };

function showDialog(div, closeButton) {

    closeButton.click(function () { 
        div.slideUp("fast"); 
        showMaster();

        return false;
    });

    fadeMaster(div);
    expandPanel.slideDown("slow"); 
 
    return false;
}

function initExpandPanel(expandPanel, expandButton, closeButton, hiddenStatusField) {

    $(document).ready(function () {

        expandPanel.hide();  
        setExpandButton(expandPanel, expandButton, hiddenStatusField);
        setCloseButton(expandPanel, closeButton, hiddenStatusField);

    });
}
function refreshExpandPanel(expandPanel, hiddenStatusField, expandButton) {
    if (hiddenStatusField != null && hiddenStatusField.value == 1) {
        fadeMaster(expandPanel, true);
        expandPanel.show();
        setExpandButton(expandPanel, expandButton, hiddenStatusField);
    }
    else {

        $('#fade').remove();

    }
}
function setCloseButton(expandPanel, closeButton, hiddenStatusField) {

    closeButton.click(function () {


        expandPanel.slideUp("fast");
        hiddenStatusField.value = 0;
        showMaster();

        return false;
    });
}
function setExpandButton(expandPanel, expandButton, hiddenStatusField) {

    expandButton.click(function () {

        if (expandPanel.is(":hidden")) {
            hiddenStatusField.value = 1;
            showExpandPanel(expandPanel, hiddenStatusField);
        } else {
            hiddenStatusField.value = 0;
            expandPanel.slideUp("slow");
            showMaster();
        }
        return false;
    });
}

function showExpandPanel(expandPanel, hiddenStatusField) {
    fadeMaster(expandPanel);
    expandPanel.slideDown("slow");
    hiddenStatusField.value = 1;
}
function hideExpandPanel(expandPanel, hiddenStatusField) {
    hiddenStatusField.value = 0;
    expandPanel.slideUp("fast");
    showMaster();


}

function showMaster() {
    $('#fade').fadeOut(function () {
        $('#fade').remove();  //fade them both out
    });

}
function fadeMaster(expandPanel, immediate) {
    //Fade in Background
    $('body').append('<div id="fade"></div>'); //Add the fade layer to bottom of the body tag.

    if (immediate)
        $('#fade').css({ 'filter': 'alpha(opacity=50)' }).fadeIn(); //Fade in the fade layer - .css({'filter' : 'alpha(opacity=80)'}) is used to fix the IE Bug on fading transparencies
    else
        $('#fade').css({ 'filter': 'alpha(opacity=50)' }).show(); //Fade in the fade layer - .css({'filter' : 'alpha(opacity=80)'}) is used to fix the IE Bug on fading transparencies

    if ($('#fade').live != null) {
        $('#fade').live('click', function () { //When clicking on the close or fade layer...


            expandPanel.slideUp("fast");
            $('#fade').fadeOut(function () {
                $('#fade').remove();  //fade them both out
            });
            return false;
        });
    }

    return false;
}

/**************************************************************************************************************************************
/**
* Roar - Notifications
*
* Inspired by Growl
*
* @version		1.0.1
*
* @license		MIT-style license
* @author		Harald Kirschner <mail [at] digitarald.de>
* @copyright	Author
**************************************************************************************************************************************/

var RoarError = new Class({

    Implements: [Options, Events, Chain],

    options: {
        duration: 3000,
        position: 'upperLeft',
        container: null,
        bodyFx: null,
        itemFx: null,
        margin: { x: 10, y: 10 },
        offset: 10,
        className: 'roar',
        onShow: $empty,
        onHide: $empty,
        onRender: $empty
    },

    initialize: function (options) {
        this.setOptions(options);
        this.items = [];
        this.container = $(this.options.container) || document;
    },

    alert: function (title, message, options) {
        var params = Array.link(arguments, { title: String.type, message: String.type, options: Object.type });
        var items = [new Element('h3', { 'html': $pick(params.title, '') })];
        if (params.message) items.push(new Element('p', { 'html': params.message }));
        return this.inject(items, params.options);
    },

    inject: function (elements, options) {
        if (!this.body) this.render();
        options = options || {};

        var offset = [-this.options.offset, 0];
        var last = this.items.getLast();
        if (last) {
            offset[0] = last.retrieve('roar:offset');
            offset[1] = offset[0] + last.offsetHeight + this.options.offset;
        }
        var to = { 'opacity': 1 };
        to[this.align.y] = offset;

        var item = new Element('div', {
            'class': this.options.className,
            'opacity': 0
        }).adopt(
			new Element('div', {
			    'class': 'roar-bg-error',
			    'opacity': 0.7
			}),
			elements
		);

        item.setStyle(this.align.x, 0).store('roar:offset', offset[1]).set('morph', $merge({
            unit: 'px',
            link: 'cancel',
            onStart: Chain.prototype.clearChain,
            transition: Fx.Transitions.Back.easeOut
        }, this.options.itemFx));

        var remove = this.remove.create({
            bind: this,
            arguments: [item],
            delay: 10
        });
        this.items.push(item.addEvent('click', remove));

        if (this.options.duration) {
            var over = false;
            var trigger = (function () {
                trigger = null;
                if (!over) remove();
            }).delay(this.options.duration);
            item.addEvents({
                mouseover: function () {
                    over = true;
                },
                mouseout: function () {
                    over = false;
                    if (!trigger) remove();
                }
            });
        }
        item.inject(this.body).morph(to);
        return this.fireEvent('onShow', [item, this.items.length]);
    },

    remove: function (item) {
        var index = this.items.indexOf(item);
        if (index == -1) return this;
        this.items.splice(index, 1);
        item.removeEvents();
        var to = { opacity: 0 };
        to[this.align.y] = item.getStyle(this.align.y).toInt() - item.offsetHeight - this.options.offset;
        item.morph(to).get('morph').chain(item.destroy.bind(item));
        return this.fireEvent('onHide', [item, this.items.length]).callChain(item);
    },

    empty: function () {
        while (this.items.length) this.remove(this.items[0]);
        return this;
    },

    render: function () {
        this.position = this.options.position;
        if ($type(this.position) == 'string') {
            var position = { x: 'center', y: 'center' };
            this.align = { x: 'left', y: 'top' };
            if ((/left|west/i).test(this.position)) position.x = 'left';
            else if ((/right|east/i).test(this.position)) this.align.x = position.x = 'right';
            if ((/upper|top|north/i).test(this.position)) position.y = 'top';
            else if ((/bottom|lower|south/i).test(this.position)) this.align.y = position.y = 'bottom';
            this.position = position;
        }
        this.body = new Element('div', { 'class': 'roar-body' }).inject(document.body);
        if (Browser.Engine.trident4) this.body.addClass('roar-body-ugly');
        this.moveTo = this.body.setStyles.bind(this.body);
        this.reposition();
        if (this.options.bodyFx) {
            var morph = new Fx.Morph(this.body, $merge({
                unit: 'px',
                chain: 'cancel',
                transition: Fx.Transitions.Circ.easeOut
            }, this.options.bodyFx));
            this.moveTo = morph.start.bind(morph);
        }
        var repos = this.reposition.bind(this);
        window.addEvents({
            scroll: repos,
            resize: repos
        });
        this.fireEvent('onRender', this.body);
    },

    reposition: function () {
        var max = document.getCoordinates(), scroll = document.getScroll(), margin = this.options.margin;
        max.left += scroll.x;
        max.right += scroll.x;
        max.top += scroll.y;
        max.bottom += scroll.y;
        var rel = ($type(this.container) == 'element') ? this.container.getCoordinates() : max;
        this.moveTo({
            left: (this.position.x == 'right')
				? (Math.min(rel.right, max.right) - margin.x)
				: (Math.max(rel.left, max.left) + margin.x),
            top: (this.position.y == 'bottom')
				? (Math.min(rel.bottom, max.bottom) - margin.y)
				: (Math.max(rel.top, max.top) + margin.y)
        });
    }

});



var RoarMessage = new Class({

    Implements: [Options, Events, Chain],

    options: {
        duration: 3000,
        position: 'upperLeft',
        container: null,
        bodyFx: null,
        itemFx: null,
        margin: { x: 10, y: 10 },
        offset: 10,
        className: 'roar',
        onShow: $empty,
        onHide: $empty,
        onRender: $empty
    },

    initialize: function (options) {
        this.setOptions(options);
        this.items = [];
        this.container = $(this.options.container) || document;
    },

    alert: function (title, message, options) {
        var params = Array.link(arguments, { title: String.type, message: String.type, options: Object.type });
        var items = [new Element('h3', { 'class': 'roarMessageh3', 'html': $pick(params.title, '') })];
        if (params.message) items.push(new Element('p', { 'html': params.message }));
        return this.inject(items, params.options);
    },

    inject: function (elements, options) {
        if (!this.body) this.render();
        options = options || {};

        var offset = [-this.options.offset, 0];
        var last = this.items.getLast();
        if (last) {
            offset[0] = last.retrieve('roar:offset');
            offset[1] = offset[0] + last.offsetHeight + this.options.offset;
        }
        var to = { 'opacity': 1 };
        to[this.align.y] = offset;

        var item = new Element('div', {
            'class': this.options.className,
            'opacity': 0
        }).adopt(
			new Element('div', {
			    'class': 'roar-bg-message',
			    'opacity': 0.7
			}),
			elements
		);

        item.setStyle(this.align.x, 0).store('roar:offset', offset[1]).set('morph', $merge({
            unit: 'px',
            link: 'cancel',
            onStart: Chain.prototype.clearChain,
            transition: Fx.Transitions.Back.easeOut
        }, this.options.itemFx));

        var remove = this.remove.create({
            bind: this,
            arguments: [item],
            delay: 10
        });
        this.items.push(item.addEvent('click', remove));

        if (this.options.duration) {
            var over = false;
            var trigger = (function () {
                trigger = null;
                if (!over) remove();
            }).delay(this.options.duration);
            item.addEvents({
                mouseover: function () {
                    over = true;
                },
                mouseout: function () {
                    over = false;
                    if (!trigger) remove();
                }
            });
        }
        item.inject(this.body).morph(to);
        return this.fireEvent('onShow', [item, this.items.length]);
    },

    remove: function (item) {
        var index = this.items.indexOf(item);
        if (index == -1) return this;
        this.items.splice(index, 1);
        item.removeEvents();
        var to = { opacity: 0 };
        to[this.align.y] = item.getStyle(this.align.y).toInt() - item.offsetHeight - this.options.offset;
        item.morph(to).get('morph').chain(item.destroy.bind(item));
        return this.fireEvent('onHide', [item, this.items.length]).callChain(item);
    },

    empty: function () {
        while (this.items.length) this.remove(this.items[0]);
        return this;
    },

    render: function () {
        this.position = this.options.position;
        if ($type(this.position) == 'string') {
            var position = { x: 'center', y: 'center' };
            this.align = { x: 'left', y: 'top' };
            if ((/left|west/i).test(this.position)) position.x = 'left';
            else if ((/right|east/i).test(this.position)) this.align.x = position.x = 'right';
            if ((/upper|top|north/i).test(this.position)) position.y = 'top';
            else if ((/bottom|lower|south/i).test(this.position)) this.align.y = position.y = 'bottom';
            this.position = position;
        }
        this.body = new Element('div', { 'class': 'roar-body' }).inject(document.body);
        if (Browser.Engine.trident4) this.body.addClass('roar-body-ugly');
        this.moveTo = this.body.setStyles.bind(this.body);
        this.reposition();
        if (this.options.bodyFx) {
            var morph = new Fx.Morph(this.body, $merge({
                unit: 'px',
                chain: 'cancel',
                transition: Fx.Transitions.Circ.easeOut
            }, this.options.bodyFx));
            this.moveTo = morph.start.bind(morph);
        }
        var repos = this.reposition.bind(this);
        window.addEvents({
            scroll: repos,
            resize: repos
        });
        this.fireEvent('onRender', this.body);
    },

    reposition: function () {
        var max = document.getCoordinates(), scroll = document.getScroll(), margin = this.options.margin;
        max.left += scroll.x;
        max.right += scroll.x;
        max.top += scroll.y;
        max.bottom += scroll.y;
        var rel = ($type(this.container) == 'element') ? this.container.getCoordinates() : max;
        this.moveTo({
            left: (this.position.x == 'right')
				? (Math.min(rel.right, max.right) - margin.x)
				: (Math.max(rel.left, max.left) + margin.x),
            top: (this.position.y == 'bottom')
				? (Math.min(rel.bottom, max.bottom) - margin.y)
				: (Math.max(rel.top, max.top) + margin.y)
        });
    }

});



