#* ------------------------------------------------------------------
#
# The CHEF Velocity Macro library
#
# To be included in each webapp's velocity file area.
#
*# ------------------------------------------------------------------

#* ------------------------------------------------------------------
# The standard way to escape text to make it safe for embedding in html
*# ------------------------------------------------------------------
#macro (chef_html $txt)$sakai_Validator.escapeHtml($txt)#end

#* ------------------------------------------------------------------
# The standard toolbar based on the $sakai_menu definition
# Note: url type items don't support form field sending, override other settings, and go to a new window
# CSS requirements:  navIntraTool inlineForm searchField
#                   portletToolBarDisabled chefToolBarSelected chefToolBarDisabled
*# ------------------------------------------------------------------
#macro (chef_toolbar)

	## use the standard menu reference
	#set ($mnu=$sakai_menu)

	## do nothing if no menu or empty menu
	#if (($mnu) && (!$mnu.isEmpty()))

		<div class="navIntraTool">
		#foreach ($item in $mnu.Items)
			## figure the link
			#set ($base=$sakai_ActionURL.reset().setParameter("sakai_action", $item.Action).toString())

			#if ($item.IsEnabled)
				#if(!$item.IsDivider)
					
					## enabled not field not divider (i.e. a button)
					#if (!$item.IsField)

						## non-url support (i.e. action back to tool)
						#if ($item.Url.length() == 0)

							## checked item support
							#if($item.IsChecked)
								<span class="chefToolBarToggle">
									<a href="#" title="$item.Title" onclick="location = '$base'#if($item.Form)+buildQueryString('$item.Form')#end;return false;">
										$item.Title
									</a>
								</span>

							## unchecked item support
							#else
								<a href="#" title="$item.Title" onclick="location = '$base'#if($item.Form)+buildQueryString('$item.Form')#end;return false;">
									$item.Title
								</a>
							#end

						## url support (i.e. to another place, another window)
						#else
							<a href="$item.Url" title="$item.Title" target="_blank">
								$item.Title
							</a>
						#end

					## here's a text field in a form for the toolbar
					#else
					<form name="$item.Form" action="$base" class="inlineForm" method="post">
						<input size="15" value="$item.Value" name="$item.Title" id="$item.Title" type="text" class="searchField" />
					</form>
					#end

				## here's a menu divider
				#else
					<span class="portletToolBarDisabled">&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp;</span>
				#end
			
			## not enabled - could be checked
			#elseif ($item.IsChecked)
				<span class="chefToolBarSelected">$item.Title</span>

			## not enabled - not checked - show if we are showing disabled
			#elseif ($mnu.Showdisabled)
				<span class="chefToolBarDisabled">$item.Title</span>
			#end

		## end of foreach loop
		#end
		</div>

	## end if "if there's a menu" if
	#end
#end


#* ------------------------------------------------------------------
# The standard way to deal with the ToolPortlet Alert
*# ------------------------------------------------------------------
#macro (chef_alert)
	#if(!$sakai_alert.isEmpty())<div class="alertMessage">#chef_html($sakai_alert.Alert)</div>#end
#end

#* ------------------------------------------------------------------
# The standard way to deal with the message at the top of a tool
*# ------------------------------------------------------------------
#macro (chef_viewmsg $msg)
	<div class="chefPageviewTitle">#chef_html($msg)</div>
#end

#* ------------------------------------------------------------------
# The standard way to deal with the message in place of a flat list
*# ------------------------------------------------------------------
#macro (chef_listmsg $msg)
	<div class="chefListMessage">#chef_html($msg)</div>
#end

#* ------------------------------------------------------------------
# The standard way to start a vm file - deal with fragment or not & css
*# ------------------------------------------------------------------
#macro (chef_start)
#if ($sakai_fragment=='false')
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <meta http-equiv="Content-Style-Type" content="text/css" />
    <title>$!{toolTitle}</title>
    $!sakai_head	
    
    	## setup wysiwyg editor
	#set($editor_path = "vm/editor/$!sakai_editor/sakai_header.vm") 
	<!-- $editor_path -->
	#parse("$editor_path")
    
#if ($frameRefresh)
	#foreach ($id in $frameRefresh)
	<script type="text/javascript" language="JavaScript">
	try
	{
		if (parent.${id}.location.toString().length > 1)
		{
			parent.${id}.location.replace(parent.${id}.location);
		}
	}
	catch (e1)
	{
		try
		{
			if (parent.parent.${id}.location.toString().length > 1)
			{
				parent.parent.${id}.location.replace(parent.parent.${id}.location);
			}
		}
		catch (e2)
		{
		}
	}
	</script>
	#end
