<?php
	class st_hybrid_adv extends WP_Widget{
	
		function st_hybrid_adv() 
		{
			parent::WP_Widget(false, $name = 'Hybrid > Adv');
		}
		
		function widget($args, $instance)
		{
			extract( $args );
			$title = apply_filters('widget_title', $instance['title']);	
			$advName = $instance['advName'];
			if ( !(is_page_template('template-blog.php') || is_home() || is_single())) {
					echo '<div class="span3">';// Display some text
			}		
			global $post;
			setup_postdata( $post ); 

			  ?>
			  <?php $advertising  = get_field('ads_container','options');
				if($advertising):?>		 	
			  <h3><?php echo $title ; ?></h3>
					<div id="col-container">
						<?php
							foreach($advertising as $adv)
							{
								if($advName == $adv['unique_name']){
									$adv =  $adv['ads'];
									foreach($adv as $ad){
										echo '<div class="adv">';
										if($ad['image']){
										 $image = wp_get_attachment_image_src($ad['image'], 'adv' ); 
										   echo '<a href="'.$ad['url'].'"><img src="'.$image[0].'"/></a>';
										 }				
										echo '</div>';
									}
								}
								
							}
						?>
					</div>
			<?php endif;?>				
		<?php
         if ( !(is_page_template('template-blog.php') || is_home() || is_single())) {
					echo '</div>';
					}
		}
		
		function update($new_instance, $old_instance)
		{
			$instance = $old_instance;
			$instance['title'] = strip_tags($new_instance['title']);
			$instance['advName'] = strip_tags($new_instance['advName']);
			return $instance;
		}
		
		function form($instance)
		{
			$title = isset($instance['title']) ? esc_attr($instance['title']) : "";		
			$advName = isset($instance['advName']) ? esc_attr($instance['advName']) : "";	
			?>
			<p>
				<label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Widget Title',"hybrid_lang"); ?></label>
				<input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo $title; ?>" />
			</p>
			<p>
				<label for="<?php echo $this->get_field_id('advName'); ?>"><?php _e('Adv Name',"hybrid_lang"); ?></label>
				<input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('advName'); ?>" type="text" value="<?php echo $advName; ?>" />
			</p>
			<?php
		}
	}
	
	function st_hybrid_widgets_adv() {			
		register_widget('st_hybrid_adv');			
	}
	add_action('widgets_init', 'st_hybrid_widgets_adv');
?>