/* = Recommendations.js
This script accompanies the f:Recs control inside of /usmf/controls/Articles/Recommendations.ascx
This script assumes that prototype.js and Fool.js will already be included in page
*/

// Article Recommendations functions
//====================================================================

if (!Recommendations) {
    var Recommendations = {
    
			initialized: false,
			
			init: function() {
				if (!this.initialized) { // we only run if we haven't run before
					this.controls.top = $("recModule").down("input");
					this.controls.bottom = null;

					if (this.controls.top && this.controls.bottom) {
						
						this.controls.buttons = [this.controls.top, this.controls.bottom];
						this.controls.show();
						this.initialized = true;
						return;
					}
				}
			},
			
			controls: {
				buttons:null,
				show: function() {
					console.log("showing");
					if(this.buttons == null) return;
					this.buttons.each(function(ctrl){
						ctrl.style.visibility = "visible";
					});
				},
				hide: function() {
					if(this.buttons == null) return;
					this.buttons.each(function(ctrl){
						ctrl.style.display = "none";
					});
					
				}
			},
			
			RecommendIt: function (Uid, DocumentId, Action, Button) {
				var url = '/ajax/Articles/Recommendation.aspx';
				var params = 'UID=' + Uid + '&Doc=' + DocumentId	+ '&Action=' + Action;
				var myAjax = new Ajax.Updater(
						'recModule',
						url,
						{
							method: 'get',
							parameters: params,
							onComplete: function(transport){
								var str = transport.responseText.replace(/\s/g, '');   // Remove spaces.
								var matches = str.match(/<span>(\d+)<\/span>/i);			 
								var num = parseInt(matches[1])  // Grab the new number of recs.
								
								$$('.recInline').each(function (span){
									span.replace('<span class="recInline">Recommended '+ num +' times.</span>');
								});
								
							}
						}
				);
		    
				// hide the rec buttons
				this.controls.hide();
			}
    }
}



//Reveal "Recommend This" buttons if user has Javascript
Fool.onContent(function() {
	Recommendations.init();
});
