/*
 * jQuery doTimeout: Like setTimeout, but better! - v0.4 - 7/15/2009
 * http://benalman.com/projects/jquery-dotimeout-plugin/
 * 
 * Copyright (c) 2009 "Cowboy" Ben Alman
 * Licensed under the MIT license
 * http://benalman.com/about/license/
 */
(function($){var a={},c="doTimeout",d=Array.prototype.slice;$[c]=function(){return b.apply(window,[0].concat(d.call(arguments)))};$.fn[c]=function(){var e=d.call(arguments),f=b.apply(this,[c+e[0]].concat(e));return typeof e[0]==="number"||typeof e[1]==="number"?this:f};function b(l){var m=this,h,k={},n=arguments,i=4,g=n[1],j=n[2],o=n[3];if(typeof g!=="string"){i--;g=l=0;j=n[1];o=n[2]}if(l){h=m.eq(0);h.data(l,k=h.data(l)||{})}else{if(g){k=a[g]||(a[g]={})}}k.id&&clearTimeout(k.id);delete k.id;function f(){if(l){h.removeData(l)}else{if(g){delete a[g]}}}function e(){k.id=setTimeout(function(){k.fn()},j)}if(o){k.fn=function(p){o.apply(m,d.call(n,i))&&!p?e():f()};e()}else{if(k.fn){j===undefined?f():k.fn(j===false);return true}else{f()}}}})(jQuery);


Array.prototype.unique = function () {
	var r = new Array();
	o:for(var i = 0, n = this.length; i < n; i++)
	{
		for(var x = 0, y = r.length; x < y; x++)
		{
			if(r[x]==this[i])
			{
				continue o;
			}
		}
		r[r.length] = this[i];
	}
	return r;
}

function new_appt_conf_beep(){
	//load the player, and play the alert sound
	$("#player_div").empty();
	$("#player_div").prepend(insertPlayer());
}

function show_new_appt_notice(appts){
	//appts.new_appts hold the appts object
	var clients = [];
	var client_i = 0;
	$.each(appts.new_appts,function(i,val){
		clients[client_i] = val.fname + ' ' + val.lname;
		client_i++;
	});
	
	//unique client list
	var unique = clients.unique(); //array of unique clients that have booked appointments
	
	//build the client list
	var client_str = '';
	var client_x = 0;
	$.each(unique,function(){
		if(client_x == 0){
			client_str += this;
			client_x++;
		}else{
			if(client_x + 1 == unique.length){
				client_str += ' and ' + this;
			}else{
				client_str += ', ' + this;
			}
			client_x++;
		}
	});
		
	//build the output for the notice
	var plurality = (unique.length > 1) ? true : false;
	var closure = '';
	if(plurality){
		closure = ' have all booked appointments recently!';
	}else{
		closure = ' has recently booked an appointment!';
	}
	var notice_str = client_str + closure;
	
	//show the notice
	$('#new_appt_notice_cont').text(notice_str).parent().show().effect('pulsate').doTimeout(9000,function(){$(this).fadeOut(3000);return(false);});
	
	//beep
	new_appt_conf_beep();
	
	//remove the in session no unconfirmed message
	$('.nounconfirmed_msg').remove();
	
	//add the new appointments to the table
	$.each(appts.new_appts,function(i,val){
		$('#ucappt_list').append("<tr id='unconfirmed" + val.appt_id + "'><td class='bordertopucappts'><input type='checkbox' rel='" + val.appt_id + "' class='approve_checkbox' name='appt_id[]' value='" + val.appt_id + "' /></td><td class='bordertopucappts' style='padding-right:10px;'><span style='font-weight:bold;color: #387481;'>" + val.fname + " " + val.lname + "</span> would like a <span style='font-weight:bold;color:#387481;'>" + val.service_name + "</span> on <span style='font-weight:bold;color:#387481;'>" + val.appt_date + " @ " + val.appt_time + "</span></td></tr>");
	});
	
	//update the number of unconfirmed
	var newval = ($('#num_unconfirmed').text() * 1) + appts.new_appts.length;
	$('#num_unconfirmed').text(newval);
	$('#unconfirmed_badge,#large_unconfirmed').text(newval);
	
	//remove the default unconfirmed
	$('#default_nouc').remove();
}







