<?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
 */

// Count options
$num_checkbox = count( $this->choices );

// Prepare name attribute
$this->attributes['name'] = ( $num_checkbox == 1 ) ? $this->name : "{$this->name}[]";
?>
<div class="it-form-field-checkbox-image it-form" id="<?php $this->get( 'id' ); ?>">
<?php
// Backup original attributes
$original_attrs = $this->attributes;

foreach ( $this->choices as $value => $image ) :

// Count the number of image
$i = isset( $i ) ? $i + 1 : 1;

// Update attributes
$this->attributes['value'] = $value;

if ( ( 1 == $num_checkbox && $this->value == $value ) || ( $num_checkbox > 1 && @in_array( $value, $this->value ) ) ) {
	$this->attributes['checked'] = 'checked';
}

// Prepare additional attributes
$label = null;
$icon  = '';

if ( is_array( $image ) ) {
	foreach ( $image as $k => $v ) {
		if ( 'label' == $k ) {
			$label = $v;
		} elseif ( 'use_icon' == $k ) {
			$icon = $v;
		} elseif ( 'image' != $k ) {
			$this->attributes[ $k ] = $v;
		}
	}

	if ( isset( $image['image'] ) ) {
		$image = $image['image'];
	}
}

// If image link is relative, prepend root URL
if ( is_string( $image ) && strpos( $image, '://' ) === false ) {
	if ( @is_file( IT_THEME_PATH . "/{$image}" ) ) {
		$image = IT_THEME_URI . "/{$image}";
	} else {
		$image = site_url( $image );
	}
}
?>
	<div class="pull-left<?php if ($this->image_per_row > 0 && $i % $this->image_per_row == 1) echo ' clear'; ?>">
		<div class="hide">
			<label class="checkbox-inline">
				<input <?php $this->html_attributes( array( 'id', 'placeholder' ) ); ?> />
				<?php esc_html_e( $label, IT_THEME_TEXTDOMAIN ); ?>
			</label>
		</div>
		<?php if ( ! $this->use_icon ) : ?>
		<img class="it-thumbnail<?php if ( isset( $this->attributes['checked'] ) ) echo ' it-form-field-checkbox-image-selected'; ?>" src="<?php echo esc_url( $image ); ?>" alt="<?php if ( empty( $label ) ) esc_attr_e( $this->attributes['value'] ); else esc_attr_e( $label, IT_THEME_TEXTDOMAIN ); ?>" title="<?php if ( empty( $label ) ) esc_attr_e( $this->attributes['value'] ); else esc_attr_e( $label ); ?>" />
		<?php else : ?>
		<i class="it-thumbnail<?php echo ' ' . $icon; if ( isset( $this->attributes['checked'] ) ) echo ' it-form-field-checkbox-image-selected'; ?>" title="<?php if ( empty( $label ) ) esc_attr_e( $this->attributes['value'] ); else esc_attr_e( $label ); ?>"></i>
		<?php endif; ?>
	</div>
<?php
// Restore original attributes
$this->attributes = $original_attrs;

endforeach;
?>
	<div class="clear"></div>
<?php
// Load Javascript assets and initialization if not already loaded
if ( ! defined( 'IT_CHECKBOX_IMAGE_INITIALIZED' ) ) :

define( 'IT_CHECKBOX_IMAGE_INITIALIZED', true );

$script = '
		// Init inline radio image
		$(".it-form-field-checkbox-image .it-thumbnail").each(function(i, e) {
			$(e).unbind("click").bind("click", function(event) {
				event.preventDefault();

				// Select / de-select clicked checkbox image
				if ($(this).hasClass("it-form-field-checkbox-image-selected")) {
					$(this).removeClass("it-form-field-checkbox-image-selected").prev().find("input").removeAttr("checked");
				} else {
					$(this).addClass("it-form-field-checkbox-image-selected").prev().find("input").attr("checked", "checked");
				}

				// Trigger change event on the hidden checkbox input
				$(this).prev().find("input").trigger("change");
			});
		});';

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

endif;
?>
</div>
