// JavaScript Document
// JavaScript NewsFront class

//function Pet(name) {
//    this._name = name;
//}
//
//Pet.prototype._name;
//
//Pet.prototype.getName = function() {
//    return this._name;
//}
//
//var p = new Pet("Max");
//alert(p.getName());

var NewsFront={
	
	url:'/moodul.php?moodul=CMS&Komponent=NewsFront&notemplate=1',
	newsMSurl:'/moodul.php?moodul=CMS&Komponent=NewsMS&notemplate=1',
	listcontolID:0,
	list_container:0,
	delete_comment_str:'Delete comment',
	
	//NewsFront_main.tpl
	//show news cfg form
	ProcessShowNewsCfgFormXupd:function(id)
	{
		$('news_front_msg').hide();//hide main.tpl msg if any	
		
		var idStr = '';
		if(id>0)
		idStr = '&id='+id;
		
		var XgetEdit_url = this.url+'&f=getEditAX'+idStr;//encodeURIComponent('http://www.google.com/search?q=Prototype');
		
		this.updateIndicatorShow('news_front_cfg_container');
		
		new Ajax.Request(XgetEdit_url, {
		  method: 'get',
		  asynchronous:true, 
		  evalScripts:true,		  
		  onSuccess: function(transport) {
			var container = $('news_front_cfg_container');
			container.update('<br />'+transport.responseText);
			new Effect.Appear('news_front_cfg_container' );			
		  }
		});
	},
	
	//NewsFront_new.tpl
	//saves a new topic
	editNewsFrontCfgXupd:function()
	{
		$('news_front_msg').hide();//hide main.tpl msg if any	
		
		var Xedit_url = this.url+'&f=edit'; 	
		
		var newsTopObj = this;
				
		if($('news_front_cfg_form') && $('news_front_cfg_form') != 'undefined')
		{
			var form_str = $('news_front_cfg_form').serialize();
			//check descr number
			//if( $('news_front_cfg_descr_n').value > $('news_front_cfg_per_page').value)
			//$('news_front_cfg_descr_n').value = $('news_front_cfg_per_page').value;
			
			new Ajax.Request(Xedit_url, {
			  method: 'post',
			  postBody:form_str,
			  asynchronous:true, 
			  evalScripts:true,		  
			  onSuccess: function(transport) {
				
				//alert(transport.responseText);
				//var json = transport.responseText.evalJSON();
				var json=eval('(' + transport.responseText + ')');
				//manage news cfg general message
				if(json.msg_news_front_cfg && json.msg_news_front_cfg != 'undefined' && json.msg_news_front_cfg != "" )
				{				
					var notice = $('msg_news_front_cfg');
					notice.update(json.msg_news_front_cfg);	
					notice.show();
					
					$('news_front_msg').hide();//hide main.tpl msg if any					
				}
				
				//manage msg_news_front_cfg_per_page message
				if(json.msg_news_front_cfg_per_page != 'undefined' && json.msg_news_front_cfg_per_page != "")
				{
					var msg_news_front_cfg_per_page = $('msg_news_front_cfg_per_page');
					msg_news_front_cfg_per_page.update(json.msg_news_front_cfg_per_page);
					msg_news_front_cfg_per_page.show();
					$('news_front_msg').hide();//hide main.tpl msg if any	
				}
				
				//manage msg_news_front_cfg_descr_n message
				if(json.msg_news_front_cfg_descr_n != 'undefined' && json.msg_news_front_cfg_descr_n != "")
				{
					var msg_news_front_cfg_descr_n = $('msg_news_front_cfg_descr_n');
					msg_news_front_cfg_descr_n.update(json.msg_news_front_cfg_descr_n);
					msg_news_front_cfg_descr_n.show();
					$('news_front_msg').hide();//hide main.tpl msg if any	
				}
				
				//manage news_front_cfg_descr_n exception
				if(json.news_front_cfg_descr_n != 'undefined' && json.news_front_cfg_descr_n != "")
				{
					var news_front_cfg_descr_n = $('news_front_cfg_descr_n');
					news_front_cfg_descr_n.value = json.news_front_cfg_descr_n;
				}
				
				//manage msg_news_front_show_n_days message
				if(json.msg_news_front_show_n_days != 'undefined' && json.msg_news_front_show_n_days != "")
				{
					var msg_news_front_show_n_days = $('msg_news_front_show_n_days');
					msg_news_front_show_n_days.update(json.msg_news_front_show_n_days);
					msg_news_front_show_n_days.show();
					$('news_front_msg').hide();//hide main.tpl msg if any	
				}
				//manage msg_news_front_show_mininews message
				if(json.msg_news_front_show_mininews != 'undefined' && json.msg_news_front_show_mininews != "")
				{
					var msg_news_front_show_mininews = $('msg_news_front_show_mininews');
					msg_news_front_show_mininews.update(json.msg_news_front_show_mininews);
					msg_news_front_show_mininews.show();
					$('news_front_msg').hide();//hide main.tpl msg if any	
				}
			  }
			});
		}
	},
	
	//add comments support
	addNewsComment:function(news_id,update_container){		
		
		if(news_id > 0)
		{
			var X_url = this.url+'&f=addCommentAX'+'&news='+news_id; 	
			
			var newsTopObj = this;
			$('news_comments_indicator_'+news_id).update('');
			newsTopObj.updateIndicatorShow('news_comments_indicator_'+news_id);
			//$('news_comments_indicator_'+news_id).show(); 
			$('add_news_comment_msg_'+news_id).hide();
			$('add_news_comment_error_msg_'+news_id).hide();
			$('msg_news_comment_body_'+news_id).update("");
			$(update_container).show(); 
			
			var comments_form = 'news_comments_form_'+news_id;
			
			if($(comments_form) && $(comments_form) != 'undefined')
			{
				var form_str = $(comments_form).serialize();				
				
				new Ajax.Request(X_url, {
				  method: 'post',
				  postBody:form_str,
				  asynchronous:true, 
				  evalScripts:true,		  
				  onSuccess: function(transport) {					
						$('news_comments_indicator_'+news_id).update('');
						//alert(transport.responseText);
						var json=eval('(' + transport.responseText + ')');						
						
						if(json.msg_news_comment_body)
						{
							$('msg_news_comment_body_'+news_id).update(json.msg_news_comment_body);							
						}						
						
						if(json.add_news_comment_error_msg)
						{
							$('add_news_comment_error_msg_'+news_id).update(json.add_news_comment_error_msg);
							$('add_news_comment_error_msg_'+news_id).show();
						}						
						
						if(json.add_news_comment_msg )
						{							
							$('add_news_comment_msg_'+news_id).update(json.add_news_comment_msg);
							$('add_news_comment_msg_'+news_id).show();
						}	
						
						if(json.news_comment_str_captchacode)
						{
							$('news_comment_str_captchacode_'+news_id).update(json.news_comment_str_captchacode);
							$('news_comment_captchacode_'+news_id).value = '';
						}
						else
						$('news_comment_str_captchacode_'+news_id).update('');
						
						if(json.news_comment_saved)
						{							
							//generate comment HTML
							var new_commentHTML = 
							'<div style="margin-left:10px;" id="news_comment_'+news_id+'_'+json.news_comment_saved+'">'+
								'<p style="margin:0px; font-size:10px;">'+json.added_date+' - '+json.news_comment_author+' '+(json.author_id != 'NULL' && json.author_id > 0 ?
								'<a href="javascript:void(0);" '+
									'onclick="NewsFront.deleteNewsComment('+news_id+','+json.news_comment_saved+',\'news_comments_container_'+news_id+'\');" '+
									'title="'+newsTopObj.delete_comment_str+'">'+newsTopObj.delete_comment_str : '' )+
								'</a>'+								
								'<span id="msg_news_comment_'+news_id+'_'+json.news_comment_saved+'" class="error"></span>'+
								'</p>'+
								'<p style="margin:0px; padding-top:5px; padding-left:5px;">'+json.add_news_comment_body+'</p>'+
								'<hr style="height:1px; width:99%" color="#E8E8E8" noshade="noshade" />'+
							'</div>';
							
							new Insertion.Top($(update_container), new_commentHTML);
							
							if(json.total_comments)
							{
								if(json.total_comments == 'zero')
								json.total_comments = '0';
								
								//$('total_comments'+news_id).update(json.total_comments);
								$('total_comments_news'+news_id).update(json.total_comments);
								
								if($('total_comments_mininews'+news_id))
								$('total_comments_mininews'+news_id).update(json.total_comments);								
							}
							
							//reset form					
							$(comments_form).reset();
							
							changeCaptchaNewsComments(news_id);
						}						
					}
				});
			}			
		}		
	},
	
	//add comments support
	deleteNewsComment:function(news_id,comment_id,update_container){		
		
		if(news_id > 0 && comment_id > 0)
		{
			var X_url = this.url+'&f=deleteCommentAX'+'&news='+news_id+'&comment='+comment_id; 	
			
			var newsTopObj = this;			
			
			newsTopObj.updateIndicatorShow('msg_news_comment_'+news_id+'_'+comment_id);
			
			new Ajax.Request(X_url, {
				  method: 'get',
				  asynchronous:true, 
				  evalScripts:true,		  
				  onSuccess: function(transport) {						
						//alert(transport.responseText);
						var json=eval('(' + transport.responseText + ')');
						
						if(json.delete_news_comment_msg)
						{
							alert(json.delete_news_comment_msg);	
						}
						
						if(json.comment_deleted && json.comment_deleted == 1)//remove comment div
						$('news_comment_'+news_id+'_'+comment_id).remove();			
						
						if(json.total_comments)
						{
							if(json.total_comments == 'zero')
							json.total_comments = '0';
								
							//$('total_comments'+news_id).update(json.total_comments);
							$('total_comments_news'+news_id).update(json.total_comments);
							
							if($('total_comments_mininews'+news_id))
							$('total_comments_mininews'+news_id).update(json.total_comments);
						}
				  }
			});
		}
	},
	//update news groups select box on language change
	ArchiveNewsGroups_Xupd:function(){		
		var newsTopObj = this;
		var Xedit_url = newsTopObj.newsMSurl+'&f=updateGroupsOnFormAX'; 	
		
		new Ajax.Request(Xedit_url, {
		  method: 'post',
		  postBody:'n_sel_language='+encodeURIComponent($('news_a_language').value),//'new_news_title='+encodeURIComponent($('new_news_title').value)
		  asynchronous:true, 
		  evalScripts:true,
		  onSuccess: function(transport) {
			//alert(transport.responseText);
			//var json = transport.responseText.evalJSON();
			var json=eval('(' + transport.responseText + ')');
			var groupsOptions = "";
			
			//clear old options
			$(newsTopObj.listcontolID+'_news_archive_group_filter').options.length = 0;
			var test = '';
			for (var i = 0; i < json.length; i++)
			{
				jsonSub = eval(json[i]);
				//populate select box with new options
				$(newsTopObj.listcontolID+'_news_archive_group_filter').options[i] = new Option(jsonSub.title,jsonSub.id);
				
				//if(jsonSub.id == insID)
				//$('new_news_sel_group_id').selectedIndex = i;				
				//test += 'id:'+jsonSub.id+' title: '+jsonSub.title+', ';					
			}
			//alert(test);
		  }
		});		
	},
	updateIndicatorShow:function(container_id){
		//var html = "ho ho";
		var html = '<img style="float:right; z-index:3000;  padding-left:10px;" src="/templates/default/img/indicator.gif"  alt="Loading..." border=0 align="absmiddle"/>';
		new Insertion.Top(container_id, html);
		$(container_id).show();
	}
}