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="tæt"/>').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;
        }

    }

});



$.fn.iceModal_new = function(options) {
    var options = $.extend({
        content: $(this).html(),
        title: $(this).attr("title"),
        alterClass: "small"
    }, options);

    /*
    var modalbox = $('<div id="modal-content2"/>')
    .toggleClass(options.alterClass, true)
    .append($('<div id="modal-title"/>').text(options.title))
    .append($('<div id="modal-innercontent"/>').append(options.content))
    .append($('<div id="modal-bottom"/>'))
    .append($('<a href="#" id="modal-close" title="tæt"/>').html("<img src='/redesign-img/btn/blank.gif' />"));
    */
    var modalbox = $("div#modal-content2")
                    .empty()
                    .toggleClass(options.alterClass, true)
                    .append($('<div id="modal-title"/>').text(options.title))
                    .append($('<div id="modal-innercontent"/>').append(options.content))
                    .append($('<div id="modal-bottom"/>'))
                    .append($('<a href="#" id="modal-close" title="tæt"/>').html("<img src='/redesign-img/btn/blank.gif' />"));

    var modalbackground = $('<div id="modal-background" class="new" />');

    modalbackground.insertAfter(modalbox);
    modalbackground.show();
    modalbox.show();

    // Close button click
    $('#modal-close,#modal-background').live("click", function() {
        modalbox.hide();
        modalbackground.remove();
    });

};

$(function() {
    var htmlRow = '<tr class="clone"><td style="width: 250px;"><label for="Name">Din vens navn:</label><input name="FriendsName" type="text"></td><td>' +
                  '<label for="Name">Din vens e-mail:</label><input name="FriendsEmail" type="text"></td><td><br/><input style="display: none;" ' +
                  'value="" class="delete_row" type="button"><input value="" class="tip_addrow" type="button"></td></tr>';
    $(".tip_addrow").live("click", function() {
        var table = $(this).closest("table.friends_table");
        var table2 = $(this).closest("table.friends_table tbody");
        var rows = table.find("tr");
        if (rows.length >= 3) { return; }
        //rows.last().clone().appendTo(table);
        //htmlRow.appendTo(table);
        table2.append(htmlRow);
        $(table).find(".delete_row").not(":last").show().click(function() {
            $(this).closest("tr").remove();
        });
        $(table).find(".tip_addrow").not(":last").remove();
    });

});
