
$(document).ready(function(){
	var _gaq = [
	  [ '_setAccount', 'UA-1018881-4' ],
	  [ '_trackPageview' ]
	];
	$(function() {
	  $.getScript( 'http://www.google-analytics.com/ga.js' );
	});
		
	$('#openloginform').click(function(e){
		$("#loginform").slideToggle('slow');
		e.preventDefault();
	});
	
	caldays = $("a[type='calday']");
	for(i=0,size=caldays.length;i<size;i++){
		$(caldays[i]).mouseover(function(){
			data = $(this).attr("daydata").split(",");
			$('#block-calendar-day-images').html(data[0]);
			$('#block-calendar-day-news').html(data[1]);
			$('#block-calendar-day-topics').html(data[2]);
			$('#block-calendar-day-comments').html(data[3]);				
		});
	}
	

	thumbs = $("div[type='thumb']");
	for(i=0,size=thumbs.length;i<size;i++){
		$(thumbs[i]).mouseover(function(){
			$(this).children(".thumb-acronym").css("visibility","visible");			
		});
		$(thumbs[i]).mouseout(function(){
			$(this).children(".thumb-acronym").css("visibility","hidden");			
		});
	}	
});

function preview(){
	$.post('http://'+window.location.host+'/main.comments.php', { op: "preview", comment: $('#comment_text').val()},
	function(data){
		$('#preview_content').html(data);
		$('#comment_text').css("display","none");
		$('#preview').css("display","block");
		$('#preview_button').attr("value","Editar");			
	});
}

function exitpreview(){
        $('#preview_content').html("");
        $('#preview').css("display","none");
		$('#comment_text').css("display","block");		
		$('#preview_button').attr("value","Previsualizar");
    
}

function send_comment(cid){
	var sendComment = $('#send_comment');
	exitpreview();
	if($('#nouser')){user = $('#nouser').val();}else{user ='';}	
	$.post('http://'+window.location.host+'/main.comments.php', { op: "send", nouser: user, comment: $('#comment_text').val(), id: cid, replyto: $('#reply_to').val() },
	function(data){
	if(data=='true'){
		sendComment.disabled=false;
		sendComment.attr("value","Comentar");
		reply(0);
		$('#comment_text').attr("value","");
		comments(cid);
	}else{		
		sendComment.disabled=false;
		sendComment.attr("value","Reintentar");
		alert(data);
	}     
   });
}

function comments(cid){
	$.get('http://'+window.location.host+'/main.comments.php', {	op: 'read', id: cid}, function(data) {
  		$('#comments').html(data);
	});	
}

// -- Varios --

function popup(url, width, height, name) {
	if (!name) {
		name = '_popup';
	}

	window.open(url.replace(/&amp;/g, '&'), name, 'height=' + height
			+ ',resizable=yes,scrollbars=yes,width=' + width);
	return false;
}

function jumpto() {
	var page = prompt('{LA_JUMP_PAGE}:', '{ON_PAGE}');
	var perpage = '{PER_PAGE}';
	var base_url = '{A_BASE_URL}';

	if (page !== null && !isNaN(page) && page > 0) {
		document.location.href = base_url.replace(/&amp;/g, '&') + '&start='
				+ ((page - 1) * perpage);
	}
}

/**
 * Find a member
 */
function find_username(url) {
	popup(url, 760, 570, '_usersearch');
	return false;
}


/**
 * Mark/unmark checklist id = ID of parent container, name = name prefix, state =
 * state [true/false]
 */
function marklist(id, name, state) {
	var parent = document.getElementById(id);
	if (!parent) {
		eval('parent = document.' + id);
	}

	if (!parent) {
		return;
	}

	var rb = parent.getElementsByTagName('input');

	for ( var r = 0; r < rb.length; r++) {
		if (rb[r].name.substr(0, name.length) == name) {
			rb[r].checked = state;
		}
	}
}

// -- Votos --
function vote(vote_id, vlevel, vkey, vtopic) {
	if(vkey != ''){
		$.get('http://'+window.location.host+'/main.votes.php', {key: vkey, level: vlevel}, function(data) {
	  		$(('#vote_id_' + vote_id)).html(data);
		});
	}else{
		$.get('http://'+window.location.host+'/main.votes.php', {topic: vtopic, level: vlevel}, function(data) {
			$(('#vote_id_' + vote_id)).html(data);
		});
		
	}
}

function light_stars(vote_id, level) {
	for (i = 1; i <= level; i++) {
		$('#star_t' + vote_id + '_r' + i).removeClass("vote-blank").addClass("vote-full");
	}
	for (i = level + 1; i <= 5; i++) {
		$('#star_t' + vote_id + '_r' + i).removeClass("vote-full").addClass("vote-blank");
	}
}

function unlight_stars(vote_id, level) {
	for (i = 1; i <= 5; i++) {
		if (level + 1 <= i) {
			$('#star_t' + vote_id + '_r' + i).removeClass("vote-full").addClass("vote-blank");
		}
		if (level >= i) {
			$('#star_t' + vote_id + '_r' + i).removeClass("vote-blank").addClass("vote-full");
		}
		if (i == 5) {
			break;
		}
	}
}
