<?php
add_action('widgets_init','tigu_banner_300x250_load_widgets');

function tigu_banner_300x250_load_widgets(){
	   register_widget("Tigu_Banner_300x250_Widget");
}

class Tigu_Banner_300x250_Widget extends WP_widget{
	
	function Tigu_Banner_300x250_Widget(){
		$widget_ops = array(
       'classname' => 'tigu_banner_300x250_widget', 
       'description' => 'Display a 300x250 banner on Sidebar.');
		$control_ops = array(
       'id_base' => 'tigu_banner_300x250_widget');
		$this->WP_Widget(
        'tigu_banner_300x250_widget', 
        'GAMELEON: 300x250 banner', 
         $widget_ops, 
         $control_ops);
		
	}
	/* User selected settings. */
	function widget($args,$instance){
		extract($args);
        $show_home = isset($instance['show_home']) ? 'true' : 'false';
        $show_page = isset($instance['show_page']) ? 'true' : 'false';
        $show_single = isset($instance['show_single']) ? 'true' : 'false';
		$link = $instance['link'];
		$alt = $instance['alt'];
		$image = $instance['image'];
		$code = $instance['code'];
        if ( ( is_home() && $show_home == 'false')  ||  ( is_page() && $show_page == 'false' ) || ( is_single() && $show_single == 'false' )) {
        } else {
        echo $before_widget;
		?>
 
		<?php
		if($image) {	
		?>				
			<div class="banner_sidebar300x250"><a href="<?php echo $link; ?>"><img src="<?php echo $image; ?>" alt="<?php echo $alt; ?>" /></a></div>
		<?php
		}
		else {
		?>
			<div class="banner_sidebar300x250"><?php echo stripslashes($code); ?></div>
	<?php
		}
		echo $after_widget;
	}
	}
	
	function update($new_instance, $old_instance){
		$instance = $old_instance;
		
		$instance['link'] = $new_instance['link'];
		$instance['alt'] = $new_instance['alt'];
		$instance['image'] = $new_instance['image'];
		$instance['code'] = $new_instance['code'];
		$instance['show_home'] = $new_instance['show_home'];
		$instance['show_page'] = $new_instance['show_page'];
		$instance['show_single'] = $new_instance['show_single'];
		return $instance;
	}
	
	function form($instance){
		$defaults = array(
        'show_home' => 'on',
        'show_page' => 'on',
        'show_single' => 'on',
        'link' => '', 
        'alt' => 'banner', 
        'image' => '', 
        'code' => '');
		$instance = wp_parse_args((array) $instance, $defaults); ?>
		<h4>USING IMAGE</h4>
		
		<p>
			<label for="<?php echo $this->get_field_id('image'); ?>">Image Url:</label>
			<input class="widefat" style="width: 216px;" id="<?php echo $this->get_field_id('image'); ?>" name="<?php echo $this->get_field_name('image'); ?>" value="<?php echo $instance['image']; ?>" />
		</p>
	   <p>
			<label for="<?php echo $this->get_field_id('link'); ?>">Target Url:</label>
			<input class="widefat" style="width: 216px;" id="<?php echo $this->get_field_id('link'); ?>" name="<?php echo $this->get_field_name('link'); ?>" value="<?php echo $instance['link']; ?>" />
		</p>
        <p>
			<label for="<?php echo $this->get_field_id('alt'); ?>">Image <em>alt</em> attribute:</label>
			<input class="widefat" style="width: 216px;" id="<?php echo $this->get_field_id('alt'); ?>" name="<?php echo $this->get_field_name('alt'); ?>" value="<?php echo $instance['alt']; ?>" />
         An imagine element must have an <em>alt</em> attribute, useful for markup validation.
	    </p>

		<h4 style='margin-top: 40px;'>OR USING CUSTOM CODE</h4>
		<p>
			<textarea class="widefat" rows="10" cols="10" id="<?php echo $this->get_field_id('code'); ?>" name="<?php echo $this->get_field_name('code'); ?>"><?php echo $instance['code']; ?></textarea>
		 </p>
         <p>
         <input class="checkbox" type="checkbox" <?php checked($instance['show_home'], 'on'); ?> id="<?php echo $this->get_field_id('show_home'); ?>" name="<?php echo $this->get_field_name('show_home'); ?>" />
         <label for="<?php echo $this->get_field_id('show_home'); ?>">Show on Home Page?</label>
         </p>
         <p>
         <input class="checkbox" type="checkbox" <?php checked($instance['show_page'], 'on'); ?> id="<?php echo $this->get_field_id('show_page'); ?>" name="<?php echo $this->get_field_name('show_page'); ?>" />
         <label for="<?php echo $this->get_field_id('show_page'); ?>">Show on Single Page?</label>
        </p>
        <p>
         <input class="checkbox" type="checkbox" <?php checked($instance['show_single'], 'on'); ?> id="<?php echo $this->get_field_id('show_single'); ?>" name="<?php echo $this->get_field_name('show_single'); ?>" />
         <label for="<?php echo $this->get_field_id('show_single'); ?>">Show on Single Post?</label>
        </p>
		<?php

	}
}
?>