function widget( $args, $instance ){
	
		extract( $args );
		
		$ad_id = $instance['m21_ad_id'];
		
		/* if the title was entered by the user, get it */
		$title =  !empty( $instance['title'] ) ? $instance['title'] : '';
		
		//Get company name
		$m21_ad_account_name = strip_tags( get_post_meta( $ad_id, 'adAccountName', true ) );
		
		//get link url
		$m21_ad_linkurl = esc_url( get_post_meta( $ad_id, 'adLinkUrl', true ) );

		//get ad format
		$m21_ad_format_id = strip_tags( get_post_meta( $ad_id, 'adFormat', true ) );
		
		//get ad format dimensions
		$ad_format = mobility21_get_ad_format( $m21_ad_format_id );
		$m21_ad_width = intval( $ad_format['width'] );
		$m21_ad_height = intval( $ad_format['height'] );
		
		/* URL to the image used */
		$m21_ad_imageurl = esc_url( get_post_meta( $ad_id, 'adImageUrl', true ) );
		
		/* Check if image should be resized to the ad format dimensions */
		if( $instance['m21_ad_resize'] == 'checked'){
		
			/* Height of the image */
			$image_height = ( !empty( $m21_ad_width ) ) ? 'height="' . $m21_ad_height . '"' : '';
			
			/* Width of the image */
			$image_width = (  !empty($m21_ad_width) ) ? 'width="' . $m21_ad_width . '"' : '';
			
		} else {
			
			$image_height = "";
			$image_width = "";
			
		}
		
		/* href URL */
		$link_url = ( !empty( $m21_ad_linkurl ) ) ? $m21_ad_linkurl : '';
		
		/* Link text */
		$link_text = ( !empty( $m21_ad_account_name ) ) ? $$m21_ad_account_name : '';
		
		/* Link title */
		$link_title = ( !empty( $m21_ad_account_name ) ) ? 'title="' . $m21_ad_account_name . '"' : '';
		
		/* div class wrapping the image button */
		$div_class = ( empty( $instance['div_class'] ) ) ? 'image-button-wrap' : $instance['div_class'] ;
		
		
		/* Check if image should be resized to the ad format dimensions */
		if( $instance['m21_ad_resize'] == 'checked'){
			
			//Use Tim Thumb script to resize the image
			$thumb_width= $m21_ad_width; //width
			$thumb_height= $m21_ad_height; //height without
			$zoom_crop=0; //resize to fit specified dimensions
			$quality = 75; //default 75 max 100
			$script_path = MOBILITY_SCRIPTS_URI . '/thumb.php';
			
			/* Check if image should be resized proportionally */
			if( $instance['m21_ad_zoomcrop'] == 'checked' ){
				
				$zoom_crop = 3; //Resize proportionally while adjusting size of scaled image
				
			}
			
			$out = '<img src="' . $script_path . '?src=' . $m21_ad_imageurl . '&w=' . $thumb_width . '&h=' . $thumb_height . '&zc=' . $zoom_crop . '" />';
		
		} else {
			
			//display image as is
			$out = '<img src="' . $m21_ad_imageurl . '" ' . $image_alt_text . ' />';
		
		}
		
		
		//wrap with link tags if link url is not empty
		if( !empty( $m21_ad_linkurl ) ){
			$out = '<a href="' . $m21_ad_linkurl . '" ' . $link_title . ' >' . $out . '</a>';
		}
		
		//check whether to use default styles
		if( $instance['m21_ad_custom_style'] != 'checked' ){
			$out = '<div class="m21-ad-default-style" style="background: #fff; display: table-cell; text-align: center; vertical-align: middle; width: ' . $m21_ad_width . 'px; height:' . $m21_ad_height . 'px;" ><div style="overflow: hidden; width: ' . $m21_ad_width . 'px;">' . $out . '</div></div>';
		}
		
		//wrap final output inside div tags
		$out = '<div class="m21-ad-wrap m21-single-ad">' . $out . '</div>';
		
		echo $before_widget;
		
		if( !empty( $title ) ){
			echo $before_title . $title . $after_title;
		}
		
		echo $out;
		echo $after_widget;
		
	}
	function widget( $args, $instance ){
	
		extract( $args );
		
		$out = '';
		
		$zoom_crop= $instance['resize_mode']; //0 | 1| 2 | 3
		$quality = 75; //default 75 max 100
		$script_path = MOBILITY_SCRIPTS_URI . '/thumb.php';
		
		//get selected ads
		$posts_to_get = empty( $instance['number_ads'] ) ? -1 : $instance['number_ads'];
		
		//get selected ads
		$args_ads = array( 	'post_type' => 'advertisement',
								'posts_per_page' => $posts_to_get,
								'orderby' => 'rand',
								'meta_query' => array(
									array(
										'key' => 'adAccountStatus',
										'value' => 'activated',
										'compare' => '='
									),
									array(
										'key' => 'adFormat',
										'value' => '',
										'compare' => 'NOT LIKE'
									)
								)
						);
		
		//add taxonomy parameter
		if( !empty( $instance['ad_group']) ){
			$args_ads['tax_query'][] = array( 'taxonomy' => 'ad_group',
												'field'=> 'id',
												'terms' => $instance['ad_group']
											);
		}
		//add a custom field parameter
		if( !empty( $instance['ad_format']) ){
			$args_ads['meta_query'][] = array( 'key' => 'adFormat',
												'value' => $instance['ad_format'],
												'compare' => '='
											);
		}
						
		$selected_ads = get_posts( $args_ads );	
		
		//loop and show the ads
		foreach( $selected_ads as $ad ){
			
			//get url to the ad image
			$ad_imageurl = esc_url( get_post_meta( $ad->ID, 'adImageUrl', true ) );
			
			//get link url to the image if any
			$ad_linkurl = esc_url( get_post_meta( $ad->ID, 'adLinkUrl', true ) );
			
			//get adformat id
			$ad_format_id = get_post_meta( $ad->ID, 'adFormat', true );
			
			//get adformat details
			$ad_format = mobility21_get_ad_format( $ad_format_id );
			
			//setup Tim Thumb
			$thumb_width= intval( $ad_format['width'] ); //width
			$thumb_height= intval( $ad_format['height'] ); //height without
			
			$ad_out = '<img src="' . $script_path . '?src=' . $ad_imageurl . '&w=' . $thumb_width . '&h=' . $thumb_height . '&zc=' . $zoom_crop . '&q=' . $quality . '" />';
			
			if( !empty($ad_linkurl) ){
				//wrap in anchor <a> tags
				$ad_out = '<a href="' . $ad_linkurl . '" >' . $ad_out . '</a>';
			}
			
			//wrap in <li> tags
			$ad_out = '<li class="ad-wrap">' . $ad_out . '</li>';
			
			//finally add to $out
			$out .= $ad_out;
			
		}//endforeach

		//wrap in <ul> tags
		$out = '<ul class="m21-random-ad-wrap">' . $out . '</ul>';
		
		echo $before_widget;
		
		if( !empty( $title ) ){
			echo $before_title . $title . $after_title;
		}
		
		echo $out;
		echo $after_widget;
		
	}