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

// Configure social channels
$configured_social_channels = array();
$num_configured             = 0;

$this->configure_social_channels( $configured_social_channels, $num_configured );
?>
<div class="it-form-field-social-channels clearfix" id="<?php $this->get( 'id' ); ?>">
	<div class="it-sortable-list pull-left<?php if ( 0 == $num_configured ) echo ' hide'; ?>" data-name="<?php $this->get( 'name' ); ?>">
		<ul class="it-items-list">
<?php
foreach ( $configured_social_channels as $name => $link ) :

if ( ! empty( $link ) ) :

// Get checking state
$checked = false;

if ( isset( $this->value[ $name ] ) && (boolean) $this->value[ $name ] ) {
	$checked = true;
} elseif ( ! isset( $this->value[ $name ] ) && isset( $this->social_channels[ $name ]['default'] ) && 'checked' == $this->social_channels[ $name ]['default'] ) {
	$checked = true;
}

// Temporary manipulate name/value
$this->attributes['name']  = "{$this->name}[{$name}]";
$this->attributes['value'] = $this->value[ $name ];
?>
			<li class="it-item ui-state-default it-social-channels-<?php esc_attr_e( $name ); ?>">
				<label class="checkbox-inline" for="<?php $this->get( 'id' ); esc_attr_e( "-{$name}" ); ?>">
					<input type="hidden" <?php $this->html_attributes( array( 'id', 'type' ) ); ?> />
					<input id="<?php $this->get( 'id' ); esc_attr_e( "-{$name}" ); ?>" type="checkbox" class="checkbox" onclick="jQuery(this).prev().val(this.checked ? 1 : 0).trigger('change');" <?php if ( $checked ) echo 'checked="checked"'; ?> />
					<?php esc_html_e( $this->social_channels[ $name ]['label'], IT_THEME_TEXTDOMAIN ); ?>
				</label>
			</li>
<?php
endif;

endforeach;
?>
		</ul>
	</div>
	<a class="btn btn-default pull-left" href="javascript:void(0)">...</a>
	<p class="pull-left it-social-channels-status<?php if ( $num_configured ) echo ' hide'; ?>">
		&nbsp;&nbsp;<?php _e( 'No configured social channel.', IT_THEME_TEXTDOMAIN ); ?>
	</p>
<?php
// Load assets if not already loaded
if ( ! defined( 'IT_SOCIAL_CHANNELS_INITIALIZED' ) ) :

define( 'IT_SOCIAL_CHANNELS_INITIALIZED', true );

IT_Init_Assets::load( 'it-social-channels-js', 'wp-jquery-ui-dialog-css' );

$script = '
		new $.IT_SocialChannels();';

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

// Support theme customizer
$support = '';

if ( isset( $this->attributes['data-customize-setting-link'] ) ) {
	$support = 'data-customize-setting-link="it_social_channels"';
}

// Generate social channel configuration modal
?>
	<div id="it-social-channels-modal" class="jsn-bootstrap hidden row" title="<?php _e( 'Social Channels Configuration', IT_THEME_TEXTDOMAIN ); ?>">
		<div class="form-horizontal" role="form">
			<?php
			foreach ( $this->social_channels as $name => $data ) :

			$value = isset( $configured_social_channels[ $name ] ) ? $configured_social_channels[ $name ] : $data['link'];
			?>
			<div class="form-group">
				<label class="col-xs-2 control-label" for="it-social-channels-config-<?php esc_attr_e( $name ); ?>"><?php esc_html_e( $data['label'], IT_THEME_TEXTDOMAIN ); ?></label>
				<div class="col-xs-8">
					<input class="form-control input-sm" id="it-social-channels-<?php esc_attr_e( $name ); ?>" name="it_social_channels[<?php esc_attr_e( $name ); ?>]" placeholder="<?php esc_attr_e( $data['placeholder'] ); ?>" type="text" value="<?php esc_attr_e( $value ); ?>" <?php echo '' . $support; ?> />
				</div>
			</div>
			<?php endforeach; ?>
		</div>
	</div>
<?php endif; ?>
</div>
