// JavaScript Document
// Developed by Jithin
// 11-09-2014 08:50 AM.
// Manage Org News

var serviceURL =localStorage['serviceURL'];
var newsdata = [];
$(function(){
	setTimeout(eventHandler,50);
	setTimeout(locationHashChanged,100);
});
												
window.onhashchange = locationHashChanged;		 	
hash=window.location.href.split('#')[1]	
function locationHashChanged() {	
	console.log('checking page....');
	hash=window.location.href.split('#')[1]	

	if(hash){
		
		$('.menu-selected').removeClass('menu-selected');			
		if($('li[data-page="'+hash+'"]').length){
			$('li[data-page="'+hash+'"]').addClass('menu-selected');		
			setTimeout(window[hash],100); // call current function	
			currentPage =hash; 
			
		}else{
			$('li[data-page="'+currentPage+'"]').addClass('menu-selected');
			if(hash){
				//window.location.hash =	currentPage;currentPage =hash; 
			}
			
		}
	}else{
		window.location.hash = "listOrganizationNews";
	}
}

///////////////////rights checking/////////////////////
jQuery('.editNews_right').live('click',editNews_right);
jQuery('.deleteNews_right').live('click',deleteNews_right);
///////////////////rights checking/////////////////////


////////////////////////////////////////Rights checking//////////////////////////////////

function deleteNews_right()
{
alert("Sorry You Don't Have Enough Permission To Delete Organization News");	
}
function editNews_right()
{
alert("Sorry You Don't Have Enough Permission To Edit Organization News");	
}
////////////////////////////////////////Rights checking//////////////////////////////////






var eventHandler = function(){
	$(document).on('click','.nav-menubar',function(){	window.location.hash = $(this).attr('data-page');	});
	$(document).on('click','.submit-news',submitNews);
	$(document).on('click','.editNews',editNews);
	$(document).on('click','.cancel-news',cancelNewsManagement);
	$(document).on('click','.deleteNews',deleteNews);
}

var listOrganizationNews = function(){
	removeCurrentPage();
	var addList = '<table width="100%" border="0" style="display:none;" cellspacing="0" cellpadding="0" class="list-table org-news-list">'+
				  '<tr><td>No</td>'+
				  '<td>Date</td>'+
				  '<td>Title</td>'+
				  '<td>Description</td>'+
				  '<td colspan="2">Action</td>'+
				  '</tr></table>';
	$('.content-area').html(addList);
	
	var q=1;
	
	$.getJSON(serviceURL + "orgnews.php?action=listAllNews",function(data){
		news = data.news;
		$.each(news,function(index,news){
			var newsDescrition =news.news_description;//.substr(0,20);
			if(newsDescrition.length > 80){
				newsDescrition = newsDescrition.substr(0,80)+"...";
			}
			var newsTitle =news.news_title;//.substr(0,20);
			if(newsTitle.length > 50){
				newsTitle = newsTitle.substr(0,50)+"...";
			}
			classedit='editNews';  classdel='deleteNews';
			$('.org-news-list').append('<tr>'+
										'<td>'+q+'</td>'+
		 								'<td>'+news.created_time+'</td>'+
		  								'<td>'+newsTitle+'</td>'+
		  								'<td>'+newsDescrition+'</td>'+
										'<td><img src="images/pen.png" class="'+classedit+'" data-id="'+news.id+'" /></td>'+
		   								'<td><img src="images/delete.png" class="'+classdel+'" data-id="'+news.id+'" /></td>'+
		  	'</tr>');		
			q++;
		});
		
		showNewPage($('.list-table'));
	});
	
}

var addOrganizationNews = function(nid){
	nid = (typeof nid === "undefined") ? "" : nid;
	
	
	removeCurrentPage(); 
		 addnewswindow = $('.new-news').clone();
		  
		 addnewswindow = addnewswindow.addClass('new-news-window');
		 
		 $('.content-area').append(addnewswindow);
		rand = Math.floor((Math.random() * 10) + 1);
		 $('.new-news-window').find('textarea').attr('id','news_description_input');
		 showNewPage($('.new-news-window'));
		
		if(nid!=''){
			$('#news_description_input').val('');
		}
			setTimeout(function(){
			 		CKEDITOR.replace('news_description_input');
					
					if(nid!=''){
						 $('.content-area').append('<input type="hidden" name="news_id" class="news_id" value="'+nid+'">');
						 $.getJSON(serviceURL +"orgnews.php?action=getNews&id="+nid,function(data){
							 CKEDITOR.instances['news_description_input'].setData( data.news_description );
							$('.content-area').find('.news_title').val(data.news_title);
						 });
					}
					
			},500);
			
			
		
		
			
}

var editNews = function(){
	$this = $(this);
	var newsid = $(this).attr('data-id');
	//addOrganizationNews(newsid);
	edit_OrganizationNews(newsid);
	
}


//
var edit_OrganizationNews = function(nid){
	nid = (typeof nid === "undefined") ? "" : nid;
	
	
	removeCurrentPage(); 
		 addnewswindow = $('.edit_news').clone();
		  
		 addnewswindow = addnewswindow.addClass('edit_news-window');
		 
		 $('.content-area').append(addnewswindow);
		rand = Math.floor((Math.random() * 10) + 1);
		 $('.edit_news-window').find('textarea').attr('id','news_description_input');
		 showNewPage($('.edit_news-window'));
		
		if(nid!=''){
			$('#news_description_input').val('');
		}
			setTimeout(function(){
			 		CKEDITOR.replace('news_description_input');
					
					if(nid!=''){
						 $('.content-area').append('<input type="hidden" name="news_id" class="news_id" value="'+nid+'">');
						 $.getJSON(serviceURL +"orgnews.php?action=getNews&id="+nid,function(data){
							 CKEDITOR.instances['news_description_input'].setData( data.news_description );
							$('.content-area').find('.news_title').val(data.news_title);
						 });
					}
					
			},500);
			
			
		
		
			
}







var submitNews = function(){
	
	var titles=$('#news_title').val();  
	var desc=$('#news_descriptions').val(); 
	
	if(titles=='' || desc=='')
	{
		alert("Please Enter All Fields");
	}
	else
	{
	var value = CKEDITOR.instances['news_description_input'].getData();
	$('#news_description_input').val(value);
	var dataString = $('.add-news-form').serialize();
	
	$.post(serviceURL + "orgnews.php?action=addNews",dataString,function(){
		if(window.location.href.split('#')[1]	== "listOrganizationNews"){
		listOrganizationNews();	
		}else{
		window.location.hash = "listOrganizationNews";	
		}
	});
	
	}
	
		

	
}


var  cancelNewsManagement = function(){
	if(window.location.href.split('#')[1]	== "listOrganizationNews"){
			listOrganizationNews();	
		}else{
		window.location.hash = "listOrganizationNews";	
		}
}
var deleteNews = function(){
	var newsid = $(this).attr('data-id');
	r = confirm("Are you sure?");
	if(r==true){
		$(this).parent().parent().hide('fade',300,function(){ $(this).remove(); })
		$.post(serviceURL +  "orgnews.php?action=del",{'id':newsid});
	}
}


var removeCurrentPage = function(){
	$('.content-area').find('table').hide('blind',500,function(){ $(this).remove(); });	
}
var showNewPage = function($this){
	$this.show('blind',500);	
}
