// JavaScript Document
/*################################################
#	Created By Jithin							##
#	Only for Employee Listing And searching		##
#	Date 16-10-2014 							##
################################################*/
var serviceURLHR 	=localStorage['serviceURLHR'];
var HRPATH 			=localStorage['basicPATH'];
var hosturl    		=localStorage['basicURL'];
var HRPATH 			=localStorage['serviceURL'];

var editIcon = "images/edit.png";
var editIconpen = "images/pen.png";
var removeIcon = "images/delete.png";

$(document).ready(function(){
	eventHandler();
	setTimeout(listEmployees,200);
	$('.SearchBrhLists').getBranch();
	$('.Searchchgdept').getDepartment();
});


function eventHandler(){
	
}


function listEmployees(){
	$.getJSON(serviceURLHR+"employeelist.php",{ page: 1},function(data){
		 emplist=data;
		 bindEmployeesList(emplist);
	});
}


function bindEmployeesList(emplist){
	 var employeegrid = $('#employee-grid').html();
		  Mustache.parse(employeegrid);   // optional, speeds up future uses
		  var rendered = Mustache.render(employeegrid,emplist);
		  $('#tablelist').html(rendered);		
}



jQuery.fn.getDepartment = function(settings){
	  var config = {
		'selected':'',
        'callback': '',
		'branch':''
    };
    if (settings){$.extend(config, settings);}
 	dpt = this;
 	departments = '';
	
	condition = '';
	if(config.branch !=''){
		condition +="&branch="+config.branch;
	}else{
		condition +="";
	}
	
	$.getJSON(serviceURLHR + "getdepartment.php?action=load",function(departdata) { 
		departnameList = departdata.department;

		$.each(departnameList,function(index,departnameList){
				
				if(departnameList.depart_id == config.selected){
					departments +='<option value="'+departnameList.depart_id+'" selected="selected">'+departnameList.depart_name+'</option>';
				}else{
					departments +='<option value="'+departnameList.depart_id+'">'+departnameList.depart_name+'</option>';	
				}
		})	
		
	return dpt.each(function(){
        $(this).append(departments);
    });
	
	if(config.callback !=''){
		window[config.callback];
	}
	});
}

jQuery.fn.getBranch = function(settings){
	$this = this;
    var config = {
		'selected':'',
        'callback': ''
    };
    if (settings){$.extend(config, settings);}
 
 	branchList = '';
	$.getJSON(serviceURLHR + 'branch.php?action=load',function(data){
		branch = data.branch;	
		$.each(branch, function(index, branch) {									 
				if(branch.branch_id == config.selected){
					branchList +='<option value="'+branch.branch_id+'" selected="selected">'+branch.branch_name+'</option>';
				}else{
					branchList +='<option value="'+branch.branch_id+'">'+branch.branch_name+'</option>';	
				}
		});	
		
		return $this.each(function(){
			$(this).append(branchList);
			console.log(branchList);
    	});
	
	if(config.callback !=''){
		window[config.callback];
	}
	});

};


jQuery.fn.center = function(parent) {
    if (parent) {
        parent = this.parent();
    } else {
        parent = window;
    }
    this.css({
        "position": "absolute",
        "top": ((($(parent).height() - this.outerHeight()) / 2) + $(parent).scrollTop() + "px"),
        "left": ((($(parent).width() - this.outerWidth()) / 2) + $(parent).scrollLeft() + "px")
    });
return 
}