<?php

/**********************************************************************

	Register sidebars

**********************************************************************/
if (function_exists('register_sidebar'))
	register_sidebar(array(
		'name'=>'Home - Left - Sidebar',
		'before_widget' => '<li id="%1$s" class="widget %2$s">',
		'after_widget' => '</li>',
		'before_title' => '<h3>',
		'after_title' => '</h3>'
	));
	register_sidebar(array(
		'name'=>'Home - Right - Sidebar',
		'before_widget' => '<li id="%1$s" class="widget %2$s">',
		'after_widget' => '</li>',
		'before_title' => '<h3><span class="title">',
		'after_title' => '</span></h3>'
	));
	register_sidebar(array(
		'name'=>'Archives - Right - Sidebar',
		'before_widget' => '<li id="%1$s" class="widget %2$s">',
		'after_widget' => '</li>',
		'before_title' => '<h3><span class="title">',
		'after_title' => '</span></h3>'
	));
	register_sidebar(array(
		'name'=>'Post - Right - Sidebar',
		'before_widget' => '<li id="%1$s" class="widget %2$s">',
		'after_widget' => '</li>',
		'before_title' => '<h3><span class="title">',
		'after_title' => '</span></h3>'
	));
/* end */

/**********************************************************************

	Sidebar hooks

**********************************************************************/
function deluxe_sidebar(){
	do_action('deluxe_sidebar');
}

add_action('deluxe_sidebar', 'display_sidebar_widgets');

function display_sidebar_widgets(){
?>

<ul class="sidebar">

	<?php
	if (is_home()) {
		if ( function_exists('dynamic_sidebar') && dynamic_sidebar(2) ) : else : endif;
	} elseif (is_single()) {
		if ( function_exists('dynamic_sidebar') && dynamic_sidebar(4) ) : else : endif;
	} else {
		if ( function_exists('dynamic_sidebar') && dynamic_sidebar(3) ) : else : endif;
	}
	?>

</ul>

<?php
}

/**********************************************************************

	Write widgets below

**********************************************************************/

/**********************************************************************
Widget: Weather widget
**********************************************************************/
class weatherForecast extends WP_Widget {

	# Constructor
	function weatherForecast() {
		$widget_ops = array('description' => __('Display weather forecast for 3 days','news'));
		$this->WP_Widget('weatherForecast', __('NewsWorld - Weather','news'), $widget_ops);
	}
 
	# Display Widget
	function widget($args, $instance) {
		extract($args);
		
		# Instances
		$title = $instance['title'];
		$wcode = $instance['wcode'];
		$wcache = $instance['wcache'];
		$wtype = $instance['wtype'];
		
		# Display
		global $news;
		if ($wcode) {
		$weather = new weather($wcode, $wcache, $wtype); $weather->parsecached();
		echo $before_widget;
		echo '<h3><span class="title">'.$title.'</span><span class="titleright">';
		if ($weather->region == "") { print "$weather->city, $weather->country</span>"; } else { print "$weather->city, $weather->region</span>"; }
		echo '</h3>';
		?>
		<span class="wea-column">
			<span class="wedate"><?php _e('Current','news'); ?></span>
			<?php
			print "<span class=\"weimage\"><img src=\"".get_bloginfo("template_directory")."/images/weather-icons/$weather->actcode.png\" alt=\"\" width=\"52\" height=\"52\" /></span>";
			print "<span class=\"wecond\">$weather->acttext</span>";
			print "<span class=\"wetemp\">$weather->acttemp °$weather->temperature</span>";
			?>
		</span>
		<span class="wea-column">
			<span class="wedate"><?php print "$weather->fore_day1_date"; ?></span>
			<?php
			print "<span class=\"weimage\"><img src=\"".get_bloginfo("template_directory")."/images/weather-icons/$weather->fore_day1_imgcode.png\" alt=\"\" width=\"52\" height=\"52\" /></span>";
			print "<span class=\"wecond\">$weather->fore_day1_text</span>";
			print "<span class=\"welow\">Low: $weather->fore_day1_tlow °$weather->temperature</span>";
			print "<span class=\"wehigh\">High: $weather->fore_day1_thigh °$weather->temperature</span>";
			?>
		</span>
		<span class="wea-column removeRightBorder">
			<span class="wedate"><?php print " $weather->fore_day2_date"; ?></span>
			<?php
			print "<span class=\"weimage\"><img src=\"".get_bloginfo("template_directory")."/images/weather-icons/$weather->fore_day2_imgcode.png\" alt=\"\" width=\"52\" height=\"52\" /></span>";
			print "<span class=\"wecond\">$weather->fore_day2_text</span>";
			print "<span class=\"welow\">Low: $weather->fore_day2_tlow °$weather->temperature</span>";
			print "<span class=\"wehigh\">High: $weather->fore_day2_thigh °$weather->temperature</span>";
			?>
		</span><div class="clear"></div>
		<?php
		echo  $after_widget;
		}

	}
 
