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

function serpentsoft_social_icons_widget_init() {
    register_widget('serpentsoft_social_icons_widget');
}

function serpentsoft_social_icons_get_icon($url_control, $class = '', $new_tab = TRUE, $toolTipPosition='top', $toolTipText=''){
    $url = serpentsoft_get_data($url_control);
    if( $url && !empty($url) ):
        $str_newTab = $new_tab ? '_blank' : '';
        return sprintf('<li><a href="%1$s" class="%2$s-metro-but" target="%3$s" rel="tooltip" data-toggle="tooltip" data-placement="%4$s" data-original-title="%5$s" title=""></a></li>', esc_url($url), $class, $str_newTab, $toolTipPosition, $toolTipText);
    endif;
}

class serpentsoft_social_icons_widget extends WP_Widget {

    function serpentsoft_social_icons_widget() {
        add_action( 'load-widgets.php', 'serpentsoft_widgets_enqueue_color_picker' );
        $widget_ops = array('classname' => 'w-social-icons', 'description' => '');
        $control_ops = array('width' => 250, 'height' => 350, 'id_base' => 'id-widget-social-icons');
        $this->WP_Widget('id-widget-social-icons', theme_name . ' - Social Icons', $widget_ops, $control_ops);
    }

    function widget($args, $instance) {
        
        extract($args);
        $title = apply_filters('widget_title', $instance['title']);

        $title = $instance['title'];
        $hide_title = $instance['hideTitle'];
        $open_newTab = $instance['openNewTab'];
        $tooltop_position = $instance['tooltipPosition'];
        
        echo $before_widget;
        
        if (!$hide_title) :
            echo $before_title;
            echo $title;
            echo $after_title;
        endif;
        
        $str_newTab = $open_newTab ? TRUE : FALSE; ?>
        
        <ul class="social-networks clearfix">

        <?php $output = '';
        
        $output .= serpentsoft_social_icons_get_icon('serpentsoft_social_facebook', 'fb', $str_newTab, $tooltop_position, 'facebook');
        $output .= serpentsoft_social_icons_get_icon('serpentsoft_social_twitter', 'twitter', $str_newTab, $tooltop_position, 'twitter');
        $output .= serpentsoft_social_icons_get_icon('serpentsoft_social_googleplus', 'googleplus', $str_newTab, $tooltop_position, 'googleplus');
        $output .= serpentsoft_social_icons_get_icon('serpentsoft_social_pinterest', 'pinterest', $str_newTab, $tooltop_position, 'pinterest');
        $output .= serpentsoft_social_icons_get_icon('serpentsoft_social_youtube', 'youtube', $str_newTab, $tooltop_position, 'youtube');
        $output .= serpentsoft_social_icons_get_icon('serpentsoft_social_linkedin', 'linkedin', $str_newTab, $tooltop_position, 'linkedin');
        
        /* RSS */
        $rss_newTab = $str_newTab ? '_blank' : '';
        if(serpentsoft_get_data('serpentsoft_social_rss_rdoOptions') == 'RSSdefault' ){
            $output .= sprintf('<li><a href="%1$s" class="rss-metro-but" target="%2$s" rel="tooltip" data-toggle="tooltip" data-placement="%3$s" data-original-title="%4$s" title=""></a></li>', get_bloginfo('rss2_url'), $rss_newTab, $tooltop_position, 'rss');
        }
        elseif( serpentsoft_get_data('serpentsoft_social_rss_rdoOptions') == 'RSScustom' ){
            $rss_custom_url = serpentsoft_get_data('serpentsoft_social_rss');
            if( !empty($rss_custom_url) ):
                $output .= sprintf('<li><a href="%1$s" class="rss-metro-but" target="%2$s" rel="tooltip" data-toggle="tooltip" data-placement="%3$s" data-original-title="%4$s" title=""></a></li>', esc_url($rss_custom_url), $rss_newTab, $tooltop_position, 'rss');
            endif;
        }
        
        $output .= serpentsoft_social_icons_get_icon('serpentsoft_social_skype', 'skype', $str_newTab, $tooltop_position, 'skype');
        $output .= serpentsoft_social_icons_get_icon('serpentsoft_social_wordpress', 'wordpress', $str_newTab, $tooltop_position, 'wordpress');
        $output .= serpentsoft_social_icons_get_icon('serpentsoft_social_dribble', 'dribble', $str_newTab, $tooltop_position, 'dribble');
        $output .= serpentsoft_social_icons_get_icon('serpentsoft_social_picasa', 'picasa', $str_newTab, $tooltop_position, 'picasa');
        $output .= serpentsoft_social_icons_get_icon('serpentsoft_social_vimeo', 'vimeo', $str_newTab, $tooltop_position, 'vimeo');
        $output .= serpentsoft_social_icons_get_icon('serpentsoft_social_envato', 'envato', $str_newTab, $tooltop_position, 'envato');
        $output .= serpentsoft_social_icons_get_icon('serpentsoft_social_myspace', 'myspace', $str_newTab, $tooltop_position, 'myspace');
        $output .= serpentsoft_social_icons_get_icon('serpentsoft_social_github', 'github', $str_newTab, $tooltop_position, 'github');
        $output .= serpentsoft_social_icons_get_icon('serpentsoft_social_outlook', 'outlook', $str_newTab, $tooltop_position, 'outlook');
        $output .= serpentsoft_social_icons_get_icon('serpentsoft_social_behance', 'behance', $str_newTab, $tooltop_position, 'behance');
        $output .= serpentsoft_social_icons_get_icon('serpentsoft_social_flattr', 'flattr', $str_newTab, $tooltop_position, 'flattr');
        
        $output .= serpentsoft_social_icons_get_icon('serpentsoft_social_google', 'google', $str_newTab, $tooltop_position, 'google');
        $output .= serpentsoft_social_icons_get_icon('serpentsoft_social_blogger', 'blogger', $str_newTab, $tooltop_position, 'blogger');
        $output .= serpentsoft_social_icons_get_icon('serpentsoft_social_reddit', 'reddit', $str_newTab, $tooltop_position, 'reddit');
        $output .= serpentsoft_social_icons_get_icon('serpentsoft_social_tumblr', 'tumblr', $str_newTab, $tooltop_position, 'tumblr');
        $output .= serpentsoft_social_icons_get_icon('serpentsoft_social_evernote', 'evernote', $str_newTab, $tooltop_position, 'evernote');
        $output .= serpentsoft_social_icons_get_icon('serpentsoft_social_apple', 'apple', $str_newTab, $tooltop_position, 'apple');
        $output .= serpentsoft_social_icons_get_icon('serpentsoft_social_lastfm', 'lastfm', $str_newTab, $tooltop_position, 'lastfm');
        $output .= serpentsoft_social_icons_get_icon('serpentsoft_social_stumbleupon', 'stumbleupon', $str_newTab, $tooltop_position, 'stumbleupon');
        $output .= serpentsoft_social_icons_get_icon('serpentsoft_social_mediatemple', 'mediatemple', $str_newTab, $tooltop_position, 'mediatemple');
        $output .= serpentsoft_social_icons_get_icon('serpentsoft_social_digg', 'digg', $str_newTab, $tooltop_position, 'digg');
        $output .= serpentsoft_social_icons_get_icon('serpentsoft_social_flickr', 'flickr', $str_newTab, $tooltop_position, 'flickr');
        $output .= serpentsoft_social_icons_get_icon('serpentsoft_social_delicious', 'delicious', $str_newTab, $tooltop_position, 'delicious');
        
        $output .= serpentsoft_social_icons_get_icon('serpentsoft_social_aboutme', 'aboutme', $str_newTab, $tooltop_position, 'aboutme');
        $output .= serpentsoft_social_icons_get_icon('serpentsoft_social_bebo', 'bebo', $str_newTab, $tooltop_position, 'bebo');
        $output .= serpentsoft_social_icons_get_icon('serpentsoft_social_fivehundredpx', 'fivehundredpx', $str_newTab, $tooltop_position, '500px');
        $output .= serpentsoft_social_icons_get_icon('serpentsoft_social_alistapart', 'alistapart', $str_newTab, $tooltop_position, 'alistapart');
        $output .= serpentsoft_social_icons_get_icon('serpentsoft_social_amazon', 'amazon', $str_newTab, $tooltop_position, 'amazon');
        $output .= serpentsoft_social_icons_get_icon('serpentsoft_social_android', 'android', $str_newTab, $tooltop_position, 'android');
        $output .= serpentsoft_social_icons_get_icon('serpentsoft_social_deviantart', 'deviantart', $str_newTab, $tooltop_position, 'deviantart');
        $output .= serpentsoft_social_icons_get_icon('serpentsoft_social_angellist', 'angellist', $str_newTab, $tooltop_position, 'angellist');
        $output .= serpentsoft_social_icons_get_icon('serpentsoft_social_forrst', 'forrst', $str_newTab, $tooltop_position, 'forrst');
        $output .= serpentsoft_social_icons_get_icon('serpentsoft_social_soundcloud', 'soundcloud', $str_newTab, $tooltop_position, 'soundcloud');
        $output .= serpentsoft_social_icons_get_icon('serpentsoft_social_appdotnet', 'appdotnet', $str_newTab, $tooltop_position, 'app.net');
        $output .= serpentsoft_social_icons_get_icon('serpentsoft_social_orkut', 'orkut', $str_newTab, $tooltop_position, 'orkut');
        $output .= serpentsoft_social_icons_get_icon('serpentsoft_social_pinboard', 'pinboard', $str_newTab, $tooltop_position, 'pinboard');
        $output .= serpentsoft_social_icons_get_icon('serpentsoft_social_newsvine', 'newsvine', $str_newTab, $tooltop_position, 'newsvine');
        
        $output .= serpentsoft_social_icons_get_icon('serpentsoft_social_rdio', 'rdio', $str_newTab, $tooltop_position, 'rdio');
        $output .= serpentsoft_social_icons_get_icon('serpentsoft_social_skydrive', 'skydrive', $str_newTab, $tooltop_position, 'skydrive');
        $output .= serpentsoft_social_icons_get_icon('serpentsoft_social_stackexchange', 'stackexchange', $str_newTab, $tooltop_position, 'stackexchange');
        $output .= serpentsoft_social_icons_get_icon('serpentsoft_social_twitch', 'twitch', $str_newTab, $tooltop_position, 'twitch');
        $output .= serpentsoft_social_icons_get_icon('serpentsoft_social_viadeo', 'viadeo', $str_newTab, $tooltop_position, 'viadeo');
        $output .= serpentsoft_social_icons_get_icon('serpentsoft_social_yahoo', 'yahoo', $str_newTab, $tooltop_position, 'yahoo');
        $output .= serpentsoft_social_icons_get_icon('serpentsoft_social_yelp', 'yelp', $str_newTab, $tooltop_position, 'yelp');
        $output .= serpentsoft_social_icons_get_icon('serpentsoft_social_drupal', 'drupal', $str_newTab, $tooltop_position, 'drupal');
        $output .= serpentsoft_social_icons_get_icon('serpentsoft_social_ebay', 'ebay', $str_newTab, $tooltop_position, 'ebay');
        $output .= serpentsoft_social_icons_get_icon('serpentsoft_social_exfm', 'exfm', $str_newTab, $tooltop_position, 'exfm');
        $output .= serpentsoft_social_icons_get_icon('serpentsoft_social_geeklist', 'geeklist', $str_newTab, $tooltop_position, 'geeklist');
        $output .= serpentsoft_social_icons_get_icon('serpentsoft_social_heroku', 'heroku', $str_newTab, $tooltop_position, 'heroku');
        $output .= serpentsoft_social_icons_get_icon('serpentsoft_social_imdb', 'imdb', $str_newTab, $tooltop_position, 'imdb');
        $output .= serpentsoft_social_icons_get_icon('serpentsoft_social_jsfiddle', 'jsfiddle', $str_newTab, $tooltop_position, 'jsfiddle');
        $output .= serpentsoft_social_icons_get_icon('serpentsoft_social_dropbox', 'dropbox', $str_newTab, $tooltop_position, 'dropbox');
        
        echo $output; ?>
        
        </ul>

<?php echo $after_widget;
    }
    
