// NOTE: The comments in this file are not stripped by the page constructor, because this file is loaded directly
//       by the browser via <script> tags. TODO: Perhaps change this to be content? Or at least go through a dispatcher
//       which could remove the comments. -->

/// <name>sytadelPageReturnUrl</name>
/// <summary>
/// Global variable containing the URL of the page to return to once a pop-up has completed.
/// </summary>
var sytadelPageReturnUrl = '';

/// <name>popAddItem</name>
/// <summary>
/// Opens a pop-up window for adding new content. The pop-up window controls updating the parent (this) window once it has completed.
/// </summary>
/// <arg name="url">The url of the add page.</arg>
/// <arg name="returnUrl">The URL to refresh this page with once the item has been added, or 'here' to refresh this page.</arg>
function popAddItem(url,returnUrl) {
    sytadelPageReturnUrl = returnUrl;
    var wf = new windowFrame(700,700);
    window.open(url,"_blank","screenX="+wf.left+",screenY="+wf.top+",left="+wf.left+",top="+wf.top+",toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,copyhistory=no,width="+wf.width+",height="+wf.height);
}

/// <name>popEditItem</name>
/// <summary>
/// Opens a pop-up window for editing content. The pop-up window controls updating the parent (this) window once it has completed.
/// </summary>
/// <arg name="itemId">The itemId of the item which will be edited.</arg>
/// <arg name="returnUrl">The URL to refresh this page with once the edited item has been saved, '.' to refresh this page,
/// or '' to do nothing.</arg>
function popEditItem(itemId) {
    sytadelPageReturnUrl = '.';
    var wf = new windowFrame(700,700);
    window.open("/content/maintain/edit/index.phtml?itemId="+itemId,"_blank","screenX="+wf.left+",screenY="+wf.top+",left="+wf.left+",top="+wf.top+",toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,copyhistory=no,width="+wf.width+",height="+wf.height);
}

/// <name>popEditVersion</name>
/// <summary>
/// Opens a pop-up window for editing content. The pop-up window controls updating the parent (this) window once it has completed.
/// </summary>
/// <arg name="id">The versionId of the item which will be edited.</arg>
/// <arg name="returnUrl">The URL to refresh this page with once the edited item has been saved, '.' to refresh this page,
/// or '' to do nothing.</arg>
function popEditVersion(versionId) {
    sytadelPageReturnUrl = '.';
    var wf = new windowFrame(700,700);
    window.open("/content/maintain/edit/index.phtml?versionId="+versionId,"_blank","screenX="+wf.left+",screenY="+wf.top+",left="+wf.left+",top="+wf.top+",location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,copyhistory=no,width="+wf.width+",height="+wf.height);
}

/// <name>popRawDataEditItem</name>
/// <summary>
/// Opens a pop-up window for doing raw data editing. The pop-up window controls updating the parent (this) window once it has completed.
/// </summary>
/// <arg name="id">The itemId of the item which will be edited.</arg>
/// <arg name="returnUrl">The URL to refresh this page with once the edited item has been saved, '.' to refresh this page,
/// or '' to do nothing.</arg>
function popRawDataEditItem(itemId) {
    sytadelPageReturnUrl = '.';
    var wf = new windowFrame(600,600);
    window.open("/content/maintain/rawDataEdit/index.phtml?itemId="+itemId,"_blank","screenX="+wf.left+",screenY="+wf.top+",left="+wf.left+",top="+wf.top+",toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,copyhistory=no,width="+wf.width+",height="+wf.height);
}

/// <name>popRawDataEditVersion</name>
/// <summary>
/// Opens a pop-up window for doing raw data editing. The pop-up window controls updating the parent (this) window once it has completed.
/// </summary>
/// <arg name="id">The versionId of the item which will be edited.</arg>
/// <arg name="returnUrl">The URL to refresh this page with once the edited item has been saved, '.' to refresh this page,
/// or '' to do nothing.</arg>
function popRawDataEditVersion(versionId) {
    sytadelPageReturnUrl = '.'; 
    var wf = new windowFrame(600,600);
    window.open("/content/maintain/rawDataEdit/index.phtml?versionId="+versionId,"_blank","screenY="+wf.top+",left="+wf.left+",top="+wf.top+",toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,copyhistory=no,width="+wf.width+",height="+wf.height);
}

// miscellaneous
function popCloneVersion (informationSpaceItemId,cloneVersionId) {
    popAddItem('/content/maintain/create/index.phtml?informationSpaceItemId=' + informationSpaceItemId + '&cloneVersionId=' + cloneVersionId,'');
}

function windowFrame(width,height) {
    widthPct = 0.9;
    heightPct = 0.9;
    heightAdjust = -30;
    if (isAtLeastIE4 || isMozilla || isFirefox) {
//        this.width = window.innerWidth * widthPct;
//        this.height = window.innerHeight * heightPct;
//        this.left = (window.innerWidth - this.width) / 2;
//        this.top = (window.innerHeight - this.height) / 2;
//        alert(this.width+','+this.height+','+this.left+','+this.top);

        this.width = window.screen.width * widthPct;
        this.height = window.screen.height * heightPct;
        this.left = (window.screen.width - this.width) / 2;
        this.top = (window.screen.height - this.height) / 2;
        // adjustment for IE's broken height calculations
        this.height += heightAdjust;
    } else {
        this.width = width;
        this.height = height;
        this.left = 100;
        this.top = 100;
    }
}

/// <name>popWindow</name>
/// <summary>
/// Opens a generic pop-up window.
/// </summary>
/// <arg name="url">The URL to load into the window.</arg>
/// <arg name="returnUrl">The URL to return back to.</arg>
function popWindow(url,returnUrl) {
    sytadelPageReturnUrl = returnUrl;
    var wf = new windowFrame(600,600);
    window.open(url,"_blank","screenY="+wf.top+",left="+wf.left+",top="+wf.top+",location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,copyhistory=no,width="+wf.width+",height="+wf.height);
}