	# Update form
	function update($new_instance, $old_instance) {
		if (!isset($new_instance['submit'])) {
			return false;
		}
		# Instances (old = new)
		$instance = $old_instance;
		$instance['title'] = $new_instance['title'];
		$instance['wcode'] = $new_instance['wcode'];
		$instance['wcache'] = $new_instance['wcache'];
		$instance['wtype'] = $new_instance['wtype'];
		return $instance;
	}
 
	# Options form
	function form($instance) {
		global $wpdb, $news;
		# Instances
		$instance = wp_parse_args((array) $instance, array('title' => __('Weather Forecast','news'), 'wcode' => $news['wcode'], 'wcache' => 3600, 'wtype' => $news['wtype']));
		$title = $instance['title'];
		$wcode = $instance['wcode'];
		$wcache = $instance['wcache'];
		$wtype = $instance['wtype'];
?>

<p>
<small><?php _e('You can use this widget to display custom weather forecasts on your sidebar.','news'); ?></small>
</p>

<p>
<label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:','news'); ?></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('wcode'); ?>"><?php _e('Location code:','news'); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id('wcode'); ?>" name="<?php echo $this->get_field_name('wcode'); ?>" type="text" value="<?php echo $wcode; ?>" />
</p>

<p>
<small><?php _e('Go to <a href="http://weather.yahoo.com/">Yahoo! Weather</a> then enter your location. Click on the RSS feed of your location 
then read the location code from the URL in address bar. The URL of the feed looks like: http://weather.yahooapis.com/forecastrss?<strong>p=USNY0996</strong>&u=f 
in this case the <strong>p=</strong> value is your location code, which is <strong>USNY0996</strong>','news'); ?></small>
</p>

<p>
<label for="<?php echo $this->get_field_id('wcache'); ?>"><?php _e('Cache time:','news'); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id('wcache'); ?>" name="<?php echo $this->get_field_name('wcache'); ?>" type="text" value="<?php echo $wcache; ?>" />
</p>

<p>
<label for="<?php echo $this->get_field_id('wtype'); ?>"><?php _e('Temperature scale:','news'); ?></label>
<select name="<?php echo $this->get_field_name('wtype'); ?>" id="<?php echo $this->get_field_id('wtype'); ?>" class="widefat">
<option value="c"<?php selected('c', $wtype); ?>>Celsius</option>
<option value="f"<?php selected('f', $wtype); ?>>Fahrenheit</option>
</select>
</p>

<input type="hidden" id="<?php echo $this->get_field_id('submit'); ?>" name="<?php echo $this->get_field_name('submit'); ?>" value="1" />

<?php
	}
}

### Initiate widget
add_action('widgets_init', 'weatherForecast_init');
function weatherForecast_init() {
	register_widget('weatherForecast');
}

/**********************************************************************
Widget: Gallery widget
**********************************************************************/
class gallery extends WP_Widget {

	# Constructor
	function gallery() {
		$widget_ops = array('description' => __('Display posts as a gallery in a widget.','news'));
		$this->WP_Widget('gallery', __('NewsWorld - Gallery','news'), $widget_ops);
	}
 
