// JavaScript Document

function addNews(schId)
{
	 schId = parseInt(schId);
	
	 $("#dialog").dialog('option', 'width', 400);
  $("#dialog").dialog('option', 'height', 200);
  $("#dialog").dialog('option', 'title', 'Create News Article');
  $("#dialog").dialog("open");
  $("#dialog").html('<p align="center"><img src="/images/loadingAnimation.gif" alt="Loading"/></p>');
		
		$.ajax({
  type: "GET",
		url: "/news/create-news/school/"+schId,
		success : function (t) {
			 $("#dialog").html(t);
		}
		});
}

function createNews()
{
	 $("#dialog").prepend('<p align="center"><img src="/images/loadingAnimation.gif" alt="Loading"/></p><p align="center">Saving Changes</p>');
		
		var formdata = $("#createNewsForm").serialize();
		
		$.ajax({
  type: "POST",
		url: "/news/create-news",
		data: formdata,
		success : function (t) {
		  $("#dialog").html(t);
		}
		});
}

function validate_createNews()
{
	 if(document.getElementById("headline").value == "")
		{
				alert("Error - Please enter a headline for your news article.");
				return false;
		}
		else
		{
			 return true;
		}
}

$(function() {
			var date=new Date();
			var m = date.getMonth();
	  var months = Array("January","February","March","April","May","June","July","August","September","October","November","December");
			var key = 0;
		$("#slider").slider({
			value:100,
			min: 1,
			max: 12,
			step: 1,
			slide: function(event, ui) {
			  key = ui.value - 1;
					if(ui.value != $("#slider").slider('option', 'value'))
					{
						 y = parseInt($("#news-year").text());
							var schId = parseInt($("#schId").val());
						 showMonthlyNews(key,y, schId)
					}
			  
				$("#news-month").html(months[key]);
				
			}
		});
		$("#slider").slider('option', 'value', (m+1));
		$("#news-month").html(months[m]);
	});

function showMonthlyNews(m,y,schId)
{
	 $("#news-listing").html('<p><img src="/iamges/loading_circle.gif" alt="loading" /></p>');
	 m = parseInt(m);
		y = parseInt(y);
	 m = m + 1;
	 $.ajax({
  type: "GET",
		url: "/news/show-news/m/"+m+"/y/"+y+"/school/"+schId,
		success : function (t) {
		  $("#news-listing").html(t);
		}
		});
}

$(function() { 
var date=new Date();
var y = date.getFullYear(); 
$("#news-year").html(y); 
}); 

function changeNewsYears(op)
{
	 var val = 0;
	 m = $("#slider").slider('option', 'value') - 1;
		if(op == "plus")
		{
	   val = 1;
		}
		else if(op == "minus")
		{
		  val = -1
		}
		y = parseInt($("#news-year").text()) + val;
		if(y < 2008)
		{
			 return ;
		}
		$("#news-year").html(y); 
		var schId = parseInt($("#schId").val());
		showMonthlyNews(m,y,schId);
}

function showClusterSchoolNews(school_id)
{
	 var m = parseInt($("#news-month").text());
		var y = parseInt($("#news-year").text());
		schId = parseInt(school_id);
		showMonthlyNews(m,y,schId);
}

function recycleNews(nid)
{
	 var conf = confirm("Are you sure you wish to move this News Item to the Newspaper Bin?");
		if(conf == true)
		{
			 location.href = "/news/scrap-news-item/nid/"+nid;
		}
}

function searchUsers(q)
{
	 $("#news-listing").html('<p><img src="/iamges/loading_circle.gif" alt="loading" /></p>');
		
		var nid = parseInt($("#nid").val());
		var url = "";
		
		
	 $.ajax({
  type: "GET",
  url: "/news/show-users/query/"+q,
  success : function (t) {
		$("#search_results").html(t);
  }
  });
}

function addNewsEditor(uid)
{
	 uid = parseInt(uid);
		var nid = parseInt($("#nid").val());
		
		if(nid == 0)
		{
			 var school = parseInt($("#school").val());
			 ajaxUrl ="/news/add-school-editor/school/"+school+"/uid/"+uid;
		}
		else
		{
			 ajaxUrl = "/news/add-news-editor/nid/"+nid+"/uid/"+uid
		}
		$.ajax({
  type: "GET",
  url: ajaxUrl,
  success : function (t) {
		$("#list-editors").html(t);
  }
  });
}

function delNewsEditor(eid)
{
	 eid = parseInt(eid);
		var nid = parseInt($("#nid").val());
				
		$.ajax({
  type: "GET",
  url: "/news/del-news-editors/nid/"+nid+"/eid/"+eid,
  success : function (t) {
		$("#list-editors").html(t);
  }
  });
}

function delSchoolEditor(id)
{
	 id = parseInt(id);
		var school = parseInt($("#school").val());
				
		$.ajax({
  type: "GET",
  url: "/news/del-school-editors/id/"+id+"/school/"+school,
  success : function (t) {
		$("#list-editors").html(t);
  }
  });
}


function showSchoolEditors(schId)
{
	 schId = parseInt(schId);
				
		$.ajax({
  type: "GET",
  url: "/news/show-school-editors/school/"+schId,
  success : function (t) {
		$("#list-editors").html(t);
  }
  });
}

function newspaperBinOp(nid, op)
{
	 nid = parseInt(nid);
		var conf = true;
		
		if(op == 'delete')
		{
			 conf = confirm("Are you sure you wish to permanently delete this News Article?");
		} 
		
		if((op == 'restore' || op == 'delete') && conf)
		{
  		$.ajax({
    type: "GET",
    url: "/news/newspaper-bin/function/"+op+"/nid/"+nid,
    success : function (t) {
  	  	$("#bin-contents").html(t);
    }
    });
		}
				
}
