var IE = /*@cc_on!@*/false;
var keepBorders = "keep";
if(IE){
	keepBorders = "";
}

var addthis_language = "es";
var addthis_append_data = true;

function blink(element){
	var element_id = "#" + element;
	$(element_id).fadeOut(100).fadeIn(100).fadeOut(100).fadeIn(100).fadeOut(100).fadeIn(100).fadeOut(100).fadeIn(100);
}

function auth(provider){
	var providerUrl = '';
	if(provider == 'google')
		providerUrl = 'https://www.google.com/accounts/o8/id';

	if(provider == 'yahoo')
		providerUrl = 'http://yahoo.com';
			
	$("#open_id_url").val(providerUrl);

	doAuth();
}

function doAuth(){
	var providerUrl = $("#open_id_url").val();

	if(providerUrl == ''){
		alert("La URL de acceso no puede estar en blanco.");
		return;
	}

	var form = $("#authform");	
	form.submit();
}

function voteComplaintUp(cid){
	$.post("/queja/votar/positivo/", {"cid":cid},
			handleVoteResponse
			, "text");
}

function voteComplaintDown(cid){
	$.post("/queja/votar/negativo/", {"cid":cid},
			handleVoteResponse
			, "text");
}

function handleVoteResponse(data){
	votes = JSON.parse(data);
	if(!votes.cid){
		alert("Ya has votado anteriormente esta queja.");
		return;
		}
	updateVoteCount(votes.cid, votes.votes_up, votes.votes_down);
}

function updateVoteCount(cid, up, down){
	var vote_up_element = "#cvup_" + cid;
	var vote_down_element = "#cvdown_" + cid;
	$(vote_up_element).text(up);
	$(vote_down_element).text(down);
}

function submitComplaint(sid, sname, saddress, surl, complaint_title, complaint){
	$.post("/quejas/nueva/enviar/", {"sid":sid, "sname":sname, "saddress":saddress, "surl":surl, "complaint_title":complaint_title, "complaint":complaint},
			submitComplaintResponse
			, "text");
}

function submitComplaintResponse(data){
	var complaint_add_response = JSON.parse(data);
	var id = complaint_add_response.id;
	var storeid = complaint_add_response.store_id;
	
	if(id){
		window.location = "/comercio/" + storeid;
	}else{
		alert("Se ha producido un error al registrar la denuncia. Por favor inténtelo de nuevo");
	}
}

function submitSiteComment(comments_name, comments_address, comments_url, comments_email, comments_title, comment){
	$.post("/comentarios/enviar/", {"comments_name":comments_name, "comments_address":comments_address, "comments_url":comments_url, "comments_email":comments_email, "comments_title":comments_title, "comment":comment},
			submitSiteCommentResponse
			, "text");
}

function submitSiteCommentResponse(data){
	var submit_site_comment_response = JSON.parse(data);
	var id = submit_site_comment_response.id;
	if(id){
		showSubmitCommentMessage("Comentario almacenado correctamente. Gracias.", false);
	}
	else{
		showSubmitCommentMessage("Se ha producido un error al almacenar su comentario. Por favor, inténtelo de nuevo.", true);
	}
	clearCommentBox();
}

function submitComplaintComment(cid, comment){
	$.post("/queja/comentar/", {"cid":cid, "comment":comment},
			submitComplaintCommentResponse
			, "text");
}

function submitComplaintCommentResponse(data){
	var comments_html = $("#complaint_comments").html();
	
	var recent_comment = JSON.parse(data);
	var cfid = "cid" + recent_comment.complaint_id + "c" + recent_comment.id;
	var content = recent_comment.content;
	var createdBy = recent_comment.createdby;
	var createdDate = recent_comment.created;
	
	var new_comment = "<div class=\"comment\" id=\"" + cfid + "\">";
	new_comment = new_comment + "<div class=\"comment_contents\">" + content + "</div>";
	new_comment = new_comment + "<div class=\"author\">Por " + createdBy + " (" + createdDate + ")</div>";
	new_comment = new_comment + "</div>";
	
	var no_comments = $("#nocomments").hide();
	comments_html += new_comment;
	$("#complaint_comments").html(comments_html);
	clearCommentBox()
	blink(cfid);
}

function submitNewUserName(userName){
	$.post("/perfilusuario/cambiarnombre/", {"username":userName},
			submitNewUserNameResponse
			, "text");
}

function submitNewUserNameResponse(data){
	var username_changed_response = JSON.parse(data);
	if(username_changed_response.ocupado == "true"){
		alert("No se puede cambiar al nombre de usuario indicado porque está en uso por otro usuario.");
	}else{
		alert("Nombre de usuario cambiado. Todas las entradas que haya en el sitio se verán con este nuevo nombre.");
	}
}