<?php
/**
 * Advertisement 125 Widget Class
 */
class Theme_Widget_SideBox extends WP_Widget {

	function Theme_Widget_SideBox() {
		$widget_ops = array('classname' => 'widget_sidebox', 'description' => __( 'Displays a info box', THEME_ADMIN_DOMAIN ) );
		$this->WP_Widget('sidebox', THEME_SLUG.' - '.__('SideBox', THEME_ADMIN_DOMAIN), $widget_ops);
		
		if ('widgets.php' == basename($_SERVER['PHP_SELF'])) {
			add_action( 'admin_print_scripts', array(&$this, 'add_admin_script') );
		}
	}
	
	function add_admin_script(){
		wp_enqueue_script( 'advertisement-widget', THEME_ADMIN_ASSETS_URI . '/js/advertisement-widget.js', array('jquery'));
	}

	function widget( $args, $instance ) {
		extract( $args );
		$wd_title = apply_filters('widget_title', empty($instance['title']) ? '' : $instance['title'], $instance, $this->id_base);

		$count = (int)$instance['count'];

		echo $before_widget;
		echo $before_title . $wd_title . $after_title;
		if( $count > 0){
			for($i=1; $i<= $count; $i++){
			$image = isset($instance['sidebox_'.$i.'_image'])?$instance['sidebox_'.$i.'_image']:'';
			$title = isset($instance['sidebox_'.$i.'_title'])?$instance['sidebox_'.$i.'_title']:'';
			$link = isset($instance['sidebox_'.$i.'_link'])?$instance['sidebox_'.$i.'_link']:'';
			$content = isset($instance['sidebox_'.$i.'_content'])?$instance['sidebox_'.$i.'_content']:'';
			if(empty($image)){
				$image = THEME_IMAGES.'/ad.png';
			}
?>
<div class="sidebox">
	<div class="sidebox-top"><!--  --></div>
	<div class="sidebox-middle">
		<div class="pad">
			<div class="icon"><img width="37" height="37" alt="" src="<?php echo $image;?>"></div>
			<div class="text">
				<h4><a href="<?php echo $link;?>"><?php echo $title;?></a></h4>
				<p><?php echo $content;?></p>
			</div>
			<div class="clear"></div>
		</div>
	</div>
	<div class="sidebox-bottom"><!--  --></div>
</div>
<?php			
			}
		}
		
		echo $after_widget;
	}

	function update( $new_instance, $old_instance ) {
		$instance = $old_instance;
		$instance['title'] = strip_tags($new_instance['title']);
		$instance['count'] = (int) $new_instance['count'];
		for($i=1;$i<=$instance['count'];$i++){
			$instance['sidebox_'.$i.'_image'] = strip_tags($new_instance['sidebox_'.$i.'_image']);
			$instance['sidebox_'.$i.'_title'] = strip_tags($new_instance['sidebox_'.$i.'_title']);
			$instance['sidebox_'.$i.'_link'] = strip_tags($new_instance['sidebox_'.$i.'_link']);
			$instance['sidebox_'.$i.'_content'] = strip_tags($new_instance['sidebox_'.$i.'_content']);
		}
		return $instance;
	}

	function form( $instance ) {
		$title = ! empty($instance['title']) ? esc_attr($instance['title']) : 'SideBox';
		$count = ! empty($instance['count']) ? absint($instance['count']) : 4;
		for($i=1;$i<=10;$i++){
			$sidebox_image = 'sidebox_'.$i.'_image';
			$$sidebox_image = isset($instance[$sidebox_image]) ? $instance[$sidebox_image] : '';
			$sidebox_title = 'sidebox_'.$i.'_title';
			$$sidebox_title = isset($instance[$sidebox_title]) ? $instance[$sidebox_title] : '';
			$sidebox_link = 'sidebox_'.$i.'_link';
			$$sidebox_link = isset($instance[$sidebox_link]) ? $instance[$sidebox_link] : '';
			$sidebox_content = 'sidebox_'.$i.'_content';
			$$sidebox_content = isset($instance[$sidebox_content]) ? $instance[$sidebox_content] : '';

		}
?>
		<p><label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Widget Title:', THEME_ADMIN_DOMAIN); ?></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('count'); ?>"><?php _e('How many sidebox to display?', THEME_ADMIN_DOMAIN); ?></label>
		<input id="<?php echo $this->get_field_id('count'); ?>" class="advertisement_count" name="<?php echo $this->get_field_name('count'); ?>" type="text" value="<?php echo $count; ?>" size="3" /></p>

		<p>
			<em><?php _e("Note: Please input FULL URL <br/>(e.g. <code>http://www.example.com</code>)", THEME_ADMIN_DOMAIN);?></em>
		</p>

		<div class="advertisement_wrap">
		<?php for($i=1;$i<=10;$i++): $sidebox_image = 'sidebox_'.$i.'_image';$sidebox_title = 'sidebox_'.$i.'_title';$sidebox_link = 'sidebox_'.$i.'_link';$sidebox_content = 'sidebox_'.$i.'_content'; ?>
			<div class="advertisement_<?php echo $i;?>" <?php if($i>$count):?>style="display:none"<?php endif;?>>
				<p><label for="<?php echo $this->get_field_id( $sidebox_image ); ?>"><?php printf(__('#%s Sidebox Icon Src:', THEME_ADMIN_DOMAIN),$i);?></label>
				<input class="widefat" id="<?php echo $this->get_field_id( $sidebox_image ); ?>" name="<?php echo $this->get_field_name( $sidebox_image ); ?>" type="text" value="<?php echo $$sidebox_image; ?>" /></p>
				<p><label for="<?php echo $this->get_field_id( $sidebox_title ); ?>"><?php printf(__('#%s Sidebox Title:', THEME_ADMIN_DOMAIN),$i);?></label>
				<input class="widefat" id="<?php echo $this->get_field_id( $sidebox_title ); ?>" name="<?php echo $this->get_field_name( $sidebox_title ); ?>" type="text" value="<?php echo $$sidebox_title; ?>" /></p>
				<p><label for="<?php echo $this->get_field_id( $sidebox_link ); ?>"><?php printf(__('#%s Sidebox Url:', THEME_ADMIN_DOMAIN),$i);?></label>
				<input class="widefat" id="<?php echo $this->get_field_id( $sidebox_link ); ?>" name="<?php echo $this->get_field_name( $sidebox_link ); ?>" type="text" value="<?php echo $$sidebox_link; ?>" /></p>
				<p><label for="<?php echo $this->get_field_id( $sidebox_content ); ?>"><?php printf(__('#%s Sidebox Content:', THEME_ADMIN_DOMAIN),$i);?></label>
				<textarea class="widefat" id="<?php echo $this->get_field_id( $sidebox_content ); ?>" name="<?php echo $this->get_field_name( $sidebox_content ); ?>"><?php echo $$sidebox_content; ?></textarea></p>
			</div>

		<?php endfor;?>
		</div>
<?php
	}
}