#end
#if ($topRefresh)
	<script type="text/javascript" language="JavaScript">
		parent.location.replace(parent.location);
	</script>
#end
#if ($focusChange)
	<script type="text/javascript" language="JavaScript">
	try
	{
		parent.setFocus(${focusChange});
	}
	catch (e1)
	{
		alert(e1);
	}
	</script>
#end
  </head>
  <body #if ($sakai_onload) onload="${sakai_onload}" #end >
#end
## no comment
#end

#* ------------------------------------------------------------------
# The standard way to end a vm file - deal with fragment or not
*# ------------------------------------------------------------------
#macro (chef_end)
#if ($sakai_fragment=='false')</body></html>#end
#end

#* ------------------------------------------------------------------
# The standard way to start a portlet body - end with portletContainerEnd
*# ------------------------------------------------------------------
#macro (chef_portletContainer)
<div class="chefPortletContainer">
#end

#* ------------------------------------------------------------------
# The standard way to end a portlet body - started with portletContainer
*# ------------------------------------------------------------------
#macro (chef_portletContainerEnd)
</div>
#end

#* ------------------------------------------------------------------
# The standard way to start a portlet content - end with portletContentEnd
*# ------------------------------------------------------------------
#macro (chef_portletContent)
	<div class = "chefPortletContent">
#end

#* ------------------------------------------------------------------
# The standard way to end a portlet content - started with portletContent
*# ------------------------------------------------------------------
#macro (chef_portletContentEnd)
	</div>
#end

#* ------------------------------------------------------------------
# The standard way to start a flat list view table - end with portletFlatListEnd
*# ------------------------------------------------------------------
#macro (chef_portletFlatList $summary)
		<table class ="chefFlatListViewTable" cellspacing="0" summary ="$summary">
#end

#* ------------------------------------------------------------------
# The standard way to end a flat list view table - started with portletFlatList
*# ------------------------------------------------------------------
#macro (chef_portletFlatListEnd)
		</table>
#end


#* ------------------------------------------------------------------
# The standard way to setup a date selection widget
#
# IMPORTANT: ResourceBundle with months referenced by $tlang.getString("jan")
#            through $tlang.getString("dec") _must_ be defined
#
# Creates a drop-down date selection, as well as a pop-up date selection
# $yearselect_id  The name and id attribute of the year selection dropdown list (html SELECT tag)
# $monthselect_id The name and id attribute of the month selection dropdown list (html SELECT tag)
# $dayselect_id   The name and id attribute of the day selection dropdown list (html SELECT tag)
# $earliestYear   The earliest year to display in the year dropdown (2002 for example)
# $latestYear     The latest year to display in the year dropdown (2005 for examlple)
# $yearSelected   The year selected
# $monthSelected  The month selected
# $daySelected    The day selected
*# ------------------------------------------------------------------
#macro (chef_dateselectionwidget $yearselect_id $monthselect_id $dayselect_id $earliestYear $latestYear $yearSelected $monthSelected $daySelected)

	#set($yearList = [$earliestYear .. $latestYear])
	
    <select name="$monthselect_id" id="$monthselect_id" onchange="blur()">  
      #if ($monthSelected == 1) <option value="1" selected="selected">$tlang.getString("jan")</option> #else <option value="1">$tlang.getString("jan")</option> #end
        
		#if ($monthSelected == 2) <option value="2" selected="selected">$tlang.getString("feb")</option> #else <option value="2">$tlang.getString("feb")</option> #end
        
		#if ($monthSelected == 3) <option value="3" selected="selected">$tlang.getString("mar")</option> #else <option value="3">$tlang.getString("mar")</option> #end
        
		#if ($monthSelected == 4) <option value="4" selected="selected">$tlang.getString("apr")</option> #else <option value="4">$tlang.getString("apr")</option> #end
        
		#if ($monthSelected == 5) <option value="5" selected="selected">$tlang.getString("may")</option> #else <option value="5">$tlang.getString("may")</option> #end
        
		#if ($monthSelected == 6) <option value="6" selected="selected">$tlang.getString("jun")</option> #else <option value="6">$tlang.getString("jun")</option> #end
        
		#if ($monthSelected == 7) <option value="7" selected="selected">$tlang.getString("jul")</option> #else <option value="7">$tlang.getString("jul")</option> #end
        
		#if ($monthSelected == 8) <option value="8" selected="selected">$tlang.getString("aug")</option> #else <option value="8">$tlang.getString("aug")</option> #end
        
		#if ($monthSelected == 9) <option value="9" selected="selected">$tlang.getString("sep")</option> #else <option value="9">$tlang.getString("sep")</option> #end
        
		#if ($monthSelected == 10) <option value="10" selected="selected">$tlang.getString("oct")</option> #else <option value="10">$tlang.getString("oct")</option> #end
        
		#if ($monthSelected == 11) <option value="11" selected="selected">$tlang.getString("nov")</option> #else <option value="11">$tlang.getString("nov")</option> #end
        
		#if ($monthSelected == 12) <option value="12" selected="selected">$tlang.getString("dec")</option> #else <option value="12">$tlang.getString("dec")</option> #end

    </select>
    
    <select name="$dayselect_id" id="$dayselect_id" onchange="blur()">
		
    #foreach($d in [1..31])
        #if($d == $daySelected)
            <option value="$d" selected="selected">$d</option>
        #else
            <option value="$d">$d</option>
        #end
	#end
	
    </select>
    
    <select name="$yearselect_id" id="$yearselect_id" onchange="blur()">
		
        #foreach ($y in $yearList)
            #if ($y == $yearSelected)
                    <option value="$y" selected="selected">$y</option>
            #else
                    <option value="$y">$y</option>
            #end
		#end
		
    </select>
	
	#chef_dateselectionwidgetpopup($yearselect_id $monthselect_id $dayselect_id)
	
