(function() {
if (typeof SEETOO == "undefined" || typeof SEETOO.UI != "undefined") {
    return;
}

SEETOO.UI = {};

SEETOO.UI.Flash = {

    create: function(id, width, height, movie, flashvars, parent) {
        var align = 'middle';
        var allowScriptAccess = 'sameDomain';
        var quality = 'high';
        var wmode = 'opaque';
        var bgcolor = '#ffffff';
        var menu = 'false';
        var allowFullScreen = true;

        var lines = [];

        lines[lines.length] = '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"';
        lines[lines.length] = ' codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0"';
        lines[lines.length] = ' id="' + id + '"';
        lines[lines.length] = ' width="' + width + '"';
        lines[lines.length] = ' height="' + height + '"';
        lines[lines.length] = ' align="' + align + '">';
        lines[lines.length] = '    <param name="movie" value="' + movie + '">';
        lines[lines.length] = '    <param name="quality" value="' + quality + '">';
        lines[lines.length] = '    <param name="bgcolor" value="' + bgcolor + '">';
        lines[lines.length] = '    <param name="menu" value="' + menu + '">';
        lines[lines.length] = '    <param name="wmode" value="' + wmode + '">';
        lines[lines.length] = '    <param name="allowScriptAccess" value="' + allowScriptAccess + '">';
        lines[lines.length] = '    <param name="allowFullScreen" value="' + allowFullScreen + '">';
        lines[lines.length] = '    <param name="flashvars" value="' + flashvars + '">';
        lines[lines.length] = ' <embed type="application/x-shockwave-flash"';
        lines[lines.length] = '     pluginspage="http://www.macromedia.com/go/getflashplayer"';
        lines[lines.length] = '     name="' + id + '"';
        lines[lines.length] = '     width="' + width + '"';
        lines[lines.length] = '     height="' + height + '"';
        lines[lines.length] = '     align="' + align + '"';
        lines[lines.length] = '     src="' + movie + '"';
        lines[lines.length] = '     quality="' + quality + '"';
        lines[lines.length] = '     bgcolor="' + bgcolor + '"';
        lines[lines.length] = '     menu="' + menu + '"';
        lines[lines.length] = '     wmode="' + wmode + '"';
        lines[lines.length] = '     allowScriptAccess="' + allowScriptAccess + '"';
        lines[lines.length] = '     allowFullScreen="' + allowFullScreen + '"';
        lines[lines.length] = '     flashvars="' + flashvars + '">';
        lines[lines.length] = ' </embed>';
        lines[lines.length] = '</object>';

        if (typeof parent == "undefined") {
            document.write(lines.join(""));
        } else {
            parent.innerHTML = lines.join("");
        }
    },

    select: function(id) {
        var isIE = navigator.appName.indexOf("Microsoft") != -1;
        return isIE ? window[id] : document[id];
    }

};

SEETOO.UI.MessageBox = function() {
    var messageBox = function() {
        var el = document.createElement('div');
        document.body.appendChild(el);

        var messageBox = $(el).empty().addClass('displayBox');
        messageBox.append('<div class="displayBoxTitle"></div>');
        messageBox.append('<div class="displayBoxMessage"></div>');
        messageBox.append('<div class="displayBoxButtons"></div>');
        messageBox.wrapInner('<div class="displayBoxContent"></div>');
        messageBox.wrap(document.createElement('center'));

        return messageBox;
    };

    $(function() {
        messageBox = messageBox();
    });

    return {
        show: function(message, o) {
            var options = {
                title: '',
                width: 360,
                buttons: {
                    'OK': function() {
                        SEETOO.UI.MessageBox.hide();
                    }
                }
            };
            o = o || {};
            $.extend(options, o);

            var offset = 0 - options.width / 2;

            messageBox.css({width: options.width, 'margin-left': offset});
            messageBox.children().children('.displayBoxTitle').text(options.title);

            if (options.title.length > 0) {
                messageBox.children().children('.displayBoxTitle').show();
            } else {
                messageBox.children().children('.displayBoxTitle').hide();
            }

            messageBox.children().children('.displayBoxMessage').empty().append(message);

            var buttonPane = $('.displayBoxButtons').empty();

            $.each(options.buttons, function(name, value) {
                var btn = $(document.createElement('button')).text(name).click(value);
                buttonPane.append(btn);
            });

            if (!messageBox.is(':visible')) {
                messageBox.showModal();
            }
        },

        hide: function() {
            messageBox.hide();
        }
    };
}();

SEETOO.UI.DisplayBox = {
    boxify: function(displayBox, o) {
        o = o || {};

        var options = {
            title: '',
            close: true,
            callback: false
        };

        if ($.data(displayBox.get(0), 'options')) {
            $.extend(options, $.data(displayBox.get(0), 'options'));
        } else if (displayBox.attr('options')) {
            $.extend(options, eval("(" + displayBox.attr('options') + ")"));
        }

        $.extend(options, o);

        $.data(displayBox.get(0), 'options', options);

        var offset = 0 - parseInt(displayBox.width().toString().replace(/px/, ""), 10) / 2;

        $.data(displayBox.get(0), 'close', function() {
            if ($.data(displayBox.get(0), 'closing')) {
                return;
            }

            $.data(displayBox.get(0), 'closing', true);

            if ($.isFunction(options.close)) {
                options.close.call();
            }

            displayBox.hide();

            $.removeData(displayBox.get(0), 'closing');
        });

        $.data(displayBox.get(0), 'callback', function() {
            if ($.isFunction(options.callback)) {
                options.callback.call();
            }
        });

        if (displayBox.children('.displayBoxContent').length == 0) {
            displayBox.wrapInner('<div class="displayBoxContent"></div>').css({display: 'none', 'margin-left': offset});
            displayBox.children('.displayBoxContent').prepend('<div class="displayBoxTitle">' + options.title + '</div>');

            if (options.close) {
                displayBox.children('.displayBoxContent').append('<div class="displayBoxClose"><a href="#">close</a></div>');
                displayBox.children('.displayBoxContent').children('.displayBoxClose').children('a').click(function() {
                    $.data(displayBox.get(0), 'close').call();
                    return false;
                });
            }
        }

        displayBox.children('.displayBoxContent').children('.displayBoxTitle').text(options.title);
    },

    notify: function(message, callback) {
        callback = callback || function() {};
        var displayBox = $('div.displayBox:visible');

        if (displayBox.children('.displayBoxNotification').length == 0) {
            displayBox.prepend('<div class="displayBoxNotification"></div>');
        }

        var notification = displayBox.children('.displayBoxNotification');
        notification.width(displayBox.width() * 0.8);
        notification.css('margin-top', displayBox.children('.displayBoxContent').css('border-top-width'));

        if ($.browser.msie) {
            notification.css('margin-left',  0 - notification.width() / 2);
        } else {
            notification.css('margin-left',  displayBox.width() * 0.2 / 2);
        }

        notification.empty().append(message);
        notification.slideDown('slow', function() {
            setTimeout(function() {
                notification.hide();
                callback.call();
            },
            4000);
        });
    }
};

var getDocumentHeight = function() {
    var scrollHeight = document.compatMode == 'CSS1Compat' ? document.documentElement.scrollHeight : document.body.scrollHeight;
    return Math.max(scrollHeight, getViewportHeight());
};

var getViewportHeight = function() {
    var height = window.innerHeight; // Safari
    var mode = document.compatMode;
    if((mode || $.browser.msie) && !$.browser.opera){
        height = document.compatMode == 'CSS1Compat' ? document.documentElement.clientHeight : document.body.clientHeight;
    }
    return height;
};

var Overlay = function() {
    var overlayCounter = 0;

    var overlayResize = function() {
        overlay.css('height', '100%');

        if ($.browser.msie && $.browser.version == '6.0') {
            overlay.css('height', getDocumentHeight());
        }
    }

    var overlay = function() {
        var el = document.createElement('div');
        document.body.appendChild(el);

        //"display: none; position: absolute; top: 0; left: 0; width: 100%; height: 100%; z-index: 100; background-color: #000000; -moz-opacity: 0.7; opacity: .70; filter: alpha(opacity=70);"
        var overlay = $(el).css({
            'display': 'none',
            'position': 'fixed',
            'margin': 0,
            'padding': 0,
            'top': 0,
            'left': 0,
            'width': '100%',
            'height': '100%',
            'z-index': 100,
            'background-color': '#000000',
            '-moz-opacity': 0.7,
            'opacity': .70,
            'filter': 'alpha(opacity=70)'
        });

        if ($.browser.msie && $.browser.version == '6.0') {
            overlay.css('position', 'absolute');
        }

        window.onresize = function() {
            if (overlayCounter > 0) {
                overlayResize();
            }
        };

        return overlay;
    };

    $(function() {
        overlay = overlay();
    });

    return {
        show: function() {
            if (overlayCounter == 0) {
                overlayResize();
                overlay.show();
            }

            overlayCounter++;
        },

        hide: function() {
            overlayCounter--;

            if (overlayCounter == 0) {
                overlay.hide();
            }
        }
    };
}();

SEETOO.UI.Overlay = {
    show: function() {
        Overlay.show();
    },

    hide: function() {
        Overlay.hide();
    }
};

// extend and override jQuery
(function($) {

    $.fn.showModal = function(o) {
        return this.each(function() {
            if ($(this).hasClass('showModal')) return;
            if (o) SEETOO.UI.DisplayBox.boxify($(this), o);
            if ($.data(this, 'callback')) $.data(this, 'callback').call();
            $(this).addClass('showModal').show();
            Overlay.show();
        });
    };

    $.fn.close = function() {
        return this.each(function() {
            $.data(this, 'close') && $.data(this, 'close').call();
        });
    };

    /*var $show = $.fn.show;
    $.fn.hide = function() {
        return this.each(function() {
            // TODO: if boxified return
            // TODO: boxify
        });
    };*/

    var $hide = $.fn.hide;
    $.fn.hide = function() {
        return this.each(function() {
            if ($.data(this, 'hiding')) {
                return;
            }

            $.data(this, 'hiding', true);

            var $this = $(this);

            $hide.apply($this, arguments);

            if ($this.hasClass('showModal')) {
                $this.removeClass('showModal')
                Overlay.hide();
            }

            $.removeData(this, 'hiding');
        });
    };

})(jQuery);

$(function() {
    // init display boxes
    $('.displayBox:not(:has(.displayBoxContent))').each(function() {
        SEETOO.UI.DisplayBox.boxify($(this));
    });

    // disable enter in forms
    $('input[type="text"]:not(.submit)').keypress(function(event) {
        if (!event) {event = window.event;} return event.keyCode != 13;
    });
});

})();
