jQuery(function($) {

    $('#modal-background,#modal-content2').remove();

    var 
        isBlocked = false,
		$win = $(window),
		$doc = $(document),

		$background = $('<div id="modal-background"/>').appendTo('body'),
		$content = $('<div id="modal-content2"/>').appendTo('body'),
		$title = $('<div id="modal-title"/>').appendTo($content),
		$innercontent = $('<div id="modal-innercontent"/>').appendTo($content),
		$bottom = $('<div id="modal-bottom"/>').appendTo($content),
		$btnClose = $('<button id="modal-close" title="Stäng fönstret"/>').appendTo($content);

    $.fn.iceModal = function(title, content, popup, blocked) {

        var showFn = function() {
            isBlocked = !!blocked;
            $content.find(':not(#modal-title,#modal-close, #modal-innercontent, #modal-bottom)').remove();
            $title.text(title);
            $background.css('height', $doc.height()).show();
            $content.css('top', ($(window).height() / 2) + $(window).scrollTop()).show();
            $innercontent.append(content);

            var titleHeight = $title.outerHeight(), titleCSS = { height: '102px' };

            if (titleHeight < 60) titleCSS.lineHeight = '65px';            

            $title.css(titleCSS);

            return false;
        };


        // Close button click
        $('button[id="modal-close"],#modal-background').click(function() {
            if (isBlocked) return false;
            $background.hide();
            $content.hide();
        });


        if (popup) {
            showFn();
        }
        else {
            this.click(showFn);
        }

        window.showOrderLoader = function() {
            $(document).iceModal('Var god dröj', '<p style="text-align: center; line-height: 120px;">Vänligen vänta medan vi registrerar din beställning.<br/><img src="/redesign-img/order/loader.gif" alt="" />', true, true);
            setTimeout(function() { window.closeOrderLoader(); }, 4000);
        }

        window.closeOrderLoader = function() {
            $background.hide();
            $content.hide();
            isBlocked = false;
        }

    }

});

