/*
    This file contains javascript methods used by Connect Daily for 
    Miscellaneous Routines for Popping open Windows.

    Connect Daily Web Calendar Software

    Copyright 2008, MH Software, Inc. All Rights Reserved
    1006 W 104th Ave #500, Northglenn CO 80234-3802
    +1 303 438 9585

    Last Edit By: "$Author: gsexton $"
    Last Checkin: "$Date: 2011/04/28 20:42:22 $"
    Revision #  : "$Revision: 1.7 $"
*/

function getPopLeft(iPrefWidth) {
    var iResult=50;
    if (screen.availWidth)
    {
        // The availLeft attempts to handle multi-monitor systems cleanly.
        iResult=(screen.availWidth-iPrefWidth)/2+(screen.availLeft==undefined ? 0 : screen.availLeft );
    }
    return iResult;
}
function getPopTop(iPrefHeight){
    var iResult=50;
    if (screen.availHeight)
    {
        iResult=(screen.availHeight-iPrefHeight)/2;
    }
    return iResult;
}

function popReminder(iItemID,iIntegral) {
    window.open("EditReminder.html?cal_item_id="+iItemID+"&integral="+iIntegral,
                "EditReminder_"+(new Date().getTime()),"scrollbars=yes,status=no,location=no,menubar=no,toolbar=no,width=800,height=400,left="+getPopLeft(800)+",top="+getPopTop(500));
}

/**
* This function pops the view item window. All arguments except for iItemID are optional.
* <BR><BR>
* An attempt will be made to center the window within the display. It works pretty well 
* for single monitor systems, and for dual monitors it works well with Firefox. IE7 is
* sort of broken. Oh well.
*
* @param iItemID the cal_item_id of the event to view.
* @param iSpecificDate The specific instance date of the event to view. 
* Default, -1 which means no specific date.
* @param bIntegral if True, display in integral mode, which gives additional info. Defaut behavior is false.
* @param iDisplayGenerated This is the system time as MilliSeconds since epoch that
* the underlying display was generated. This value can be passed to ViewItem so that
* ViewItem can detect if the underlying event has been changed since the display
* was generated. If this parameter is not defined, and a global variable of
* check_after is defined, that value is used. Default value is undefined.
* @param cStyleSheet Style Sheet that should be used to override the default. If this
* argument is not defined, and a global variable style_sheet is defined, that is used.
*/
function popItem(iItemID, iSpecificDate,bIntegral,iDisplayGenerated,cStyleSheet) {
    if (cStyleSheet==undefined)
    {
        if (!(typeof style_sheet=="undefined"))
        {
            cStyleSheet=style_sheet;
        }
    }
    if (iDisplayGenerated==undefined)
    {
        if (!(typeof checkafter=="undefined"))
        {
            iDisplayGenerated=checkafter;
        }
    }
    if (bIntegral==undefined)
    {
        if (!(typeof integral=="undefined"))
        {
            bIntegral=integral;
        }
    }
    var sDisplay="ViewItem.html?integral="+(bIntegral==undefined ? "0" : (bIntegral ? "1&detail=1" : "0"))+
                "&cal_item_id="+iItemID+
                (((iSpecificDate==undefined) || (iSpecificDate<0)) ? '' : '&dtwhen=' + iSpecificDate)+
                (cStyleSheet==undefined ? '' : '&style_sheet='+cStyleSheet)+
                (iDisplayGenerated==undefined ? '' : '&checkafter=' + iDisplayGenerated);
	
    if (displayDialogInline()) {
		myDialog = new dijit.Dialog({
			title: "",
			content: "<IFRAME ID='DIJIT_SHOWEVENT' FRAMEBORDER=NO SCROLLING=AUTO STYLE='width: 598px; height: 490px;' SRC='"+sDisplay+"'></IFRAME>",
			style: "overflow: hidden; width: 600px; height: 500px;"});
		myDialog.show();
	} else {
	    window.open(sDisplay,
                "ViewItemWin_"+(new Date().getTime()),
                "scrollbars=yes,resizable=yes,status=no,location=no,menubar=no,toolbar=no,width=670,height=500,left="+getPopLeft(620)+",top="+getPopTop(500));
	}
}
function isIE() {
    	return navigator.userAgent.toLowerCase().indexOf("msie")>0;
}
/*
	We're having some problems with the dijit.Dialog. If the container windows is
	too small, or if we're running in an IFRAME, things get ultra wierd. Use this
	function to see if it's safe.
*/
function displayDialogInline() {
	var iWidth=(isIE() ? document.body.clientWidth : window.innerWidth);
	var bLargeEnough=(iWidth>=800);
	var bFrame=(window.self!=window.top);
	// IPads are giving us a problem. You can't close the dijit.Dialog. Use this as a work-around
	var bIPad=(navigator.userAgent.indexOf('iPad')>0);
	//	alert('iWidth='+iWidth+' bLargeEnough='+bLargeEnough+' bFrame='+bFrame+' bIPad='+bIPad);
	return (bLargeEnough && !(bFrame || bIPad));
}

