/**
 * Launches a Help Dialog
 * @param url 
 * @param section 
 * @param width
 * @param height
 * @param modal Is the dialog modal?  true if so, false otherwise.  If null, defaults to false.  The help dialog
 *                      is typically non-modal.
 */
function showHelp( url, section, width, height, modal) {
	
	var theId="helpDialog";
	var theHref = url;
	// Default the width to 700 px
	width = width == null ? '700px' : width;
	// Default the height to 500 px
	height = height == null ? '500px' : height;
	// Determine modality - defaults to false
	modal = modal == null ? false : modal;
	section = typeof(section) == "undefined" ? null : section;
	/*
	 * nullify the section
	*/
	var dialogObj;
	if(dijit.byId(theId) == null) {
		/*
		 * Create the new dialog
		 */
		dialogObj = new dijit.Dialog({
		    id: theId,
		    title: 'StormNow Help',
		    href: theHref,
		    draggable: true,
		    refreshOnShow: true
		});
		dojo.style(theId, 'width', width);
		dojo.style(theId, 'height', height);
		dojo.style(theId, 'overflow', 'auto' );
		dialogObj.show();
		
	} else {
		/*
		 * Dialog already exists, apply the new styling information
		 */
		dialogObj = dijit.byId(theId);
		var dialogHref = dialogObj.href;
		dojo.style(theId, 'width', width);
		dojo.style(theId, 'height', height);
		dojo.style(theId, 'overflow', 'auto' );
		/*
		 * Only reload if HREFs have changed - prevents unnecessary flicker, reloads, waiting, etc...
		 */
		if(dialogHref != theHref || (!dialogObj.open)) {
			dialogObj.href=theHref;	
			if(dialogObj.open) {
				/*
				 * Dialog is already visible, just update the content
				 */
				dialogObj.refresh();
			} else {
				/*
				 * Dialog isn't visible, show it (which'll update the content, too)
				*/
				dialogObj.show();
			}
			/*
			 * If the help page has a subsection to view, scroll to it once the dialog has loaded.
			*/
			if(section) {
				queueScrollToSection(theId,section);
			}
		} else {
			/*
			 * If the help page has a subsection to view, scroll to it now (the 
			*/
			if(section) {
				scrollToSection(theId,section);
			}
		}
	}

	if(!modal) {
		dojo.query(".dijitDialogUnderlay").style("width","0px");
	} else {
	}
	return false;
}

/**
 * Launches the bulletin viewer dialog
 * @param code 
 * @param width
 * @param height
 * @param modal Is the dialog modal?  true if so, false otherwise.  If null, defaults to false.  The help dialog
 *                      is typically non-modal.
 */
function showBulletinHelp(url, code, width, height, modal) {
	var theId=code + "Dialog";
	var theHref = url + "helpfiles/bulletin_previewer.asp?code=" + code;
	showHelp( theHref,null, width, height, modal);
	return false;
}

/**
 * @param id
 * @param section
 */
function queueScrollToSection(id,section){                                                                                                                                      
	var pane=dijit.byId(id);
	var handle=dojo.connect(pane,"onLoad",function(evt){                                          
	if(section!=null){
		scrollToSection(id,section);
	}
	dojo.disconnect(handle);
	});        
}


/**
 * @param id
 * @param section
 */
function scrollToSection(id,section){                                                                                                                                      
	var pane=dijit.byId(id);
	if((pane !=null) && (section!=null)){
		var anchor=dojo.query(section)[0];
		var elem = dojo.byId(section);
		var coords=dojo.coords(elem, true);
		var contentpane=dojo.byId("helpContent");                 
		var newScrollTop =coords.t;
		newScrollTop = newScrollTop < 0 ? 0 : newScrollTop;
		contentpane.scrollTop=newScrollTop;
	}
}