// JavaScript Document

(function($){
	$.ajaxSetup({async:false});
	var methods = {
		init: function(){return this;},
		initChat: function(cid){
			this.bind('click',function(){
				window.open('talk.php?cid='+cid,'Chat','height=600,width=600,toolbar=no,location=no,menubar=no,resizable=no,titlebar=no,directories=no');
			});
			return this;
		},
		anticipate: function(uid,cid){
			//console.log($(this));
			var el = this;
			var sound;
			setInterval(function (){
				$.post('ajax/refreshAvailability.php',{'uid':uid});
				$.post('ajax/checkChatRequests.php',{'cid':cid},function(data){
					//console.log(data);
					var org = el.html();
					if(data!=0){
						var txt = '';
						for(var i in data){
							var token = data[i].token;
							var request = data[i].request;
							var q = data[i].question;
							if($('#'+token.tid).length == 0){
								el.append('<div id="'+token.tid+'" class="requests">Pending chat request: <strong>'+token.name+'</strong>.<br /><em>'+q+'</em> <button id="_'+token.tid+'" value="'+request.rid+'" class="r_btn">Accept</button></div>');
							}
						}
						$('.requests').each(function(){
							var tid = $(this).attr('id');
							var e = false;
							for(var i in data){
								if(tid == data[i].token.tid){
									e = true;
								}
							}
							if(!e){$(this).remove();}
						});
						if(org != el.html()){
							sound.play();
						}
					}else{
						el.html('');
					}
				},'json');
			},4000);
			$('.r_btn').live('click',function(){
				var tid = $(this).attr('id');
				var rid = $(this).val();
				window.open('talk_admin.php?tid='+tid.substr(1)+'&rid='+rid,tid,'height=600,width=600,toolbar=no,location=no,menubar=no,resizable=no');
			});
			soundManager.url = 'sound/swf/';
			soundManager.onload = function(){
				sound = soundManager.createSound({
					id:'notify',
					url:'sound/notify.mp3'
				});
			}
			return this;
		},
		awaitAcceptance: function(tid,rid,cid,q,name){
			var el = this;
			var CRA = setInterval(function(){
				$.post('ajax/checkRequestAccepted.php',{'tid':tid,'rid':rid},function(r){
					if(r=='1'){
						window.location='talk.php?tid='+tid;
					}else if(r=='expired'){
						el.html('No one is currently available. Please leave a message.<br /><textarea id="message">'+q+'</textarea><br />Email: <input type="text" id="email" /><button id="leaveMessage">Leave Message</button>');
						clearInterval(CRA);
					}
				});
			},4000);
			$('#leaveMessage').live('click',function(){
				$.post('ajax/leaveMessage.php',{'cid':cid,'name':name,'message':el.children('#message').val(),'email':el.children('#email').val()},function(msg){
					window.close();
				});
			});
		}
	}
	$.fn.SC = function(method){
	if(methods[method]){
		return methods[ method ].apply( this, Array.prototype.slice.call( arguments, 1 ));
	} else if ( typeof method === 'object' || ! method ) {
		return methods.init.apply( this, arguments );
	} else {
		$.error( 'Method ' +  method + ' does not exist.' );
	}
	
	};
})(jQuery);