	# Display Widget
	function widget($args, $instance) {
		extract($args);
		
		# Instances
		$title = $instance['title'];
		$postsnum = $instance['postsnum'];
		$custom_key = $instance['custom_key'];
		$custom_url = $instance['custom_url'];
		$custom_title = $instance['custom_title'];
		$type = $instance['type'];
		
		# Display
		global $news;
		$display = new WP_query(); $display->query('showposts='.$postsnum.'&meta_key='.$custom_key); global $wp_query; $wp_query->in_the_loop = true; if ($display->have_posts()) :
		echo $before_widget;
		echo '<h3><span class="title">'.$title.'</span>';
		if ($custom_url && $custom_title) {
		echo '<span class="titleright"><a href="'.$custom_url.'" class="rounded-regular">'.$custom_title.'</a></span>';
		}
		echo '</h3>';
		?>
		<div class="gallery-wrap">
			
			<span class="gallery-items">
				<?php while ($display->have_posts()) : $display->the_post(); ?>
				<a href="#"><?php echo $post->ID; ?></a>
				<?php endwhile; ?>
			</span>
			
			<a href="#forward" class="nextgal"></a>
			<a href="#backward" class="prevgal"></a>
			
			<div class="gallery">
			<?php while ($display->have_posts()) : $display->the_post(); ?>
			<?php dt_thumb('width=224&height=126&link='.$type); ?>
			<?php endwhile; ?>
			<div class="clear"></div></div>
			
		</div>
		<?php
		echo $after_widget;
		endif;

	}
 
	# Update form
	function update($new_instance, $old_instance) {
		if (!isset($new_instance['submit'])) {
			return false;
		}
		# Instances (old = new)
		$instance = $old_instance;
		$instance['title'] = $new_instance['title'];
		$instance['postsnum'] = $new_instance['postsnum'];
		$instance['custom_key'] = $new_instance['custom_key'];
		$instance['custom_url'] = $new_instance['custom_url'];
		$instance['custom_title'] = $new_instance['custom_title'];
		$instance['type'] = $new_instance['type'];
		return $instance;
	}
 
	# Options form
	function form($instance) {
		global $wpdb, $news;
		# Instances
		$instance = wp_parse_args((array) $instance, array('title' => __('Gallery','news'), 'postsnum' => 10, 'custom_key' => 'slideshow', 'custom_url' => '', 'custom_title' => __('View all','news'), 'type' => 'overlay'));
		$title = $instance['title'];
		$postsnum = $instance['postsnum'];
		$custom_key = $instance['custom_key'];
		$custom_url = $instance['custom_url'];
		$custom_title = $instance['custom_title'];
		$type = $instance['type'];
?>

<p>
<small><?php _e('You can use this widget to display custom weather forecasts on your sidebar.','news'); ?></small>
</p>

<p>
<label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:','news'); ?></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('postsnum'); ?>"><?php _e('Number of posts to rotate:','news'); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id('postsnum'); ?>" name="<?php echo $this->get_field_name('postsnum'); ?>" type="text" value="<?php echo $postsnum; ?>" />
</p>

<p>
<label for="<?php echo $this->get_field_id('type'); ?>"><?php _e('Gallery image link:','news'); ?></label>
<select name="<?php echo $this->get_field_name('type'); ?>" id="<?php echo $this->get_field_id('type'); ?>" class="widefat">
<option value="overlay"<?php selected('overlay', $type); ?>>Overlay</option>
<option value="image"<?php selected('image', $type); ?>>Zoom</option>
<option value=""<?php selected('', $type); ?>>Link to post only</option>
</select>
</p>

<p>
<label for="<?php echo $this->get_field_id('custom_key'); ?>"><?php _e('Custom key:','news'); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id('custom_key'); ?>" name="<?php echo $this->get_field_name('custom_key'); ?>" type="text" value="<?php echo $custom_key; ?>" />
</p>

<p>
<small><?php _e('Do not change this value unless you want a new set of posts to be rotated in this gallery widget. If you change the custom key, you must make sure that you have 
posts with which have this custom key/field already','news'); ?></small>
</p>

<p>
<label for="<?php echo $this->get_field_id('custom_url'); ?>"><?php _e('Link to:','news'); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id('custom_url'); ?>" name="<?php echo $this->get_field_name('custom_url'); ?>" type="text" value="<?php echo $custom_url; ?>" />
</p>

<p>
<small><?php _e('If you want to display a button that links to archive or a link please include the full URL address here.','news'); ?></small>
</p>

<p>
<label for="<?php echo $this->get_field_id('custom_title'); ?>"><?php _e('Link to attribute:','news'); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id('custom_title'); ?>" name="<?php echo $this->get_field_name('custom_title'); ?>" type="text" value="<?php echo $custom_title; ?>" />
</p>

<input type="hidden" id="<?php echo $this->get_field_id('submit'); ?>" name="<?php echo $this->get_field_name('submit'); ?>" value="1" />

<?php
	}
}

### Initiate widget
add_action('widgets_init', 'gallery_init');
function gallery_init() {
	register_widget('gallery');
}

/**********************************************************************
Widget: Advertisement widget in sidebar
**********************************************************************/
class ads extends WP_Widget {

