<?php
/**
 * @version    $Id$
 * @package    IT_Theme_Framework
 * @author     InnoThemes Team <support@innothemes.com>
 * @copyright  Copyright (C) 2012 InnoThemes.com. All Rights Reserved.
 * @license    GNU/GPL v2 or later http://www.gnu.org/licenses/gpl-2.0.html
 *
 * Websites: http://www.innothemes.com
 */

// Prepare placefolder
if ( 'Y-m-d' == $this->placeholder ) {
	$this->placeholder = date( 'Y-m-d' );
}
?>
<div class="it-form-field-date-picker input-group" id="<?php $this->get( 'id' ); ?>">
	<input type="text" <?php $this->html_attributes( array( 'id', 'type' ) ); ?> />
	<span class="input-group-btn">
		<a href="javascript:void(0);" class="btn btn-default btn-sm" title="<?php _e( 'Pick Date', IT_THEME_TEXTDOMAIN ); ?>">...</a>
	</span>
<?php
// Load Javascript assets and initialization if not already loaded
if ( ! defined( 'IT_DATE_INITIALIZED' ) ) :

define( 'IT_DATE_INITIALIZED', true );

IT_Init_Assets::load( 'jquery-ui-datepicker' );

$script = '
		$("div.it-form-field-date-picker > .input-group-btn > a.btn").each(function(i, e) {
			$(e).click(function(event) {
				event.preventDefault();

				this.it_date_picker = this.it_date_picker || $(this).parent().prev().datepicker({
					showOn: "focus",
					dateFormat: "yy-mm-dd",
					defaultDate: $(e).parent().prev().val(),
					onSelect: function(dateText, inst) {
						$(this).val(dateText);
					}
				});

				this.it_date_picker.datepicker("show");
			});
		})';

IT_Init_Assets::inline( 'js', $script, true );

endif;
?>
</div>
