<?php
/* groupoflinks */
if(!class_exists('em_groupedlinks')) {
	class em_groupedlinks extends AQ_Block {

		protected $the_options;
		protected $the_child_options;

		function __construct() {
			$block_options = array(
				'pb_block_icon' => 'simpleicon-link',
				'pb_block_icon_color' => '#3D9970',
				'name' => __('Group of Links','mthemelocal'),
				'size' => 'span12',
				'tab' => __('Elements','mthemelocal'),
				'desc' => __('Add Link set','mthemelocal')
			);
			//create the widget

			/*-----------------------------------------------------------------------------------*/
			/*	groupoflinks
			/*-----------------------------------------------------------------------------------*/

			$mtheme_shortcodes['groupoflinks'] = array(
			    'params' => array(),
			    'no_preview' => true,
			    'shortcode_desc' => __('Generates group of links', 'mthemelocal'),
			    'shortcode' => '[groupoflinks] {{child_shortcode}} [/groupoflinks]',
			    'popup_title' => __('Insert Group of Links', 'mthemelocal'),		    
			    'child_shortcode' => array(
			        'params' => array(
			            'name' => array(
			                'std' => __('Link Name','mthemelocal'),
			                'type' => 'text',
			                'label' => __('Name', 'mthemelocal'),
			                'desc' => __('Name', 'mthemelocal'),
			            ),
			            'link' => array(
			                'std' => '',
			                'type' => 'text',
			                'label' => __('link', 'mthemelocal'),
			                'desc' => __('link', 'mthemelocal'),
						),
						'active' => array(
							'type' => 'select',
							'label' => __('Active Link', 'mthemelocal'),
							'desc' => __('Active Link', 'mthemelocal'),
							'options' => array(
								'no' => __('No','mthemelocal'),
								'yes' => __('Yes','mthemelocal')
							)
						),
			        ),
					'title_field' => 'name',
			        'shortcode' => '[group_thelink target="{{target}}" link="{{link}}" active="{{active}}" name="{{name}}"]',
			        'clone_button' => __('+ Add Group of Links', 'mthemelocal')
			    )
			);
			$this->the_options = $mtheme_shortcodes['groupoflinks'];
			$this->the_child_options = $mtheme_shortcodes['groupoflinks']['child_shortcode'];

			parent::__construct('em_groupedlinks', $block_options);

			//add ajax functions
			add_action('wp_ajax_aq_block_groupoflinks_add_new', array($this, 'add_groupoflinks_tab'));
		}

		function form($instance) {

			$defaults = array(
				'tabs' => array(
					0 => array(
						'name' => __('Link Name','mthemelocal'),
						'link' => 'http://example.com/',
						'active' => 'no',
					)
				),
				'title' => __('Links Tab','mthemelocal'),
				'type'	=> 'tab',
				'entrance_animation' => ''
			);

			$instance = wp_parse_args($instance, $defaults);
			extract($instance);
			?>
			<div class="description cf">
			<?php
			echo mtheme_generate_builder_form($this->the_options,$instance);
			?>
			
				<ul id="aq-sortable-list-<?php echo $block_id ?>" class="aq-sortable-list" rel="<?php echo $block_id ?>">
					<?php
					// echo '<pre>THis is it';
					// print_r($instance);
					// echo '</pre>';
					$tabs = is_array($tabs) ? $tabs : $defaults['tabs'];
					// echo '<pre>THis is it after';
					// print_r($tabs);
					// echo '</pre>';
					$count = 1;
					foreach($tabs as $tab) {
						$this->tab($tab, $count);
						$count++;
					}
					?>
				</ul>
				<p></p>
				<a href="#" rel="groupoflinks" class="aq-sortable-add-new button"><?php _e('Add New','mthemelocal'); ?></a>
				<p></p>
			</div>
			<?php
		}

		function tab($tab = array(), $count = 0) {

			$child_field_id = $this->get_field_id('tabs');
			$child_field_name = $this->get_field_name('tabs');
			$child_count = $count;

			mtheme_create_tab( $this->the_child_options, $tab, $child_count, $child_field_id, $child_field_name );

		}

		function block($instance) {
			extract($instance);

			// echo '<pre>';
			// print_r ($instance);
			// echo '</pre>';

			$shortcode = mtheme_dispay_build($this->the_options,$block_id,$instance);
			mtheme_process_shortcode($shortcode,$encode=true);
		}
		function tabbed_method() {
		}
		/* AJAX add tab */
		function add_groupoflinks_tab() {

			$count = isset($_POST['count']) ? absint($_POST['count']) : false;
			$this->block_id = isset($_POST['block_id']) ? $_POST['block_id'] : 'mtheme-block-9999';

			//default key/value for the tab
			$tab = array(
				'name' => __('Link Name','mthemelocal'),
				'link' => '',
				'active' => '',
			);

			if($count) {
				$this->tab($tab, $count);
			} else {
				die(-1);
			}

			die();
		}

		function update($new_instance, $old_instance) {
			$new_instance = mtheme_recursive_sanitize($new_instance);
			return $new_instance;
		}
	}
}
