﻿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();
    }

    Dialog.buttonTemplate = $("template_button");
};

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, updateControl, updateControl2) {

    var dlg = $("dialog");

	setupDialog();

    dlg.okHandler = okHandler;
    dlg.cancelHandler = cancelHandler;
    dlg.updateControl = updateControl;
    dlg.updateControl2 = updateControl2;
    
    $("dialog_button").css("visibility", "visible");
	$("dialog_message").css("display", "none");
	$("dialog_content").css("display", "block");
	$("dialog_content").css("visibility", "visible");
	$("dialog_content").css("height", "auto");
	$("dialog_contentholder").css("padding-top", "10px");
	
	$("dialog_buttons").show();

    var iframe = $("dialog_content").css({
        width: width + "px",
        height: height + "px"
    });

    dlg.loaded = function() {
        //alert(iframe.contentDocument);
        if (!iframe.contentDocument)
            iframe.contentDocument = document.frames["dialog_content"].document;

        //alert(iframe.contentDocument);
        //alert(iframe.contentWindow.loaded());

        $("dialog_title").innerHTML = iframe.contentWindow.loaded(title,args);
        if (iframe.contentWindow.afterLoaded)
            iframe.contentWindow.afterLoaded();
    };


    resizeDialog(width, height);
    dlg.show();
    
    $("dialog_title").innerHTML = "Loading...";
    iframe.src = src;    
};

function closeDialog(result, selectedId) {
    var dlg = $("dialog");
    //alert(selectedId);
    $("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(selectedId, dlg.updateControl, dlg.updateControl2);
    }
    
    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"
    });
}

function showInput(prompt, title, okHandler, updateElement, maxLength) {

    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);
//    }

    var btnOk = $("<input type=\"button\" class=\"ctrl_button\" />");
    btnOk.value = " OK ";
    btnOk.onclick = function() { okHandler(updateElement, $("input_value").value); closeDialog(); };
    btnOk.css("margin-right", "10px");
    btnOk.appendTo(container);
    var btnCancel = $("<input type=\"button\" class=\"ctrl_button\" />");
    btnCancel.value = " CANCEL ";
    btnCancel.onclick = function() { okHandler(updateElement); closeDialog(); };
    btnCancel.appendTo(container);


    $("dialog_message").css("display", "block");

    $("dialog_content").css("visibility", "hidden");
    $("dialog_content").css("height", "1px");

    $("dialog_contentholder").css("padding-top", "0");
    
    //alert($("dialog_content").style.display);

    //	$("dialog_message").css({
    //        width: width + "px",
    //        height: height + "px"
    //    });

    $("dialog_title").innerHTML = title;
    $("dialog_messageText").innerHTML = prompt;

    var input = $("input_value");
    
    if (!maxLength || maxLength < 1)
        maxLength = 50;
        
    if (!input) {
        input = $("<input type=\"text\" id=\"input_value\" maxlength=\"" + maxLength + "\" style=\"width:90%;font-size:larger;\" />");
        insertAfter(input, $("dialog_messageText"));
    }
    else
        input.value = "";


    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"
    });
}

function insertAfter(newChild, refChild) { refChild.parentNode.insertBefore(newChild, refChild.nextSibling); }
 
 

