<?php
/*
 * Smarty plugin
 * -------------------------------------------------------------
 * File:     function.datePopup.php
 * Type:     function
 * Name:     datePopup
 * Purpose:  Umieszcza pole tekstowe z możlwiością wybrania daty.
 * -------------------------------------------------------------
 */

function smarty_function_datePopup($params, &$smarty)
{
	$options = "
						closebutton: \"<img src=\\\"" . URL_STATIC_CONTENT . "/image/x.gif\\\" alt=\\\"Zamknij\\\" />\",
						" . /* closebutton: '<img src=\"" . URL_STATIC_CONTENT . "/image/Strona/x.gif\" alt=\"Zamknij\" \/> */ "
						url_static_content: '" . URL_STATIC_CONTENT . "'";

	if (isset($params['year']))
	{
		$options .= ",
						yearnext: '&gt;',
						yearprev: '&lt;',
						nextbutton: '&raquo;',
						prevbutton: '&laquo;'";
	}

	$ret = '
				<div id="' . $params['div_id'] . '" class="kalendarzyk" style="display:block;"></div>
				<script type="text/javascript" language="javascript">
				var calendar = false;
				var cal = $(\'' . $params['div_id'] . '\');

				var toogle_calendar = function()
				{

					if(calendar)
					{
						calendar.toggleCalendar();
					}
					else
					{
						var options = Object.extend({'.$options.'
							})

						cal.show();
						calendar = new scal(\''. $params['div_id'] . '\', \'' . $params['field_id'] . '\', options);';

	if (isset($params['start_date']) && $params['start_date'])
	{
		$ret .= '
						calendar.start_date = \'' . $params['start_date'] . '\';';
	}

	if (isset($params['year']))
	{
		$ret .= '
						calendar.showYearChanger();';

		if (is_numeric($params['year']) && !isset($params['value'])) {
			$ret .= '
						calendar.setCurrentDate(new Date(' . ((int)date('Y') + $params['year']) . ', ' . ((int)date("n") - 1) . ', ' . date("j") . '));';
		}
	}

	if (isset($params['value']))
	{
		$val = explode(' ', $params['value']);
		$val = $val[0];

		$val = explode('-', $val);

		if ($val[1] <= 0 && $val[1] > 11)
			$val[1] = 0;
		else
			(int)$val[1]--;

		$value = implode(',', $val);

		$ret .= '
						calendar.setCurrentDate(new Date(' . $value . '));';
	}

	$ret .= '
					}
				}

				var open_calendar = function()
				{
					if(calendar)
						calendar.openCalendar();
					else
						toogle_calendar();
				}

				cal.style.display = \'none\';
				';

	if (isset($params['left']))
		$ret .= 'cal.style.left = \'' . $params['left'] . 'px\';';

	if (isset($params['right']))
		$ret .= 'cal.style.right = \'' . $params['right'] . 'px\';';

	if (isset($params['top']))
		$ret .= 'cal.style.top = \'' . $params['top'] . 'px\';';

	if (isset($params['bottom']))
		$ret .= 'cal.style.bottom = \'' . $params['bottom'] . 'px\';';

	$ret .= '
			</script>
			<input '.(isset($params['class']) ? 'class="'.$params['class'] . '" ': '') . 'type="text" name="' . $params['field_name'] . '" value="' . (isset($params['value']) ? $params['value'] : '') .'" id="' . $params['field_id'] . '" />
			<img style="cursor: pointer;" src="' . URL_STATIC_CONTENT . '/image/calendar_small.gif" alt="Wybierz datę" onclick="toogle_calendar()" />';

	return $ret;
}

?>
