/// <reference path="jquery-1.2.6.min.debug.js" />

/*************************************/
/*GERBER Script Lib v2.0.1           */
/*  dep: jQuery 1.2.6 or better      */
/*************************************/
if (typeof Grb == 'undefined') var Grb = {};
if (!Grb.Text) Grb.Text = {};
if (!Grb.UI) Grb.UI = {};

/***************/
/*StringBuilder*/
/***************/
Grb.Text.StringBuilder = function StringBuilder() {
    this.buffer = []; }
Grb.Text.StringBuilder.prototype.toString = function toString() { 
    return this.buffer.join(""); }
Grb.Text.StringBuilder.prototype.append = function append(value) {
    this.buffer.push(value); 
    return this; }
Grb.Text.escapeSingleQuote = function escapeSingleQuote(e) {
    //escape single quotes.  
    return e.replace('\'','\\\''); }


/****************/
/* Gerber Modal */
/****************/
Grb.UI.Modal = function Modal() {}

if (!Grb.UI.Modal.state) Grb.UI.Modal.state = {Title: '&nbsp;', className: '', Width: 400, Height: 400, UserContent: null, UserContentParent: null, Active: false, Resizable: false};
if (!Grb.UI.Modal.gui) {
    var mdl1 = "<table id=\"gerberModalBox\" border=\"0\" class=\"modalBox\" style=\"display: none; position: absolute; top: 50px; left: 50px;\">";
    var mdl2 = "<tr><td class=\"modalHeaderZone\"><div class=\"modalHeader\"><span class=\"modalHeaderText\"></span><span class=\"closerOff\" onmouseover=\"this.className='closerOn';\" onmouseout=\"this.className='closerOff';\" onclick=\"Grb.UI.Modal.hideModal();\">&nbsp;</span></div></td></tr>";
    var mdl3 = "<tr><td><div id=\"modalWorkingPanel\" style=\"display: none;\"></div><div class=\"modalUserPanel\"><!--  User content --></div></td></tr>";
    var mdl4 = "<tr><td><div class=\"modalFooterZone\"><div class=\"modalFooterStatus\">&nbsp;</div><div class=\"modalSizerHandle ui-resizable-se ui-resizable-handle\">&nbsp;</div></div></td></tr></table>";  
    Grb.UI.Modal.gui = mdl1 + mdl2 + mdl3 + mdl4;
}

Grb.UI.Modal.hideModal = function hideModal() {
    Grb.UI.Modal.state.UserContent.style.display = 'none';
    Grb.UI.Modal.state.UserContentParent.appendChild(Grb.UI.Modal.state.UserContent);
    var mdl = $('#gerberModalBox')[0];
    if (mdl) mdl.style.display = 'none';
    Grb.UI.Modal.hideOverlay();
    
    $('#GerberModalWrapper').remove();

    Grb.UI.Modal.state.Active = false;
    
    //remove event handler here so esc and clicks both remove the handler
    $(document).unbind('keypress.grbmodal');

}

Grb.UI.Modal.escHandler = function escHandler(e) {
    if (e.which === 27 || e.keyCode === 27) {
        Grb.UI.Modal.hideModal();
    }
}

Grb.UI.Modal.setFooter = function setFooter(value) {
    var modal = $('#gerberModalBox');
    if (modal) {
        var footer = modal.find('.modalFooterStatus');
        if (footer) footer[0].innerHTML = value; }
}

Grb.UI.Modal.setTitle = function setTitle(value) {
    var modal = $('#gerberModalBox');
    if (modal) {
        var header = modal.find('.modalHeaderText');
        if (header) header[0].innerHTML = value; }
}

Grb.UI.Modal.showProgress = function showProgress() {
    var pnl = $('#modalWorkingPanel')[0];
    if (pnl) pnl.style.display = '';
}

Grb.UI.Modal.hideProgress = function hideProgress() {
    var pnl = $('#modalWorkingPanel')[0];
    if (pnl) pnl.style.display = 'none';
}

Grb.UI.Modal.showOverlay = function showOverlay() {
    var modalOverlay = document.createElement("DIV")
    modalOverlay.id = 'GerberModalOverlay';
    var h1 = $(window).height();
    var h2 = $(document).height();
    if (h2 > h1) {
        modalOverlay.style.height = h2 + 'px';
    }
    else {
        modalOverlay.style.height = h1 + 'px';
    }
    //modalOverlay.style.height = $('body').height() + 'px';
    $('body')[0].appendChild(modalOverlay);
    modalOverlay.className = 'modalOverlay';
    modalOverlay.style.display = 'block';
}

Grb.UI.Modal.hideOverlay = function hideOverlay() {
    //Grb.UI.Modal.state.UserContentParent.removeChild($('#GerberModalOverlay')[0]);
    $('body')[0].removeChild($('#GerberModalOverlay')[0]);
}

