/******************************************************************
 * News Manager
 ******************************************************************/
dojo.provide("xng.plumbbManager");
dojo.declare("xng.plumbbManager", null, {

	// ----- Attributes -----
	root_url: "",
	plumb_url: "http://www.plumb.com",
	error_load_message: "Error!",
	loading_message: '<div class="loading"><img src="http://xng.dyndns.biz/aligned/site/images/loading.gif"/></div>',
	current_post: 0,
	current_feed: 0,
	id_blog: 0,
	id_widget: 0,
	
	post_text:"",
	
	// ----- Methods -----
	// Constructor
	initialize: function(url, id_blog, id_widget)
	{
		// Put setters here like: this.name=name;
		this.root_url = url;
		this.loading_message = '<div class="loading"><img src="' + this.root_url + '/images/loading.gif"/></div>';
		this.current_post = 0;
		this.id_widget = id_widget;
		this.id_blog = id_blog;
	},

	//---------------------------------------------------------------------------------------------
	loadMostBuzz: function(page)
	{
		dojo.io.bind({
			sync: false,
			url: this.root_url + '/include/plumbb.load.php',
			handler: this.loadMostBuzzCallback,
			content:{
				HID_Load: "MostBuzz",
				ID_Blog: this.id_blog,
				ID_Widget: this.id_widget,
				page: page
			},
			error: function(type, error){
				document.getElementById('plumbb_load').innerHTML = this.error_load_message;
			}
		});
	},

	loadMostBuzzCallback: function(type, data, evt)
	{
		if (type == 'error') {
			document.getElementById('plumbb_load').innerHTML = plumbbManager.error_load_message;
		} else {
			var posts = JSON.parse(data);
			plumbbManager.createMostBuzzPostsContentDivs(posts);
			var info = posts[posts.length-1];//Pagination info
			//plumbbManager.showPostsPaging('all', info);
		}
	},
	
	//---------------------------------------------------------------------------------------------
	loadMostPopular: function(page)
	{
		dojo.io.bind({
			sync: false,
			url: this.root_url + '/include/plumbb.load.php',
			handler: this.loadMostPopularCallback,
			content:{
				HID_Load: "MostPopular",
				ID_Blog: this.id_blog,
				ID_Widget: this.id_widget,
				page: page
			},
			error: function(type, error){
				document.getElementById('plumbb_load').innerHTML = this.error_load_message;
			}
		});
	},

	loadMostPopularCallback: function(type, data, evt)
	{
		if (type == 'error') {
			document.getElementById('plumbb_load').innerHTML = plumbbManager.error_load_message;
		} else {
			var posts = JSON.parse(data);
			plumbbManager.createMostPopularContentDivs(posts);
			var info = posts[posts.length-1];//Pagination info
			//plumbbManager.showPostsPaging('most_popular', info);
		}
	},

	//---------------------------------------------------------------------------------------------
	loadPost: function(id_post, id_item, prefix)
	{
		
		document.getElementById(prefix + '_post_'+id_post+'_content').innerHTML = this.loading_message; //show loading image
		dojo.io.bind({
			sync: false,
			url: this.root_url + '/include/plumbb.load.php',
			handler: function(type, data, evt){
					 	plumbbManager.loadPostCallback(type, data, evt, id_post, prefix); 
			},
			content: {
				HID_Load: "Post",
				ID_Blog: this.id_blog,
				ID_Widget: this.id_widget,
				ID_Post: id_post,
				ID_Feed: id_item
			},
			error: function(type, error){
				document.getElementById('plumbb_load').innerHTML = this.error_load_message;
			}
		});
	},
	
	loadPostCallback: function(type, data, evt, post_id, prefix)
	{
		if (type == 'error') {
			document.getElementById('plumbb_load').innerHTML = plumbbManager.error_load_message;
		} else {
			var post = JSON.parse(data);
			document.getElementById(prefix + '_post_'+post_id+'_content').innerHTML = post[0].content;
			
			//Add link to original site
			var html_link = '<div class="post_link"><a href="'+post[0].url+'" rel="nofollow" target="_blank"> read the original post &gt;&gt; </a></div>';
			document.getElementById(prefix + '_post_'+post_id+'_content').innerHTML = document.getElementById(prefix + '_post_'+post_id+'_content').innerHTML + html_link;
			//document.getElementById('titleAllPosts').setAttribute(,post[0].content);
		}
	},
	//---------------------------------------------------------------------------------------------
	loadFeeds: function()
	{
		dojo.io.bind({
			sync: false,
			url: this.root_url + '/include/plumbb.load.php',
			handler: this.loadFeedsCallback,
			content: {
				HID_Load: "Feeds",
				ID_Blog: plumbbManager.id_blog,
				ID_Widget: plumbbManager.id_widget
			},
			error: function(type, error){
				document.getElementById('plumbb_load').innerHTML = this.error_load_message;
			}
		});
	},
	
	loadFeedsCallback: function(type, data, evt )
	{
		if (type == 'error') {
			document.getElementById('plumbb_load').innerHTML = plumbbManager.error_load_message;
		} else {
			var feeds = JSON.parse(data);
			plumbbManager.createUncategorizedContentDivs(feeds);
			//Load first feeds posts
			if( (feeds.length-1) > 1){
				plumbbManager.feedMore(feeds[0]['id']);
				//plumbbManager.current_item = feeds[0]['id'];
			}
		}
	},
	
	//---------------------------------------------------------------------------------------------
	loadFeedPost: function(id_item)
	{
		dojo.io.bind({
			sync: false,
			url: this.root_url + '/include/plumbb.load.php',
			handler: this.loadFeedsCallback,
			content: {
				HID_Load: "FeedPost",
				ID_Blog: plumbbManager.id_blog,
				ID_Widget: plumbbManager.id_widget,
				ID_Feed: this.id_item
			},
			error: function(type, error){
				document.getElementById('plumbb_load').innerHTML = this.error_load_message;
			}
		});
	},
	
	loadFeedPostCallback: function(type, data, evt )
	{
		if (type == 'error') {
			document.getElementById('plumbb_load').innerHTML = plumbbManager.error_load_message;
		} else {
			alert(data);
		}
	},
	
	loadFeedPosts: function(id_item)
	{
		plumbbManager.current_feed = id_item;
		dojo.io.bind({
			sync: false,
			url: this.root_url + '/include/plumbb.load.php',
			handler: this.loadFeedPostsCallback,
			content: {
				HID_Load: "FeedPosts",
				ID_Blog: plumbbManager.id_blog,
				ID_Widget: plumbbManager.id_widget,
				ID_Feed: id_item
			},
			error: function(type, error){
				document.getElementById('plumbb_load').innerHTML = this.error_load_message;
			}
		});
	},
	
	//---------------------------------------------------------------------------------------------
	// Create the posts titles 
	loadFeedPostsCallback:function(type, data, evt )
	{
		if (type == 'error') {
			document.getElementById('plumbb_load').innerHTML = plumbbManager.error_load_message;
		} else {
			var posts_html = "";
			
			var posts = JSON.parse(data);
			
			for(i=0; i< posts.length-1; i++){
				var post =  posts[i];
				
				var post_date = '<div class="date">' + post['pubdate'] + '</div>';
				
				var post_more_link = '<a class="more_link" id="feed_more_link_'+post['id']+'" onclick="plumbbManager.postMore('+post['id']+','+plumbbManager.current_feed+',\'feed\');" style="display:block;">more&gt;&gt;</a>';
			
				var post_less_link = '<a class="more_link" id="feed_less_link_'+post['id']+'" onclick="plumbbManager.postLess('+post['id']+',\'feed\');" style="display:none;">&lt;&lt;less</a>';
			
				// Check Particular style to first post title
				var post_title_class = "post_title";
				if(i==0) post_title_class = "post_title_first"; // era post
				posts_html = posts_html + '<div class="'+post_title_class+'">'+post_date+'<div class="title_news" onclick="plumbbManager.postMore('+post['id']+','+plumbbManager.current_feed+',\'feed\');">' + post['name'] + '</div>'+post_more_link + post_less_link +'<div class="post_content" id="feed_post_'+post['id']+'_content" style="display:none;"></div></div>';
			}		
			document.getElementById('feed_posts').innerHTML = posts_html;
		}
	},
	
	postMore:function(id_post, id_item, prefix)
	{
		document.getElementById(prefix+'_post_'+id_post+'_content').innerHTML = this.loading_message;
		show_div(prefix+'_post_'+id_post+'_content');
		
		this.loadPost(id_post, id_item, prefix);
		hide_div(prefix + '_more_link_' + id_post );
		show_div(prefix + '_less_link_' + id_post);
	},
	
	postLess:function(id_post, prefix)
	{
		hide_div(prefix+'_post_'+id_post+'_content');
		hide_div(prefix + '_less_link_' + id_post ); 
		document.getElementById(prefix + '_post_' + id_post + '_content').innerHTML = "&nbsp;"; 
		show_div(prefix + '_more_link_' + id_post);
	},
	
	feedMore:function(id_item)
	{
		// Unselect previos post title
		if(document.getElementById('feed_post_'+this.current_item+'_title')){
			document.getElementById('feed_post_'+this.current_item+'_title').style.textDecoration ="none";
		}
		// Select the current post title
		document.getElementById('feed_post_'+id_item+'_title').style.textDecoration ="underline";
		
		this.current_item = id_item;
		document.getElementById('feed_posts').innerHTML = plumbbManager.loading_message;		
		this.loadFeedPosts(id_item);
	},
	
	
	createMostBuzzPostsContentDivs:function(posts)
	{
		
		var posts_html = "";
		var i = 0;
		document.getElementById('all_posts').innerHTML = "";
		for(i=0; i< posts.length-1; i++){
			var post =  posts[i];
			
			//var resume = this.getPostPartialText(post['id'],post['itemid'], post['name']);
			
			var post_date = '<div class="date">' + post['pubdate'] + '</div>';
			
			//var post_title = '<div class="title_news" style="cursor:pointer;" onclick="plumbbManager.loadPost('+post['id']+','+post['itemid']+',\'mb\');">' + post['name'] + ' - ' + resume + '</div>';
			
			var post_title = '<div class="title_news" style="cursor:pointer;" onclick="plumbbManager.loadPost('+post['id']+','+post['itemid']+',\'mb\');">' + post['name'] + '</div>';
			
			var post_more_link = '<a class="more_link" id="mb_more_link_'+post['id']+'" onclick="plumbbManager.postMore('+post['id']+','+post['itemid']+',\'mb\');" style="display:block;">more&gt;&gt;</a>';
			
			var post_less_link = '<a class="more_link" id="mb_less_link_'+post['id']+'" onclick="plumbbManager.postLess('+post['id']+',\'mb\');" style="display:none;">&lt;&lt;less</a>';
			
			// Check Particular style to first post title
			var post_title_class = "post_title";
			if(i==0) post_title_class = "post_title_first";
			posts_html = posts_html + '<div id="mb_post_'+post['id']+'_title" class="'+post_title_class+'" > ' +	post_date + post_title + post_more_link + post_less_link + '</div>';
			
			posts_html = posts_html + '<div id="mb_post_'+post['id']+'_content" class="post_content"  style="display:none;">&nbsp;</div>';
			//posts_html = posts_html + '<p class="line_news">&nbsp;</p>';
			document.getElementById('all_posts').innerHTML = document.getElementById('all_posts').innerHTML + posts_html;
			posts_html = "";
		}
		//document.getElementById('all_posts').innerHTML = posts_html;
	
	},
	
	
	createMostPopularContentDivs:function(posts)
	{
		var posts_html = "";
		var i = 0;
		
		for(i=0; i< posts.length-1; i++){
			var post =  posts[i];
			//var resume = this.getPostPartialText(post['id'],post['itemid'], post['name']);
			
			var post_date = '<div class="date">' + post['pubdate'] + '</div>';
			var post_title = '<div class="title_news" style="cursor:pointer;" onclick="plumbbManager.loadPost('+post['id']+','+post['itemid']+',\'mp\');">' + post['name'] + '</div>';
			var post_more_link = '<a class="more_link" id="mp_more_link_'+post['id']+'" onclick="plumbbManager.postMore('+post['id']+','+post['itemid']+',\'mp\');" style="display:block;">more&gt;&gt;</a>';
			
			var post_less_link = '<a class="more_link" id="mp_less_link_'+post['id']+'" onclick="plumbbManager.postLess('+post['id']+',\'mp\');" style="display:none;">&lt;&lt;less</a>';
			
			// Check Particular style to first post title
			var post_title_class = "post_title";
			if(i==0) post_title_class = "post_title_first";
			posts_html = posts_html + '<div id="mp_post_'+post['id']+'_title" class="'+post_title_class+'" > ' + post_date + post_title + post_more_link + post_less_link + '</div>';
			
			posts_html = posts_html + '<div id="mp_post_'+post['id']+'_content" class="post_content"  style="display:none;">&nbsp;</div>';
			//posts_html = posts_html + '<p class="line_news">&nbsp;</p>';
		}
		document.getElementById('most_popular_posts').innerHTML = posts_html;
	},
	
	createUncategorizedContentDivs:function(feeds)
	{
		var feeds_html_titles = "";
		var feeds_html_content = "";
		
		var i = 0;
		for(i=0; i< feeds.length-1; i++){
			var feed =  feeds[i];
			
			var feed_title = '<div class="title_feed" style="cursor:pointer;" onclick="plumbbManager.feedMore('+feed['id']+');">' + feed['name'] + '</div>';
						
			// Check Particular style to first post title
			var post_title_class = "post_title";
			if(i==0) post_title_class = "post_title_first";
			feeds_html_titles = feeds_html_titles + '<li><div id="feed_post_'+feed['id']+'_title" class="'+post_title_class+'" > ' + feed_title + '</div></li>';
			
			feeds_html_content = feeds_html_content + '<div id="feed_'+feed['id']+'_content" class="feed_titles" >&nbsp;</div>';
			//posts_html = posts_html + '<p class="line_news">&nbsp;</p>';
		}
		document.getElementById('feeds').innerHTML = '<ul class="feed_titles">' + feeds_html_titles  + '</ul>';
	},
	
	changeTab:function(tab_name)
	{
		switch(tab_name){
			case "AllPosts":
				// change styles
				document.getElementById('titleAllPosts').setAttribute("class","tabTitle_left");
				document.getElementById('titleMostPopularPosts').setAttribute("class","tabTitle_center_off");
				document.getElementById('titleFeeds').setAttribute("class","tabTitle_rigth_off");
				// for IE
				document.getElementById('titleAllPosts').setAttribute("className","tabTitle_left");
				document.getElementById('titleMostPopularPosts').setAttribute("className","tabTitle_center_off");
				document.getElementById('titleFeeds').setAttribute("className","tabTitle_rigth_off");
				// hide and show tabs
				hide_div('tabMostPopularPosts');
				hide_div('tabFeeds');
				show_div('tabAllPosts');
				break;
			
			case "MostPopularPosts":
				// change styles
				document.getElementById('titleMostPopularPosts').setAttribute("class","tabTitle_center");
				document.getElementById('titleAllPosts').setAttribute("class","tabTitle_left_off");
				document.getElementById('titleFeeds').setAttribute("class","tabTitle_rigth_off");
				// For IE
				document.getElementById('titleMostPopularPosts').setAttribute("className","tabTitle_center");
				document.getElementById('titleAllPosts').setAttribute("className","tabTitle_left_off");
				document.getElementById('titleFeeds').setAttribute("className","tabTitle_rigth_off");
				// hide and show tabs
				hide_div('tabAllPosts');
				hide_div('tabFeeds');
				show_div('tabMostPopularPosts');
				
				break;
				
			case "Feeds":
				// change styles
				document.getElementById('titleFeeds').setAttribute("class","tabTitle_rigth");
				document.getElementById('titleAllPosts').setAttribute("class","tabTitle_left_off");
				document.getElementById('titleMostPopularPosts').setAttribute("class","tabTitle_center_off");
				// for IE
				document.getElementById('titleFeeds').setAttribute("className","tabTitle_rigth");
				document.getElementById('titleAllPosts').setAttribute("className","tabTitle_left_off");
				document.getElementById('titleMostPopularPosts').setAttribute("className","tabTitle_center_off");
				// hide and show tabs
				hide_div('tabAllPosts');
				hide_div('tabMostPopularPosts');
				show_div('tabFeeds');
				// change styles
				document.getElementById('feed_posts').removeAttribute('minHeight');
				break;
		}
	},
	
	showPostsPaging: function(prefix, info)
	{
		// First hidde all 
		document.getElementById(prefix + '_posts_paging').style.display = "none";
		document.getElementById(prefix + '_posts_paging_back').style.display = "none";
		document.getElementById(prefix + '_posts_paging_forward').style.display = "none";
		// Now show  
		if( info['back']==true || info['forward']==true ){
			document.getElementById(prefix + '_posts_paging').style.display = "block";
			if( info['back']==true ){
				document.getElementById(prefix + '_posts_paging_back').style.display = "block";
			}
			if( info['forward']==true ){
				document.getElementById(prefix + '_posts_paging_forward').style.display = "block";
			}
		}
	},
	
	getPostPartialText: function(id_post, id_item, name)
	{
		this.loadPost2(id_post, id_item, name);
		return plumbbManager.post_text;
	},
	
	loadPost2: function(id_post, id_item, name)
	{
		dojo.io.bind({
			sync: true,
			url: this.root_url + '/include/plumbb.load.php',
			handler: function(type, data, evt){
					 	plumbbManager.loadPostCallback2(type, data, evt, name); 
			},
			content: {
				HID_Load: "Post",
				ID_Blog: this.id_blog,
				ID_Widget: this.id_widget,
				ID_Post: id_post,
				ID_Feed: id_item
			},
			error: function(type, error){
				document.getElementById('plumbb_load').innerHTML = this.error_load_message;
			}
		});
	},
	
	loadPostCallback2: function(type, data, evt, name)
	{
		if (type == 'error') {
			//document.getElementById('plumbb_load').innerHTML = plumbbManager.error_load_message;
		} else {
			var post = JSON.parse(data);
			var content = post[0].content;
			//var partial_text = content.substr(0,150);
			//plumbbManager.post_text = partial_text;
			plumbbManager.loadPartialText(content, name);
		}
	},
	
	loadPartialText: function(text, name) //to get formated partial text using php functions
	{
		dojo.io.bind({
			sync: true,
			url: this.root_url + '/include/plumbb.load.php',
			handler: this.loadPartialTextCallback, 
			content: {
				HID_Load: "ExtractPostText",
				post_title: name,
				post_text: text
			},
			error: function(type, error){
				document.getElementById('plumbb_load').innerHTML = this.error_load_message;
			}
		});
	},
	
	loadPartialTextCallback: function(type, data, evt)
	{
		if (type == 'error') {
			//document.getElementById('plumbb_load').innerHTML = plumbbManager.error_load_message;
		} else {
			plumbbManager.post_text = data;
		}
	}
	
});


