﻿var BTC = BTC || {};

/* jQuery plugins */
(function ($) {

    $.fn.extend({
        equalizeSize: function (options) {
            /*
            var ie7 = $('html').is('.ie7'),
            ie8 = $('html').is('.ie8'),
            ie6 = $('html').is('.ie6');
            if(ie7 | ie8 | ie6) {
            return ;
            }
            */

            if (this.length == 0) return this;
            var elms = this, opts = $.extend({}, $.fn.equalizeSize.defaults, options),
				main = $('#main'), updateRequested = false, pending = false,
				updateSimple = function (ev) {
				    var h = 0;
				    elms.each(function () {
				        h = Math.max($(this).height(), h);
				    });
				    elms.height(h);
				    updateRequested = false;
				    pending = false;
				};

            // relayout is a custom event that is fired when a widget's content is rebuilt
            $(window).load(function () {
                updateSimple();
            });

            setInterval(function () {
                if (updateRequested && pending == false) {
                    pending = true;
                    updateSimple();
                }
            }, 200);

            var body = $('body');

            $('body').bind('relayout', function () { updateRequested = true; })

            return this;
        }
    });

    $.fn.equalizeSize.defaults = {};

    jQuery.fn.print0 = function () {
        if (this.size() > 1) {
            this.eq(0).print();
            return;
        } else if (!this.size()) {
            return;
        }

        var strFrameName = ("printer-" + (new Date()).getTime());
        if ($.browser.opera) {
            var tab = window.open("", strFrameName);
            tab.document.open();
            var objDoc = tab.document;
        } else {
            var jFrame = $("<iframe name='" + strFrameName + "'>");
            jFrame
                .css("width", "1px")
                .css("height", "1px")
                .css("position", "absolute")
                .css("left", "-9999px")
                .appendTo($("body:first"))
            ;

            var objFrame = window.frames[strFrameName];
            var objDoc = objFrame.document;
            //        var jStyleDiv = $("<div>").append(//            $("style").clone()//        );//objDoc.write(jStyleDiv.html());
        }

        objDoc.open();
        objDoc.write("<!DOCTYPE html>");
        objDoc.write("<html>");
        objDoc.write("<head>");
        objDoc.write("<link type='text/css' rel='stylesheet' media='print' href='/Content/css/print.css' />");
        objDoc.write("<title>");
        objDoc.write(document.title);
        objDoc.write("</title>");
        if ($('html').is('.ie')) {
            objDoc.write("<script>function printPage(){document.execCommand('print', false, null);}</script>");
        } else {
            objDoc.write("<script>function printPage(){print();}</script>");
        }
        objDoc.write("</head>");
        objDoc.write("<body>");
        objDoc.write(this.html());
        objDoc.write("</body>");
        objDoc.write("</html>");
        objDoc.close();

        ($.browser.opera ? tab : objFrame).focus();
        setTimeout(function () { ($.browser.opera ? tab : objFrame).printPage(); if (tab) { tab.close(); } else { jFrame.remove(); } }, 1000);
    };

})(jQuery);

/* BTC helper functions */
(function ($, ß) {
    var styleTemplate = '<style id="backgroundSwitch" type="text/css">\n';
    styleTemplate += '#main { background: url(/Content/images/bg-raster.png) top left, url(%%url%%) top center no-repeat %%bgcolor%%; }\n';
    styleTemplate += '.no-multiplebgs #main { background: url(/Content/images/bg-raster.png) }\n';
    styleTemplate += '.no-multiplebgs #main-wrapper { background: url(%%url%%) top center no-repeat %%bgcolor%%; }\n';
    styleTemplate += '</style>';

    var popup = $('#widget-content-popup');
    if (popup.length > 0) popup.template('popup');

    $.extend(ß, {
        switchBackground: function (url, bgcolor) {
            $('style#backgroundSwitch').remove();
            $('head').append(styleTemplate.replace(/%%url%%/g, url).replace(/%%bgcolor%%/g, bgcolor));
        },
        todo: function (message, element) {
            if (console && console.debug)
                console.debug(message, element);
            else {
                var e = $(element).css({ outline: '10px dashed hotpink' });
                console.log(message + "\n" + e.html());
            }
        },
        showPopup: function (element, url) {
            $.get(url, function (html) {
                var popup = $.tmpl('popup', { Content: html }).appendTo('body');
                ß.repositionPopup(popup, element);
                popup.load(function () {
                    ß.repositionPopup(popup, element);
                });
            });
        },
        repositionPopup: function (popup, element) {
            popup.css({
                position: 'absolute',
                left: element.offset().left + element.outerWidth() + 20 + 'px',
                top: element.offset().top + element.outerHeight() / 2 - popup.outerHeight() / 2 + 'px'
            });
        }
    });
})(jQuery, BTC);