    // Update Widget Fields
    function update($new_instance, $old_instance) {
        $instance = $old_instance;
        $instance['title'] = strip_tags($new_instance['title']);
        $instance['hideTitle'] = strip_tags($new_instance['hideTitle']);
        $instance['openNewTab'] = strip_tags($new_instance['openNewTab']);
        $instance['tooltipPosition'] = strip_tags($new_instance['tooltipPosition']);
        
        return $instance;
    }

    // Draw Widget Controls
    function form($instance) {
        $defaults = array('title' => __('Social Icons', 'serpentsoft' ), 'hideTitle' => '', 'openNewTab' => 'true', 'tooltipPosition' => 'top');
        $instance = wp_parse_args((array) $instance, $defaults); ?>
        
        <p>
            <label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title', 'serpentsoft'); ?> :</label>
            <input id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" value="<?php echo $instance['title']; ?>" class="widefat" type="text" />
        </p>
        
        <p>
            <label for="<?php echo $this->get_field_id('hideTitle'); ?>"><?php _e('Hide Box Title?', 'serpentsoft'); ?></label>
            <input id="<?php echo $this->get_field_id('hideTitle'); ?>" name="<?php echo $this->get_field_name('hideTitle'); ?>"
                   value="false" <?php if ($instance['hideTitle']) { echo 'checked="checked"'; } ?> type="checkbox" />
        </p>

        <p>
            <label for="<?php echo $this->get_field_id('openNewTab'); ?>"><?php _e('Open in a new window?', 'serpentsoft'); ?></label>
            <input id="<?php echo $this->get_field_id('openNewTab'); ?>" name="<?php echo $this->get_field_name('openNewTab'); ?>"
                   value="false" <?php if ($instance['openNewTab']) { echo 'checked="checked"'; } ?> type="checkbox" />
        </p>
        
        <p>
            <label for="<?php echo $this->get_field_id('tooltipPosition'); ?>"><?php _e('Tooltip Position', 'serpentsoft'); ?>: </label>

            <select id="<?php echo $this->get_field_id('tooltipPosition'); ?>" name="<?php echo $this->get_field_name('tooltipPosition'); ?>" >
                <option value="top" <?php if($instance['tooltipPosition'] == 'top') echo "selected=\"selected\""; else echo ""; ?>>Top</option>
                <option value="right" <?php if($instance['tooltipPosition'] == 'right') echo "selected=\"selected\""; else echo ""; ?>>Right</option>
                <option value="bottom" <?php if($instance['tooltipPosition'] == 'bottom') echo "selected=\"selected\""; else echo ""; ?>>Bottom</option>
                <option value="left" <?php if($instance['tooltipPosition'] == 'left') echo "selected=\"selected\""; else echo ""; ?>>Left</option>
            </select>
        </p>
        
<?php } } ?>