﻿jQuery.fn.BirthdayPanel = function(cfg){
	this.each(function(){
			jQuery(this).data("ht",jQuery(this).html());
			jQuery(this).html(jQuery(this).find('span.loading').html());
		
		});
	
	var config = jQuery.extend({
			emplUrl: "/birthdays.php?category=Employees",
			studUrl: "/birthdays.php"
		},cfg);
	
  var dataReq = function(url_) {
  	var resData = null;
  	$.ajax({
			type: 'GET',
			url: url_,
			dataType: 'json',
			success: function(data){
				if(jQuery.isArray(data) && data.length > 0){
					resData = data;
				}else{
					resData = [];
				}
			},
			error: function(XMLHttpRequest, textStatus, errorThrown){
				resData = null;
			},
			async: false,
			cache: false
		
		});
		return resData;
	}
	
	var emplData = dataReq(config.emplUrl);
	var studData = dataReq(config.studUrl);
	
	var showData = function(self, ul, data){
		
		if(data == null){
			jQuery('<li></li>').html(self.find('span.error').html()).appendTo(ul);
		}
		else if(data.length == 0 ){
			jQuery('<li></li>').html(self.find('span.norecords').html()).appendTo(ul);
		
		}else{
			$.each(data,function(index,value){
						jQuery('<li></li>').html(value).appendTo(ul);
				});
		}
		
	}
	
	return this.each(function(){
			var self = jQuery(this);
			self.html(self.data("ht"));
			self.find('span.error, span.norecords, span.loading').hide();
			var itemlist = self.find('div.emplHolder').append('<ul></ul>');	
			showData(self,itemlist,emplData);
			itemlist = self.find('div.studHolder').append('<ul></ul>');	
			showData(self,itemlist,studData);
			
		});
	
}

jQuery(function($){
	//alert($('div.headlines'));
	$('#birthday').BirthdayPanel();
	
	});
	