Grb.UI.Modal.loadModalGUI = function loadModalGUI(guiMarkup) {

    //create a new element for the html
    var modalContainer = document.createElement("DIV")
    modalContainer.id = 'GerberModalWrapper';
    $(modalContainer).append(guiMarkup);
    //modalContainer.innerHTML = guiMarkup;
    Grb.UI.Modal.state.UserContentParent.appendChild(modalContainer);

    if (Grb.UI.Modal.state.className != '') {
        modalContainer.className = Grb.UI.Modal.state.className;
    }

    var modal = $('#gerberModalBox');
    var userPanel = modal.find('div.modalUserPanel')[0];
    var header = modal.find('span.modalHeaderText')[0];

    header.innerHTML = Grb.UI.Modal.state.Title;

    //expecting this to have been initially hidden....
    Grb.UI.Modal.state.UserContent.style.display = '';

    userPanel.appendChild(Grb.UI.Modal.state.UserContent);
    userPanel.style.width = Grb.UI.Modal.state.Width + 'px';
    userPanel.style.height = Grb.UI.Modal.state.Height + 'px';


    var resizeGripper = modal.find('div.modalSizerHandle');
    //debugger
    if (Grb.UI.Modal.state.Resizable === true) {
        resizeGripper[0].style.display = 'none';
        //resizeGripper[0].style.display = '';
        //modal.find('div.modalUserPanel').resizable({ handles: {'se':resizeGripper} });
        //modal.resizable({ handles: {'se':resizeGripper} });
        //todo
        //userPanel.makeResizable({ handle: resizeGripper, modifiers: { x: 'width', y: 'height' }, limit: { x: [200, 1024], y: [200, 1024]} });
    }
    else {
        resizeGripper[0].style.display = 'none';
    }

    //modal.makeDraggable({ handle: modal.find('div.modalHeader')[0] });
    modal.draggable({ handle: '.modalHeader' });

    modal[0].style.visibility = 'hidden';
    modal[0].style.display = '';
    Grb.UI.Modal.showOverlay();

    //default to centered   
    var l = ($(window).width() - modal.width()) / 2;
    var t = ($(window).height() - modal.height()) / 2;
    if (l < 0) { l = 0; }
    if (t < 0) { t = 0; }

    t = t + $(window).scrollTop();

    modal[0].style.top = t + 'px';
    modal[0].style.left = l + 'px';

    modal[0].style.visibility = '';
}

Grb.UI.Modal.showModal = function showModal(userContentId, options) {

    //guard
    if (Grb.UI.Modal.state.Active === true) { return; }

    if (options.width) { Grb.UI.Modal.state.Width = options.width; }
    else { Grb.UI.Modal.state.Width = 400; }

    if (options.height) { Grb.UI.Modal.state.Height = options.height; }
    else { Grb.UI.Modal.state.Height = 400; }

    if (options.title) { Grb.UI.Modal.state.Title = options.title; }
    else { Grb.UI.Modal.state.Title = '&nbsp;'; }

    if (options.resizable) { Grb.UI.Modal.state.Resizable = options.resizable; }
    else { Grb.UI.Modal.state.Resizable = false; }

    if (options.className) { Grb.UI.Modal.state.className = options.className; }
    else { Grb.UI.Modal.state.className = ''; }

    Grb.UI.Modal.state.Active = true;

    //legacy support
    if (userContentId.substring(0,1) != '#') { userContentId = '#' + userContentId; }

    Grb.UI.Modal.state.UserContent = $(userContentId)[0];
    Grb.UI.Modal.state.UserContentParent = Grb.UI.Modal.state.UserContent.parentNode;

    Grb.UI.Modal.loadModalGUI(Grb.UI.Modal.gui);

    $(document).bind('keypress.grbmodal', (Grb.UI.Modal.escHandler));
} // end of showModal()


Grb.isTrue = function isTrue(val, trueResult, falseResult) {

    if (val == true || val == 'True' || val == 'true' || val == 'TRUE') return trueResult;
    else return falseResult;
}

function moveData(fromId, toId) {
    var from;
    if ($("#" + fromId).attr("value") == undefined) {
        from = $("#" + fromId).attr("innerHTML");
    }
    else {
        from = $("#" + fromId).attr("value");
    }
    if ($("#" + toId).attr("value") == undefined) {
        $("#" + toId).attr("innerHTML", from);
    }
    else {
        $("#" + toId).attr("value", from);
    }
    return true;
} //end of moveData(fromId, toId)

function clearData(elementId) {
    if ($("#" + elementId).attr("value") == undefined) {
        $("#" + elementId).attr("innerHTML", "");
    }
    else {
        $("#" + elementId).attr("value", "");
    }
    return true;
} // end of clearData(elementId)

function setCheckBox(fromId, toChkBx) {
    var from;
    if ($("#" + fromId).attr("value") == undefined) {
        from = $("#" + fromId).attr("innerHTML");
    }
    else {
        from = $("#" + fromId).attr("value");
    }
    if (from == true || from == 'True' || from == 'true' || from == 'TRUE' || from == 'yes' || from == 'Yes' || from == 'YES') {
        $("#" + toChkBx).attr("checked", "checked");
    }
    else {
        $("#" + toChkBx).attr("checked", "");
    }
    return true;
} // end of setCheckBox()

function highlightThis(elementId) {
    $("#" + elementId).effect("highlight", {}, 1000);
    return true;
}

function hideThis(elementId) {
    $("#" + elementId).attr("style", "display:none;");
    return true;
} //function hideThis

function showThis(elementId) {
    $("#" + elementId).attr("style", "display:inline;");
    return true;
} //function showThis



Grb.UI.FireDefaultButton = function WebForm_FireDefaultButton(event, target) {
    if (event.keyCode == 13) {
        var src = event.srcElement || event.target;
        if (!src || (src.tagName.toLowerCase() != "textarea")) {
            var defaultButton;
            defaultButton = $("#" + target);
            if (!defaultButton) {
                return false;
            }
            defaultButton = defaultButton[0];
            if (defaultButton && typeof (defaultButton.click) != "undefined") {
                defaultButton.click();
                event.cancelBubble = true;
                if (event.stopPropagation) event.stopPropagation();
                return false;
            }
        }
    }
    return true;
}
