var countParticipationsData = -1;

countParticipationsCallback = function (req) {
	var messageXML = req.xhRequest.responseXML;
	
	// fix IE bug
	if (!messageXML || !messageXML.firstChild) {
		messageXML = Spry.Utils.stringToXMLDoc(escape(req.xhRequest.responseText));
		if (!messageXML || !messageXML.firstChild) {
			printMessage(area, 'Error interno en el servidor.');
			return;
		}
	}
		
	var data = Spry.XML.documentToObject(messageXML);
	if (data && data['int']) {
		countParticipationsData = parseInt(data['int']._value());
	} else {
		countParticipationsData = -1;		
	}
}

function countParticipations(type) {
	Spry.Utils.loadURL("POST", "/su/services/countParticipations?TYPE=" + type + "&ts=" + timestamp(), false, countParticipationsCallback, {postData: " ",errorCallback: commonToolsCallbackError});
}

/******* gets a snapshot *******/
function getSnapshot() {
	if (isUserLogged() == true) {
		countParticipations('ALBUM');
		if (countParticipationsData == -1) {
			return;	
		}
		
		if (countParticipationsData < 20) {
			Spry.$('flashcontent').snapshot();
		} else {
			mostrar_toolbar('alert_fotos');
		}
	} else {
		mostrar_toolbar('foto_video');
	}
}

/******* add2mivideoteca *******/
function addToMiVideoteca(id) {
	if (isUserLogged() == true) {
		countParticipations('VIDEOTECA');
		if (countParticipationsData == -1) {
			return;	
		}
		
		if (countParticipationsData < 20) {
			Spry.Utils.loadURL("POST", "/su/services/addFavorite?TYPE=VIDEOTECA&ITEMID=" + id, true, addItemVideotecaCallback, {postData: " ", errorCallback: commonToolsCallbackError});
		} else {
			mostrar_toolbar('alert_videos');
		}
	} else {
		mostrar_toolbar('fav_video');
	}
}

/************** send2friend ************/
function send_email() {
	// antes de enviar: completar friend_pageurl con el "window.location.hostname"
	var cmForm = Spry.$('send_video_form');
	if (cmForm) {
		cmForm.action = "/su/services/sendToFriend";
		Spry.Utils.submitForm(cmForm, sendFriendResponse, {method:'post', errorCallback: sendFriendError, headers: {"Content-Type": "application/x-www-form-urlencoded; charset=utf-8"}});
	}
	
	return false;
}

/************** share **************/
function share(identifier) {
	var shareURL = "";
	
	if (identifier == 'meneame.net') {
		shareURL = 'http://meneame.net/submit.php?url='+document.location;
	} else if (identifier == 'del.icio.us') {
		shareURL = 'http://del.icio.us/post?title='+escape(document.title)+'&url='+document.location;
	} else if (identifier == 'digg.com') {
		shareURL = 'http://digg.com/submit?phase=2&url='+document.location;
	} else if (identifier == 'facebook.com') {
		shareURL = 'http://www.facebook.com/sharer.php?src='+document.location;
	} else if (identifier == 'yahoo.es') {
		shareURL = 'http://myweb2.search.yahoo.com/myresults/bookmarklet?u='+document.location+'&t='+escape(document.title);
	}
	
	if (shareURL != "") {
		window.open(shareURL, '','');
	}
	
	return false;
}

/************** Clipboad_xxxx ************/
function ClipBoard_link() {
	if (navigator.appName=="Microsoft Internet Explorer") {
		var holdtext_link = Spry.$('holdtext_link');
		holdtext_link.innerText = Spry.$('copytext_link').innerText;
		Copied = holdtext_link.createTextRange();
		Copied.execCommand("Copy");
		showMessage("El código se ha copiado en el portapapeles.");
	} else {
   	 	alert ("Selecciona el texto y presiona Crtl+C para copiar al portapapeles"); 
   	}
}
			
function ClipBoard_embed() {
	if (navigator.appName=="Microsoft Internet Explorer") {
		var holdtext_embed = Spry.$('holdtext_embed');
		holdtext_embed.innerText = Spry.$('copytext_embed').innerText;
		Copied = holdtext_embed.createTextRange();
		Copied.execCommand("Copy");
		showMessage("El código se ha copiado en el portapapeles.");
	} else {
   	 	alert ("Selecciona el texto y presiona Crtl+C para copiar al portapapeles"); 
   	}
}

/*** callback functions ***/
captureLoginResponse = function (req) {
	showMessage("El usuario se ha identificado correctamente.", 2000);
}

favoriteLoginResponse = function (req) {
	showMessage("El usuario se ha identificado correctamente.", 2000);
}


addItemVideotecaCallback = function (req) {
	showMessage("El video se ha añadido a tu videoteca.");
}

sendFriendResponse = function (req) {
	showMessage("Gracias por enviar nuestro video", 3000);
}

sendFriendError = function (req) {
	commonToolsCallbackError(req);
	Spry.$('send_video_form').action = "";
}

favoriteError = function (req) {
	commonToolsCallbackError(req);
}

captureError = function (req) {
	commonToolsCallbackError(req);
}

commonToolsCallbackError = function (req) {
	// reset data
	countParticipationsData = -1;
	
	printError("toolsErrorArea", req);
	$("#toolsErrorBox").fadeIn('fast');
	setTimeout("ocultar_toolbar('toolsErrorBox')", 4000);
}