function printCalendar() {
	if (isIE()) {
		// This seems to work better in IE when using frames.
		document.execCommand('print', false, null);
	} else {
		window.self.focus();
		window.print();
	}
}

var pWidth,pHeight,pZ;
function widenDiv(divID){
    var myDiv=document.getElementById(divID);
    if (myDiv)
    {
        with (myDiv.style)
        {
            pWidth=width;
            pHeight=height;
            pZ=zIndex;
            zIndex=99;
            width="auto";
            height="auto";
        }
    }
}
function shrinkDiv(divID, iOriginalWidth,iOriginalHeight){
    var myDiv=document.getElementById(divID);
    if (myDiv)
    {
        with (myDiv.style)
        {
            zIndex=pZ;
            width=pWidth;
            height=pHeight;
        }
    }
}
function sizeWindow(){
    var myDiv=document.getElementById("ContentDiv");
    var winWidth=document.body.scrollWidth;
    var winHeight=document.body.scrollHeight;
    var newHeight;

    if (isIE())
    {
        newHeight=winHeight-document.documentElement.clientHeight+30;
    } else
    {
        newHeight=winHeight-window.innerHeight+30;
    }
    if (screen.availHeight)
    {
        if (newHeight>screen.availHeight)
        {
            newHeight=screen.availHeight;
        }
    }
    // center the window
    if (window.moveTo)
        window.moveTo((screen.availWidth-winWidth)/2,(screen.availHeight-winHeight)/2);

    window.resizeBy(0,newHeight);

}
function sizeWindowOld() {
    
    var winHeight,winWidth,clWidth,clHeight;

    if (isIE())
    {
        clWidth=document.body.clientWidth;
        clHeight=document.body.clientHeight;
        alert(document.body.scrollHeight+" clientHeight="+document.body.scrollHeight);
    } else
    {
        clHeight=window.innerHeight;
        clWidth=window.innerWidth;
        clHeight=document.body.scrollHeight;
        clWidth=document.body.scrollWidth;

    }
    if (screen.availHeight)
    {
    }

    // add 30 to the width for a border
    // add 60 to the height to account for a border + the titlebar
    //  alert("WinWidth="+winWidth+" WinHeight="+winHeight);
    window.resizeTo(clWidth+30,clHeight+40);
    
    //center the window
    if (window.moveTo)
        window.moveTo((screen.availWidth-winWidth)/2,(screen.availHeight-winHeight)/2);
}

function popObjectLogEntries(iObjectType,iObjectID) {
    window.open("ViewLogEntriesForObject.html?object_type_id="+iObjectType+"&object_id="+iObjectID,
                "ViewLEObj_"+(new Date().getTime()),"scrollbars=yes,status=no,location=no,menubar=no,toolbar=no,width=800,height=400,left="+getPopLeft(800)+",top="+getPopTop(500));
}


/**
 * This function toggles the visibility of a div in response to 
 * a click. 
 *  
 * @param divID the ID of the Div to hide or display. 
 * @param imgID The ID of the image for the up/down arrow. 
 * @param imgDownName The File name of the down image. 
 * @param imgUpName The file name of the up image.
 */
function toggleDiv(divID,imgID,
                   imgDownName,
                   imgDownNameHot,
                   imgUpName,
                   imgUpNameHot) {
    var myDiv=document.getElementById(divID);
    if (myDiv!=null)
    {
        var myImg=document.getElementById(imgID);
        if (myDiv.style.display=='none')
        {
            myDiv.style.display='block';
            myImg.src=imgUpName;
            myImg.onmouseout=function() {
                this.src=imgUpName;
            }
            myImg.onmouseover=function(){
                this.src=imgUpNameHot;
            }
            
        } else
        {
            myDiv.style.display='none';
            myImg.src=imgDownName;
            myImg.onmouseout=function() {
                this.src=imgDownName;
            }
            myImg.onmouseover=function(){
                this.src=imgDownNameHot;
            }
        }
    }
}


function showEvent(cal_item_id,julian_date) {
myDialog = new dijit.Dialog({
	title: sTitle,
	content: "<IFRAME ID='DIJIT_SHOWEVENT' FRAMEBORDER=NO SCROLLING=AUTO STYLE='width: 600px; height: 490px;' SRC='ViewItem.html?cal_item_id="+cal_item_id+"&dtwhen="+julian_date+"&checkafter="+checkafter+"&integral="+(integral?"1&detail=1":"0")+"'></IFRAME>",

	style: "overflow: hidden; width: 600px; height: 500px;"});
    myDialog.show();
}


