//Site specific Javascript

// Using a namespacing var for global vars
var KSA = {};


function callHref ( e ) {
	var obj = this.up('.teamOptions');
	var oldContent = obj.innerHTML;
	obj.update('<img class="loading" src="/img/loading.gif" />');
	new Ajax.Request (this.href, {
	    onException: function ( requestor, exception ) {
	        alert(exception);
	    },
	    onFailure: function ( request, json ) {
	        alert('There was a problem processing the request');
	    },
	    onSuccess: function (request, json ) {
			if(request.getHeader('X-NFiResponse') == 'Error') {
			     obj.update(oldContent);
                 alert('We\'re sorry.  There was an error processing your request.');
                 return;
            }
			//If the request returned a notice that the user has reached the maximum
			if(request.getHeader('X-NFiResponse') == 'Can\'t Follow') {
			     obj.update(oldContent);
			     alert('You are already following the maximum amount of teams.  Please stop following another team to follow this one');
			     return;
			}
			
			if(request.getHeader('X-NFiResponse') == 'Success') {
				obj.update(request.responseText);
				var cntr = $("followTeamCount");
				if(Object.isElement(cntr)){
				    new Ajax.Updater(cntr,"/teams/getteamfollowcount");
				}
			}
			
			//add the onclick handlers for links that act like forms
			attachLinkFormClickHandlers();
			
	    }
	});
	
	
	//prevent the link from redirecting ..
	return false;
}

function callHref2 ( e ) {
    var obj = this.up('.teamOptions');
    var oldContent = obj.innerHTML;
    obj.update('<img class="loading" src="/img/loading.gif" />');
    new Ajax.Request (this.href, {
        onException: function ( requestor, exception ) {
            obj.update(oldContent);
            alert(exception);
        },
        onFailure: function ( request, json ) {
            obj.update(oldContent);
            alert('There was a problem processing the request');
        },
        onSuccess: function (request, json ) {
            if(request.getHeader('X-NFiResponse') == 'Error') {
                 obj.update(oldContent);
                 alert('We\'re sorry.  There was an error processing your request.');
                 return;
            }
                        
            if(request.getHeader('X-NFiResponse') == 'Success') {
                new Effect.Fade(obj.up('.teamBox'));
            }
            
        }
    });
    
    
    //prevent the link from redirecting ..
    return false;
}


Event.observe ( window, 'load', function ( ) {
	//add the onclick handlers for links that act like forms
	attachLinkFormClickHandlers();
});

function attachLinkFormClickHandlers ( ) {
	$$('a.linkform').each ( function ( node ) {
		node.onclick = callHref;
	});
	$$('a.linkform2').each ( function ( node ) {
        node.onclick = callHref2;
    });
}