	# Constructor
	function ads() {
		$widget_ops = array('description' => __('Display configured advertisements (This is for right sidebar only)','news'));
		$this->WP_Widget('ads', __('NewsWorld - Advertisement','news'), $widget_ops);
	}
 
	# Display Widget
	function widget($args, $instance) {
		extract($args);
		
		# Instances
		
		# Display
		global $news;
		dt_show_ads('position=Right&before=<li class="clean">&after=</li>');

	}
 
	# Update form
	function update($new_instance, $old_instance) {
		if (!isset($new_instance['submit'])) {
			return false;
		}
		# Instances (old = new)
		$instance = $old_instance;
		return $instance;
	}
 
	# Options form
	function form($instance) {
		global $wpdb, $news;
		# Instances
		$instance = wp_parse_args((array) $instance, array());
?>

<p>
<small><?php _e('Advertisements should be configured in theme options page. Use this widget to add an advertisement based on its position.','news'); ?></small>
</p>

<input type="hidden" id="<?php echo $this->get_field_id('submit'); ?>" name="<?php echo $this->get_field_name('submit'); ?>" value="1" />

<?php
	}
}

### Initiate widget
add_action('widgets_init', 'ads_init');
function ads_init() {
	register_widget('ads');
}

/**********************************************************************
Widget: Advertisement widget in sidebar
**********************************************************************/
class ads_c extends WP_Widget {

	# Constructor
	function ads_c() {
		$widget_ops = array('description' => __('Display configured advertisements (This is for left sidebar only)','news'));
		$this->WP_Widget('ads_c', __('NewsWorld - Advertisement','news'), $widget_ops);
	}
 
	# Display Widget
	function widget($args, $instance) {
		extract($args);
		
		# Instances
		
		# Display
		global $news;
		dt_show_ads('position=Left&before=<li><div class="adside">&after=</div><div class="clear"></div></li>');

	}
 
	# Update form
	function update($new_instance, $old_instance) {
		if (!isset($new_instance['submit'])) {
			return false;
		}
		# Instances (old = new)
		$instance = $old_instance;
		return $instance;
	}
 
	# Options form
	function form($instance) {
		global $wpdb, $news;
		# Instances
		$instance = wp_parse_args((array) $instance, array());
?>

<p>
<small><?php _e('Advertisements should be configured in theme options page. Use this widget to add an advertisement based on its position.','news'); ?></small>
</p>

<input type="hidden" id="<?php echo $this->get_field_id('submit'); ?>" name="<?php echo $this->get_field_name('submit'); ?>" value="1" />

<?php
	}
}

### Initiate widget
add_action('widgets_init', 'ads_c_init');
function ads_c_init() {
	register_widget('ads_c');
}

?>