var destUser;

// callback is provided only for animation needs
function getRoleTopVideos(elem, callback){
	var callCallback = true;
	
	var ajaxCall = function (){
		$.ajax({
			type: 'get',
			url: req,
			success: function(data) {
				target.append(data);
				if(callback)
					callback(target.parent());
			}
		});
	};
	
	try {
		if( elem.attr("class") ){
			var toggler = elem.toggleClass("on").toggleClass("off");
			
			if (!callback)
				toggler.next().slideToggle("fast");
			
			// only when opening 
			if(elem.attr("class").match(/.*on.*/) && elem.attr('id') != ""){
				var target = $("#top-" + elem.attr('id'));
				
				// the content has not been loaded yet
				if (target.size() > 0 && target.is(":empty")) {
					destUser = elem.attr('id');
					var req = "rolesTopVideo.do?destUser=" + destUser;
					
					ajaxCall();
					callCallback = false;
				}
			}
		}
	} // execute this in any other case
	finally {
		if (callback && callCallback)
			callback (elem.children('dd'));
	}
}



//operation to double check if user is already joined
function checkJoin(evt, dest, toRole, from) {
	joinOperations(dest, toRole, from, false);
}

// operation to join a user
function performJoin(dest, toRole, from) {
	joinOperations(dest, toRole, from, true);
}

// ajax call to operation
function joinOperations(dest, toRole, from, doJoin) {
	var http = new XMLHttpRequest();

	// perform join
	if (doJoin)
		http.open("GET", "join.do?destUser=" + dest + "&operation=join", true);
	// check join 
	else 
		http.open("GET", "join.do?destUser=" + dest + "&operation=check", true);

	http.onreadystatechange = function() {
		if (http.readyState == 4 && http.status == 200) {
			
			var result = http.responseText;
			if (jQuery.trim(result) == "joined") {
				//send to page
				redirectToPage(dest, toRole, from);
			}
			else if (jQuery.trim(result) == "haveToJoin") {
				//change button and ask to join
				notYetJoined(dest, toRole, from);
			}
		}
	};
	http.send(null);
}

function notYetJoined(dest, toRole, from) {
	// do nothing
}

function disJoin(e, dest, toRole, from) {

	var event = e || window.event;
	var disjoinLink = event.target || event.srcElement;
	
	//var disjoinLink = aElement;
	var http = new XMLHttpRequest();

	http.open("GET", "join.do?destUser=" + dest + "&operation=disjoin", true);

	http.onreadystatechange = function() {
		if (http.readyState == 4 && http.status == 200) {
			// change disjoin element to join
			disjoinLink.removeAttribute('href');
			disjoinLink.setAttribute('href', "javascript:performJoin('" + dest + "','" + toRole + "','" + from + "')" );
			disjoinLink.innerHTML = 'Join Again';
			disjoinLink.parentNode.className = 'join';
			//disjoinLink.parent.setAttribute('class','join');

			// change over-object from 'go' to 'join'
			var overElem = document.getElementById("join-" + dest);
			overElem.removeAttribute('href');
			overElem.setAttribute('href', "javascript:performJoin('" + dest + "','" + toRole + "','" + from + "')" );
			overElem.firstChild.src = "images/join.png";
			overElem.firstChild.alt = "join";                 
		}
	};
	http.send(null);
}

function redirectToPage(dest, toRole, from) {
	var link = "";
	if(toRole.toLowerCase() == "syndicator"){
		if(from.toLowerCase() == "creator") {
			link = "createContentFlow.do?syndicator=" + dest;
			window.location = link;
		}
		else {
			link = "syndicatorPublic.do?destUser=" + dest;
			window.location = link;
		}
	}
	
	else if(toRole.toLowerCase() == "agent") {
		if(from.toLowerCase() == "advertiser") {
			link = "advertiserController.do?agent=" + dest;
			window.location = link;
		}
		else {
			link = "agentPublic.do?destUser=" + dest;
			window.location = link;
		}
	}
	
	else if(toRole.toLowerCase() == "webtv") {
		link = "webTvHome.do?destUser=" + dest;
		window.location = link;
	} else if(toRole.toLowerCase() == "creator") {
		link = "creatorPublic.do?destUser=" + dest;
		window.location = link;
	} else {
		throw "UNKNOW_ROLE";
	}
}

function registerVisitor() {
	confirm("Sorry but you need to register to enjoy this WebTV!");
	location.href='registerUser.do?role=endUser';
}
