var SmsFeed = new Class({
	
	initialize : function(id) {
		this.id = id;
	},
	
	fadeToGray : function() {
		$('sms-feed').tween('color', '#888');
	},
	
	fadeOut : function() {
		$('sms-feed').tween('color', '#fff');
	},
	
	substitute : function(txt) {
		
		new Fx.Tween($('sms-feed'), {
			
			onComplete : function() {
				
				$('sms-feed').set('text', txt);
				
				new Fx.Tween($('sms-feed'), {}).set('color', '#3B5998');
				
				oSmsFeed.fadeToGray.delay(1800);
				oSmsFeed.update.delay(3000);
				
			}
			
		}).start('color', '#fff');
		
	},
	
	substituteSoft : function(txt) {
		
		new Fx.Tween($('sms-feed'), {
			
			onComplete : function() {
				
				$('sms-feed').set('text', txt);
				
				new Fx.Tween($('sms-feed'), {}).start('color', '#888');
				
				oSmsFeed.update.delay(3000);
				
			}
			
		}).start('color', '#fff');
		
	},
	
	update : function() {
		
		oData = new Hash();
		if(oSmsFeed.id == null) {
			oSmsFeed.id = 'NULL'; //to-do... fix this hack... should accept true null
		} 
		oData.set('id', oSmsFeed.id);
		
		new Request.JSON({
			method: 'post',
			data: JSON.encode(oData),
			url: '/ajax/SmsFeed.update.ajax.php',
			onFailure: function(exc) {
				oSmsFeed.update.delay(6000);
			},
			onError: function() {
				oSmsFeed.update.delay(6000);
			},
			onSuccess: function(o) {

				if($type(o) == 'object') {
					if(o.success) {
						
						if(o.smsid != oSmsFeed.id) {
							oSmsFeed.id = o.smsid;
							oSmsFeed.substitute(o.txt);
						} else if(o.txt != $('sms-feed').get('text')) {
							oSmsFeed.substituteSoft(o.txt);
						} else {
							oSmsFeed.update.delay(3000);	
						}
						
					} else {
						//alert('No Update');
						oSmsFeed.update.delay(3000);
					}
				} else {
					oSmsFeed.update.delay(6000);
				}

			}
		}).send();
		
	}
						
});
