﻿var ll = null;
var Dialog = { buttonTemplate: null };

var prevScroll = 0;
function hideScrollbar() {
    prevScroll = $viewport().y;
    document.body.style.overflow = 'hidden';
    document.getElementsByTagName("html")[0].style.overflow = 'hidden';
    //window.scrollTo(0, top);
}
function showScrollbar() {
    document.body.style.overflow = '';
    document.getElementsByTagName("html")[0].style.overflow = '';
    window.scrollTo(0, prevScroll);
}

function changeDialogPage(src, args) {
	var dlg = $("dialog");
	var iframe = $("dialog_content");

    dlg.loaded = function() {
    if (!iframe.contentDocument)
            iframe.contentDocument = document.frames["dialog_content"].document;
        $("dialog_title").innerHTML = iframe.contentWindow.loaded(args);
    };
    
    iframe.src = src;
};

function setupDialog() {
    if (!ll) {
        var dlg = $("dialog");

        hideScrollbar();
        ll = Ekina.UI.createLockLayer(50, "black", null, null, true, null);

        dlg.parentNode.removeChild(dlg);
        document.body.appendChild(dlg);

        dlg.css("zIndex", ll.style.zIndex + 1);

        $("dialog_button").onclick = function() {
            closeDialog(null);
        };

        Ekina.Events.register(window, "onresize", function() {
            resizeDialog(0, 0);
        });
    }
    else {
        hideScrollbar();
        ll.show();
    }
};

function setButtons(buttons) {
	var buttonContainer = $("dialog_buttons");
	
	while(buttonContainer.firstChild.childNodes.length > 0)
		buttonContainer.firstChild.removeChild(buttonContainer.firstChild.firstChild);
	while(buttonContainer.lastChild.childNodes.length > 0)
		buttonContainer.lastChild.removeChild(buttonContainer.lastChild.firstChild);
		
	for (var i = 0; i < buttons.length; i++) {
		var button = buttons[i];
		var btn = Dialog.buttonTemplate.cloneNode(false);
		btn.clickhandler = button.handler;
		btn.onclick = function() { if (this.clickhandler) this.clickhandler(); return false; };
//		if (button.bold)
//			btn.addClass("bold");
		if (i > 0)
			btn.style.marginLeft = "5px";
		if (button.width)
			btn.style.width = button.width + "px";
		btn.value = button.text;
		if (button.left)
			buttonContainer.firstChild.appendChild(btn);
		else
			buttonContainer.lastChild.appendChild(btn);
	};
};

function showDialog(src, width, height, title, okHandler, cancelHandler, args) {

    var dlg = $("dialog");

	setupDialog();

    dlg.okHandler = okHandler;
    dlg.cancelHandler = cancelHandler;
    
    $("dialog_button").css("visibility", "visible");
	$("dialog_message").css("display", "none");
	$("dialog_content").css("display", "block");
	
	$("dialog_buttons").show();

    var iframe = $("dialog_content").css({
        width: width + "px",
        height: height + "px"
    });

    dlg.loaded = function() {
        if (!iframe.contentDocument)
            iframe.contentDocument = document.frames["dialog_content"].document;
        $("dialog_title").innerHTML = iframe.contentWindow.loaded(args);
        if (iframe.contentWindow.afterLoaded)
			iframe.contentWindow.afterLoaded();
    };


    resizeDialog(width, height);
    dlg.show();

    $("dialog_title").innerHTML = "Loading...";
    iframe.src = src;    
};

function closeDialog(result) {
    var dlg = $("dialog");

    $("dialog_content").src = "about:blank";
    dlg.style.display = "none";

    if ($null(result)) {
        if (!$null(dlg.cancelHandler))
            dlg.cancelHandler();
    }
    else {
        if (!$null(dlg.okHandler))
            dlg.okHandler(result);
    }
    
    dlg.cancelHandler = null;
    dlg.okHandler = null;
    ll.hide();
    showScrollbar();
};

function resizeDialog(width, height) {

    var v = $viewport();
    //alert(v.y + " - " + document.body.scrollTop);
    var padWidth = Math.floor((v.width / 100) * 10); // 10%
    width = width > 0 ? width : (v.width - (padWidth * 2));
    height = height > 0 ? height : (v.height - (padWidth * 2));
    
    $("dialog").css({
        left: v.x + (v.width / 2 - (width + 20) / 2) + "px",
        top: v.y + (v.height / 2 - (height + 50) / 2) + "px",
        width: width + 20 + "px",
        height: height + 20 + "px"
    });
    $("dialog_content").css({
        width: width + "px",
        height: height + "px"
    });
}

function showMessage(message, title, buttons) {

    var dlg = $("dialog");
    setupDialog();

    $("dialog_button").css("visibility", "hidden");
	$("dialog_buttons").hide();
        
	var container = $("dialog_buttonContainer");
	
	while (container.childNodes.length > 0)
		container.removeChild(container.firstChild);
	
	for (var i = 0; i < buttons.length; i++) {
		var button = $("<input type=\"button\" class=\"ctrl_button\" />");
		button.value = buttons[i].text;
		button.onclick = buttons[i].handler;
		button.style.display = "inline";
		if (buttons[i]["bold"])
			button.css("fontWeight", "bold");
		if (i > 0)
			button.css("margin-left", "5px");
		button.appendTo(container);
	}
	
	$("dialog_message").css("display", "block");
	$("dialog_content").css("display", "none");
	
//	$("dialog_message").css({
//        width: width + "px",
//        height: height + "px"
//    });
    
    $("dialog_title").innerHTML = title;
    $("dialog_messageText").innerHTML = message;
	
    var v = $viewport();
	//sizeDialog($("dialog_contentholder").offsetWidth, $("dialog_contentholder").offsetHeight);
	//positionDialog($("dialog_contentholder").offsetWidth, $("dialog_contentholder").offsetHeight);
	
	dlg.show();
	
	dlg.style.width = $("dialog_message").offsetWidth + "px";
	
	dlg.style.width = "500px";
	dlg.style.height = "auto";
	
    dlg.css({
        left: v.x + (v.width / 2 - dlg.offsetWidth / 2) + "px",
        top: v.y + (v.height / 2 - dlg.offsetHeight / 2) + "px"
    });
}