#end


#* ---------------------------------------------------------------------------------------
# The standard way to instantiate a popup calendar widget that allows the user to select a date
# $yearselect_id  The id attribute of the year selection dropdown list (html SELECT tag)
# $monthselect_id The id attribute of the month selection dropdown list (html SELECT tag)
# $dayselect_id   The id attribute of the day selection dropdown list (html SELECT tag)
# ----------------------------------------------------------------------------------------
*#
#macro (chef_dateselectionwidgetpopup $yearselect_id $monthselect_id $dayselect_id)
	
	## Everything is in an our own external JavaScript library
	## <script type="text/javascript" src="#libraryLink('jscalendar/sakai-jscalendar.js')"></script>
	## Call a JavaScript function from within the library to do setup
	## <script type="text/javascript" defer="1">chef_dateselectionwidgetpopup('$yearselect_id', '$monthselect_id', '$dayselect_id');</script>
	
	## Everything is in an our own external JavaScript library
	<script type="text/javascript" src="#libraryLink('calendar/sakai-calendar.js')"></script>
	## Call a JavaScript function from within the library to do setup
	<script type="text/javascript">chef_dateselectionwidgetpopup('$yearselect_id', '$monthselect_id', '$dayselect_id');</script>
#end


#* ------------------------------------------------------------------
# The standard way to instantiate a WYSIWYG formatted text editing widget
# $textarea_id - The HTML id of the textarea form element already in the HTML document
*# ------------------------------------------------------------------
#macro (chef_setupformattedtextarea $textarea_id)

	## setup wysiwyg editor
	#set($editor_path = "vm/editor/$!sakai_editor/sakai_body.vm") 
	<!-- $editor_path -->
	#parse("$editor_path")

	<script type="text/javascript" defer="1">chef_setupformattedtextarea('$textarea_id');</script>
	
#end

#* ------------------------------------------------------------------
# The standard way to instantiate a WYSIWYG full-document editing widget
# $textarea_id - The HTML id of the textarea form element already in the HTML document
*# ------------------------------------------------------------------
#macro (chef_setuphtmldocumenttextarea $textarea_id)

	## setup wysiwyg editor
	#set($editor_path = "vm/editor/$!sakai_editor/sakai_body.vm") 
	<!-- $editor_path -->
	#parse("$editor_path")

	<script type="text/javascript" defer="1">chef_setuphtmldocumenttextarea('$textarea_id');</script>
	
#end

#macro (sakai_courier)
#if ($courier)
<script type="text/javascript" language="JavaScript">
updateTime = ${courierTimeout}000;
updateUrl = "$courier";
scheduleUpdate();
</script>
#end
#end

#* ------------------------------------------------------------------
#
# For CHEF 1.x support
#
*# ------------------------------------------------------------------

#macro (toolbar $toolbar_mnu)
#set($sakai_menu=$toolbar_mnu)
#chef_toolbar()
#end

#macro (toolLink $toolLinkJunk $toolLinkAction)$sakai_ActionURL.setParameter("sakai_action","$toolLinkAction")#end

#macro (toolForm $toolFormJunk)$sakai_ActionURL.setAction()#end

