<?php

class Intense_Widget_Ads extends WP_Widget {

	function __construct() {

		$widget_ops = array('classname' => 'ads', 'description' => 'Add multiple ads');
		$control_ops = array('id_base' => 'ads-widget');
		parent::__construct('ads-widget', '(Intense) Ads', $widget_ops, $control_ops);

	}

	function widget( $args, $instance ) {
		global $intense_visions_options;

		extract( $args );

		$ad_count = isset( $intense_visions_options['intense_widgets_ads_count'] ) ? $intense_visions_options['intense_widgets_ads_count'] : 4;
		$width = !empty( $instance[ 'ads_img_width' ] ) ? $instance[ 'ads_img_width' ] : 125;
		$height = !empty( $instance[ 'ads_img_height' ] ) ? $instance[ 'ads_img_height' ] : $width;

		if ( is_numeric( $width ) ) $width -= 2;
		if ( is_numeric( $height ) ) $height -= 2;

		echo $before_widget;

		$title = apply_filters( 'widget_title', isset( $instance['title'] ) ? $instance['title'] : '' );

		if ( $title ) {
			echo $before_title . $title . $after_title;
		}
		?>

		<div id="ads_<?php echo $widget_id; ?>" class="intense widget-ads" >
			
			<?php for( $i = 1; $i <= $ad_count; $i++ ) : ?>
				<?php if ( $instance[ 'ads_img_' . $i ] && $instance[ 'ads_link_' . $i ] ) : ?>
					<div class="widget-ad" style="float: left;">						
						<a href="<?php echo $instance[ 'ads_link_' . $i ]; ?>">
							<img src="<?php echo $instance[ 'ads_img_' . $i ]; ?>" alt="" width="<?php echo $width; ?>" height="<?php echo $height; ?>" />
						</a>						
					</div>
				<?php endif; ?>
			<?php endfor; ?>

			<div class="clearfix"></div>
		</div>

		<?php
		
		echo $after_widget;
	}

	function update( $new_instance, $old_instance ) {
		global $intense_visions_options;

		$instance = $old_instance;

		$ad_count = isset( $intense_visions_options['intense_widgets_ads_count'] ) ? $intense_visions_options['intense_widgets_ads_count'] : 4;

		$instance[ 'ads_img_width' ] = $new_instance[ 'ads_img_width' ];
		$instance[ 'ads_img_height' ] = $new_instance[ 'ads_img_height' ];

		for( $i = 1; $i <= $ad_count; $i++ )  {
			$instance[ 'ads_img_' . $i ] = $new_instance[ 'ads_img_' . $i ];
			$instance[ 'ads_link_' . $i ] = $new_instance[ 'ads_link_' . $i ];			
		}

		return $instance;

	}

	function form( $instance ) {
		global $intense_visions_options;

		$ad_count = isset( $intense_visions_options['intense_widgets_ads_count'] ) ? $intense_visions_options['intense_widgets_ads_count'] : 4;

		$defaults = array(
			'ads_img_width' => 125,
			'ads_img_height' => 125
		);

		for( $i = 1; $i <= $ad_count; $i++ )  {			
			$defaults[ 'ads_img_' . $i ] = '';
			$defaults[ 'ads_link_' . $i ] = '';			
		}		

		$instance = wp_parse_args( (array) $instance, $defaults );

		?>

		<p>
			<label for="<?php echo $this->get_field_id( 'ads_img_width' ); ?>"><?php _e( 'Width:', 'intense' ); ?></label>
			<input type="text" id="<?php echo $this->get_field_id( 'ads_img_width' ); ?>" name="<?php echo $this->get_field_name( 'ads_img_width' ); ?>" value="<?php echo $instance['ads_img_width']; ?>" size="3" />
			&nbsp;&nbsp;X&nbsp;&nbsp;
			<label for="<?php echo $this->get_field_id( 'ads_img_height' ); ?>"><?php _e( 'Height:', 'intense' ); ?></label>
			<input type="text" id="<?php echo $this->get_field_id( 'ads_img_height' ); ?>" name="<?php echo $this->get_field_name( 'ads_img_height' ); ?>" value="<?php echo $instance['ads_img_height']; ?>" size="3" />
		</p>

		<?php

		for( $i = 1; $i <= $ad_count; $i++ )  {
			?>			
			<p><strong><?php printf( __( 'Ad %s', 'intense' ), $i ); ?></strong></p> 
			<p>
				<label for="<?php echo $this->get_field_id( 'ads_img_' . $i ); ?>"><?php _e( 'Image URL:', 'intense' ); ?></label>
				<input class="widefat" type="text" id="<?php echo $this->get_field_id( 'ads_img_' . $i ); ?>" name="<?php echo $this->get_field_name( 'ads_img_' . $i ); ?>" value="<?php echo $instance['ads_img_' . $i]; ?>" />
			</p>
			<p>
				<label for="<?php echo $this->get_field_id( 'ads_link_' . $i ); ?>"><?php _e( 'Link URL:', 'intense' ); ?></label>
				<input class="widefat" type="text" id="<?php echo $this->get_field_id( 'ads_link_' . $i ); ?>" name="<?php echo $this->get_field_name( 'ads_link_' . $i ); ?>" value="<?php echo $instance['ads_link_' . $i]; ?>" />
			</p>
			<?php
		}		
	}	
}
