<?php
/**
 * Register widgetized area and update sidebar with default widgets
 *
 * @since Morgan 1.0
 */
function morgan_widgets_init() {
	register_sidebar( array(
		'name' => __( 'Sidebar', wpfc_theme_name() ),
		'id' => 'sidebar-1',
		'before_widget' => '<aside id="%1$s" class="widget %2$s">',
		'after_widget' => '</aside>',
		'before_title' => '<h3 class="widget-title">',
		'after_title' => '</h3>',
	) );
	register_sidebar( array(
		'name' => __( 'Sermon Sidebar', wpfc_theme_name() ),
		'id' => 'sidebar-sermon',
		'before_widget' => '<aside id="%1$s" class="widget %2$s">',
		'after_widget' => '</aside>',
		'before_title' => '<h3 class="widget-title">',
		'after_title' => '</h3>',
	) );
	register_sidebar( array(
		'name' => __( 'Footer', wpfc_theme_name() ),
		'id' => 'footer-sidebar',
		'before_widget' => '<li id="%1$s" class="widget thirds %2$s">',
		'after_widget' => '</li>',
		'before_title' => '<h3 class="widget-title">',
		'after_title' => '</h3>',
	) );
}
add_action( 'widgets_init', 'morgan_widgets_init' );

/**
 * Custom Widget: Contacts
 */
class morgan_address_widget extends WP_Widget {

/**
* Widget setup.
*/
function morgan_address_widget() {
		$widget_ops = array('classname' => 'morgan-address', 'description' => __( 'This widget is used to show your contacts (based on your settings in Appearance => Theme Options)', 'morgan') );
		parent::__construct('morgan-address-widget', __('Morgan Contacts', 'morgan'), $widget_ops);
		$this->alt_option_name = 'morgan-address-widget';
}

function widget($args, $instance) {		
        extract( $args );
        $title = apply_filters('widget_title', $instance['title']);
		        
            echo $before_widget;
			if ( $title )
                echo $before_title . $title . $after_title;
				$morgan_fb = of_get_option( 'social_fb' );
				$morgan_twitter = of_get_option( 'social_twitter' );
				$morgan_gplus = of_get_option( 'social_gplus' );
				$morgan_email = of_get_option( 'social_email' );
				$morgan_phone = of_get_option( 'morgan_phone' );
				$morgan_street = of_get_option( 'morgan_street' );
				$morgan_address = of_get_option( 'morgan_address' );
				?>							

				<?php if ( $morgan_address ) : 
					$address_url = preg_replace( '![^a-z0-9]+!i', '+', $morgan_street. ' ' .$morgan_address );?>
					<p><i class="icon-map-marker"></i> <a href="<?php echo 'http://maps.google.com/maps?q=' . $address_url; ?>" title="<?php esc_attr_e( 'Directions', 'morgan' ); ?>"><?php echo $morgan_street. '<span class="city-state">' .$morgan_address. '</span>'; ?> </a></p>
				<?php endif; ?>

				<?php if ( $morgan_phone ) : ?>
					<p><i class="icon-phone"></i> <a href="tel:<?php echo $morgan_phone; ?>"><?php echo $morgan_phone; ?></a></p>
				<?php endif; ?>

				<?php if ( $morgan_email ) : ?>
					<p><i class="icon-envelope"></i> <a href="mailto:<?php echo antispambot( $morgan_email ); ?>"><?php echo $morgan_email; ?></a></p>
				<?php endif; ?>
				
				<div id="contact-social">
					<ul class="contact-social">
						<?php if ( $morgan_fb ) : ?>
							<li class="facebook"><a href="<?php echo esc_url( $morgan_fb ); ?>" title="<?php esc_attr_e( 'Facebook', 'morgan' ); ?>"><i class="icon-facebook"></i><span><?php _e( 'Facebook', 'morgan' ); ?></span></a></li>
						<?php endif; ?>

						<?php if ( $morgan_twitter ) : ?>
							<li class="twitter" ><a href="<?php echo esc_url( $morgan_twitter ); ?>" title="<?php esc_attr_e( 'Twitter', 'morgan' ); ?>"><i class="icon-twitter"></i><span><?php _e( 'Twitter', 'morgan' ); ?></span></a></li>
						<?php endif; ?>

						<?php if ( $morgan_gplus ) : ?>
							<li class="gplus"><a href="<?php echo esc_url( $morgan_gplus ); ?>" title="<?php esc_attr_e( 'Google+', 'morgan' ); ?>"><i class="icon-google-plus"></i><span><?php _e( 'Google+', 'morgan' ); ?></span></a></li>
						<?php endif; ?>
					</ul>
				</div><!-- #contact-social -->
              <?php echo $after_widget; ?>
        <?php
    }
 /** @see WP_Widget::form */
    function form($instance) {				
        $title = esc_attr($instance['title']);
	   ?>
            <p><label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:', 'morgan'); ?> <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; ?>" /></label></p>
			<p><?php _e('Set your contacts in Appearance => Theme Options', 'morgan'); ?></p>
 <?php 
    }
    /** @see WP_Widget::update */
    function update($new_instance, $old_instance) {				
	$instance = $old_instance;
	$instance['title'] = strip_tags($new_instance['title']);
	    return $instance;
    }
} 

// Add function to widgets_init that'll load our widget.
add_action( 'widgets_init', 'morgan_address_load_widgets' );

// Register our widget.
function morgan_address_load_widgets() {
register_widget( 'morgan_address_widget' );
}
?>