#macro (imageLink $imageLinkName)$sakai_ActionURL.setResourcePath("${sakai_image_path}${imageLinkName}")#end

#macro (scriptLink $scriptName)$sakai_ActionURL.setResourcePath("${sakai_script_path}${scriptName}")#end

#macro (libraryLink $path)${sakai_library_path}${path}#end

#macro (scheduleLink $scheduleLinkName)/library/schedule/$scheduleLinkName#end

#macro (chatlogLink $chatlogLinkPanel)$sakai_ActionURL.setParameter("$param_panel","$chatlogLinkPanel")#end

#macro (contentLink $contentLinkPanel)$sakai_ActionURL.setParameter("$param_panel","$contentLinkPanel")#end

#macro (contentCourierLink $contentCourierLinkPanel)$sakai_ActionURL.setParameter("$param_panel","$contentCourierLinkPanel")#end

#macro (toolLinkParam $toolLinkJunk $toolLinkAction $toolLinkQuery)$sakai_ActionURL.setParameter("sakai_action","$toolLinkAction").setQueryString("$toolLinkQuery")#end


#* ------------------------------------------------------------------
#
# A standard way to display the legend in Calendar tool
#
*# ------------------------------------------------------------------
#macro (eventLegend $tlang)

<table style="margin-right:auto;margin-left:auto;margin-top:1em;width:80%" class="listHier" cellpadding="3" summary="$tlang.getString('gen.eventtype.list.summary')">
<tr><th colspan="3" style="text-align:center">$tlang.getString('view.legend')</th></tr>
<tr>
<td><img src = "#imageLink("sakai/academic_calendar.gif")" border="0" alt="$tlang.getString('del.acad')" /> $tlang.getString('del.acad')</td>
<td><img src = "#imageLink("sakai/activity.gif")" border="0" alt="$tlang.getString('del.activ')" /> $tlang.getString('del.activ')</td>
<td><img src = "#imageLink("sakai/cancelled.gif")" border="0" alt="$tlang.getString('del.cancell')" /> $tlang.getString('del.cancell')</td>
</tr>
<tr>
<td><img src = "#imageLink("sakai/class_dis.gif")" border="0" alt="$tlang.getString('del.class') - $tlang.getString('del.disc')" /> $tlang.getString('del.class') - $tlang.getString('del.disc')</td>
<td><img src = "#imageLink("sakai/class_lab.gif")" border="0" alt="$tlang.getString('del.class') - $tlang.getString('del.lab')" /> $tlang.getString('del.class') - $tlang.getString('del.lab')</td>
<td><img src = "#imageLink("sakai/class_lec.gif")" border="0" alt="$tlang.getString('del.class') - $tlang.getString('del.lect')" /> $tlang.getString('del.class') - $tlang.getString('del.lect')</td>
</tr>
<tr>
<td><img src = "#imageLink("sakai/class_sma.gif")" border="0" alt="$tlang.getString('del.class') - $tlang.getString('del.small')" /> $tlang.getString('del.class') - $tlang.getString('del.small')</td>
<td><img src = "#imageLink("sakai/class_session.gif")" border="0" alt="$tlang.getString('del.classsession')" /> $tlang.getString('del.classsession')</td>
<td><img src = "#imageLink("sakai/computersession.gif")" border="0" alt="$tlang.getString('del.computer')" /> $tlang.getString('del.computer')</td>
</tr>
<tr>
<td><img src = "#imageLink("sakai/deadline.gif")" border="0" alt="$tlang.getString('del.dead')" /> $tlang.getString('del.dead')</td>
<td><img src = "#imageLink("sakai/exam.gif")" border="0" alt="$tlang.getString('del.exam')" /> $tlang.getString('del.exam')</td>
<td><img src = "#imageLink("sakai/meeting.gif")" border="0" alt="$tlang.getString('del.meet')" /> $tlang.getString('del.meet')</td>
</tr>
<tr>
<td><img src = "#imageLink("sakai/multi-conference.gif")" border="0" alt="$tlang.getString('del.multi')" /> $tlang.getString('del.multi')</td>
<td><img src = "#imageLink("sakai/quiz.gif")" border="0" alt="$tlang.getString('del.quiz')" /> $tlang.getString('del.quiz')</td>
<td><img src = "#imageLink("sakai/special_event.gif")" border="0" alt="$tlang.getString('del.special')" /> $tlang.getString('del.special')</td>
</tr>
<tr>
<td><img src = "#imageLink("sakai/webassignment.gif")" border="0" alt="$tlang.getString('del.web')" /> $tlang.getString('del.web')</td>        
</tr>
</table>

#end
