<?php
/*
Plugin Name: Advanced Twitter
Plugin URI: 
Description: Pull Twitter feeds according to a set of parameters and display it in the sidebar.
Version: 1.0
Author: Jonas Palmero
Author URI: 
*/

require_once($_SERVER['DOCUMENT_ROOT'] . "/wp-content/themes/ja_buddypress/wpalchemy/MetaBox.php");

if (is_admin()) { wp_enqueue_style('custom_meta_css', get_bloginfo('stylesheet_directory') . '/metatwitter.css'); }

$custom_metabox = new WPAlchemy_MetaBox(array
(
	'id' => '_twitter_meta',
	'title' => 'Advanced Twitter Settings',
	'template' => 'advanced_twitter_meta.php',
	'priority' => 'high',
	'context' => 'side'
));


wp_enqueue_script( 'jquery_tweet', '/js/tweet/jquery.tweet.js', array('jquery') );

// $wpalchemy_media_access = new WPAlchemy_MediaAccess();

// Create advanced-twitter Widget

function widget_advanced_twitter($args) {
		global $custom_metabox;

	
			   $custom_metabox->the_field('twitter_query');
			   $query = $custom_metabox->get_the_value();
			  
			   $custom_metabox->the_field('twitter_count');
			   $count = $custom_metabox->get_the_value();

		extract($args);
		echo $before_widget;
		
	if ((is_single() || is_page()) && !empty($query)) {
		
		echo $before_title;?>Related Tweets <?php echo $after_title;

		?>
		<script type='text/javascript'>
    jQuery(document).ready(function($){
        $(".tweet").tweet({
            query: "<?php echo $query;?>",
            join_text: "auto",
            avatar_size: 32,
            count: <?php echo $count;?>,
            auto_join_text_default: "", 
            auto_join_text_ed: "we",
            loading_text: "loading tweets...",
            outro_text: "",
			//template: function(i){return i["text"]}

        });
    });
</script>
		
		<?php
		  echo '<div class="tweet"></div>';
		}
		echo $after_widget;
}

function widget_advanced_twitter_init()
{
  register_sidebar_widget(__('Advanced Twitter'), 'widget_advanced_twitter');
}

add_action("plugins_loaded", "widget_advanced_twitter_init");
	

?>