/// <name>popWindowWithSize</name>
/// <summary>
/// Opens a generic pop-up window to a given size.
/// </summary>
/// <arg name="url">The URL to load into the window.</arg>
/// <arg name="returnUrl">The URL to return back to.</arg>
/// <arg name="width">The width of the pop-up window.</arg>
/// <arg name="height">The height of the pop-up window.</arg>
function popWindowWithSize(url,returnUrl,width,height) {
    sytadelPageReturnUrl = returnUrl;
    var wf = new windowFrame(width,height);
    window.open(url,"_blank","screenY="+wf.top+",left="+wf.left+",top="+wf.top+",location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,copyhistory=no,width="+width+",height="+height);
    return false;
}

/// <name>popHelp</name>
/// <summary>
/// Opens a help pop-up window.
/// </summary>
/// <arg name="itemId">The itemId of the help item to be displayed.</arg>
/// <arg name="fromItemId"></arg>
/// <arg name="returnUrl"></arg>
function popHelp(itemId,fromItemId,returnUrl) {
    sytadelPageReturnUrl = returnUrl;
    window.open("/ui/helpWindow.phtml?itemId="+itemId+"&fromItemId="+fromItemId,"_blank","screenX=100,screenY=100,toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,copyhistory=no,width=500,height=500");
}

/// <name>popRefresh</name>
/// <summary>
/// Called by the pop-up window, this function refreshes the page from the URL specified when the pop-up window was created.
/// It is only called if the pop-window was successful.
/// </summary>
/// <arg name="newVersionId">The versionId of the new item. (Not currently used, but may well be if we move to modals)</arg>
// Reloads the page from sytadelPageRefreshUrl
function popRefresh(newVersionId,newItemId) {
    // if this is not the original page, then just return
    if ((typeof(sytadelPageReturnUrl) == "undefined") || (sytadelPageReturnUrl.length == 0)) {
        return;
    }
    // otherwise work out how to refresh and do it
    var refreshUrl = '';
    if (sytadelPageReturnUrl == '.') {
        refreshUrl = window.location.href;
    } else {
        refreshUrl = sytadelPageReturnUrl;
    }
    // if url is /content/index.phtml or /content/item.phtml then we need to add a refreshCache=1
    regexp = new RegExp("/content/(index|item).phtml",'gi');
    if (regexp.test(refreshUrl)) {
        // see if it already has a get
        regexp = new RegExp('refreshCache=1','gi');
        if (!regexp.test(refreshUrl)) {
            regexp = new RegExp("\\x3F",'gi');
            if (regexp.test(refreshUrl)) {
                refreshUrl = refreshUrl+'&refreshCache=1';
            } else {
                refreshUrl = refreshUrl+'?refreshCache=1';
            }
        }
    }
    window.self.location = refreshUrl
}

/// <name>popSetReturnUrl</name>
/// <summary>
/// Sets a new return url for when the parent is refreshed on pop close. Typically called by the pop window on the parent.
/// </summary>
/// <arg name="newUrl>The new URL to set</arg>
function popSetReturnUrl(newUrl) {
    sytadelPageReturnUrl = newUrl;
}

// Prompts the user for a comment with a caller supplied prompt string, and adds it
// as a cgi parameter to the passed in location object's href property.
// Returns true (commented added) or false (no comment provided by user).
function addCommentToUrl (object,promptString) {
    return addModalDialogToUrl(object,"/ui/dialogs/addComment.phtml",{'promptString':promptString});
}

// Adds a modal dialog to an object's url. The return values from the modal dialog get appended
// to the object.href property
// 
// @param object Object to add modal dialog to
// @param dialog The name of the dialog to call (in pages/ui/dialogs/)
// @param params Parameters to pass to the modal dialog
// @return false if there was an error, true otherwise
function addModalDialogToUrl (object,dialog,params) {
    // Show a modal dialog, passing the parameters to allow both javascript and php access in the dialog code
    var r = showModalDialog(dialog+'?'+getUrlParameterStringFromKeyedArray(params),params);
    // the return value should be an associative array or object of what the user selected and/or typed in or false if there was an error
    if (!r) {
        return false;
    }
    // Append the return values to the href of the object
    object.href = object.href + '&' + getUrlParameterStringFromKeyedArray(r);
    return true;
}

// Returns a URL parameter string from a keyed array or object.
//
// Input array/object:
//
// var object = {'apple':'green','orange':'orange'}
//
// Output string:
//
// apple=green&orange=orange&
//
// @param arrayOrObject The target array or object. 
// @return string The URL parameter string.
function getUrlParameterStringFromKeyedArray (arrayOrObject) {
    var string = '';
    for (key in arrayOrObject) {
        string = string + key + '=' + arrayOrObject[key] + '&';
    }
    return string;
}

// Generates a fixed 400 x 180 pixel conformatiom message dialog, prompting
// for OK or Cancel.
//
// Returns a boolean indicating whether OK (true) or Cancel (false)
// was clicked on.
function promptForConfirmation (message) {
    return promptForConfirmationWithSize(message,400,180);
}

// Generates a variable size message dialog, prompting for OK or Cancel.
//
// Returns a boolean indicating whether OK (true) or Cancel (false)
// was clicked on.
function promptForConfirmationWithSize (message, width, height) {
    var confirmation = showModalDialog("/ui/dialogs/confirm.phtml",message,"dialogWidth: "+width+"px; dialogHeight: "+height+"px; resizable: no; status: no;");
    return confirmation;
}
