//REMOVER LA IMAGEN DE UN EVENTO EN UNA AGENDA
function remove_event_image(evento,uid){
	var dataString = 'event='+evento+'&uid='+uid;
	$.ajax({
		type: "POST",
		url: "http://www.imeev.com/ajax/change_event_img.php?action=remove",
		data: dataString,
		cache: false,
		success: function(html){
			$("#delete_event_image").hide();
			$("#event_image_span").html(html);
		}
	});
}
//REEMPLAZA <3 POR ♥
function heart(texto){
	texto = texto.replace("<3","♥");
	return texto;
}
//MUESTRA MAS ESTADOS DE ANIMO EN FRIENDS_MOODS
function show_more_moods(mood_id){
	$("#same-user-moods-"+mood_id).slideDown("slow");
	$("#same-user-mood-option-"+mood_id).hide();
}
//MUESTRA EL EVENTO EN EL PANEL DE CONTROL, CONVOCADO DESDE EL CALENDARIO
function showevent(anio,mes,dia,uid){
	mes = (mes*1)+1;
	$("#showevent").html('<div align="center" style="margin:5px"><img src="http://www.imeev.com/images/ajax-loader.gif"></div>');
	$.ajax({
		type: "POST",
		url: "http://www.imeev.com/ajax/load_dates.php",
		data: "action=loadevents&id="+uid+"&anio="+anio+"&mes="+mes+"&dia="+dia,
		cache: false,
		success: function(html){
				$("#showevent").html(html);
		}
	});
}
//AGREGA AL USUARIO A UN GRUPO
function add_group(gid,uid,sig){
	$.ajax({
		type: "POST",
		url: "http://www.imeev.com/ajax/add_group.php",
		data: "uid="+uid+"&gid="+gid+"&sig="+sig,
		cache: false,
		success: function(){
			$("#related-group-"+gid+" #popular-options").fadeOut(500);
			$("#related-group-"+gid+" #popular-options").fadeIn(500,function(){
				$(this).html("<b>Te has unido.</b>");
			});
		}
	});
}
//REMUEVE COMMENTS
function remove_comment(id){
	$.ajax({
		type: "POST",
		url: "http://www.imeev.com/ajax/remove_engine.php",
		data: "id="+id+"&a=comment",
		cache: false,
		success: function(){
			$(".comment-"+id).fadeOut();
		}
	});
}
function Change_This(aList)
{
	var x=document.getElementById("otro")
	if(aList.selectedIndex == 0){
		x.disabled=false
	}else{
		x.disabled=true;
		x.value = '';
	}

}
$(document).ready(function(){
	$("#slides").jFlow({
		slides: "#agenda_slides",
		controller: ".jFlowControl", // must be class, use . sign
		slideWrapper : "#jFlowSlide", // must be id, use # sign
		selectedWrapper: "jFlowSelected",  // just pure text, no sign
		width: "100%",
		height: "230px",
		duration: 400,
		prev: ".prev", // must be class, use . sign
		next: ".next" // must be class, use . sign
	});
});
function cargaragenda(uid){
	$.ajax({
		url: "http://www.imeev.com/ajax/load_dates.php",
		data: "action=loadcalendar&id="+uid,
		dataType: "json",
		success: function(calendarEvents){ 
			$("#calendar-event-list").datepicker({
				dayNames: ['Domingo', 'Lunes', 'Martes', 'Mi&eacute;rcoles', 'Jueves', 'Viernes', 'S&aacute;bado'],
				dayNamesMin: ['Do', 'Lu', 'Ma', 'Mi', 'Ju', 'Vi', 'Sa'],
				dayNamesShort: ['Dom', 'Lun', 'Mar', 'Mi&eacute;', 'Jue', 'Vie', 'S&aacute;b'],
				monthNames: ['Enero','Febrero','Marzo','Abril','Mayo','Junio','Julio','Agosto','Septiembre','Octubre','Noviembre','Diciembre'],
				monthNamesShort: ['Ene','Feb','Mar','Abr','May','Jun','Jul','Ago','Sep','Oct','Nov','Dic'],
				nextText: 'Siguiente',
				prevText: 'Atr&aacute;s',
				changeYear: true,
				changeMonth: true,
				// [rows, columns] if you want to display multiple calendars.                         
				numberOfMonths: [1, 1],
				showCurrentAtPos: 0,
				beforeShowDay: function (date){
					for (i = 0; i < calendarEvents.length; i++) {
					  	if (date.getMonth() == calendarEvents[i][0] - 1 
					  	&& date.getDate() == calendarEvents[i][1]
					  	&& date.getFullYear() == calendarEvents[i][2]) {
							return [true,"ui-datepicker-current-day ui-state-active",calendarEvents[i][3],uid]; 
						}
					}
					return [true, "",,uid];//enable all other days
				}
			});
		}
	});
}
function show_friends(type,uid,orden){
	$.post("http://www.imeev.com/ajax/show_friends.php",
		{type: type, uid: uid,orden: orden},
		 function(html){
			$("#friends-frame").html('<div align="center" style="margin:5px"><img src="http://www.imeev.com/images/ajax-loader.gif"></div>');
			$("#friends-frame").html(html);
		}
	);
}
function profile_page(uid,url){
	$("#profile-loader").load("http://www.imeev.com/profile_pages/"+url+".php");
	return false;	
}
function comment_mood(comment_id,cambio){
	$("#mood_comment_inputs_"+comment_id).toggle();
	if(cambio == 0){
		$("#mood_comment_frame_"+comment_id).toggleClass("mood_comment_frame");
	}
}
function load_cats_gift_shop(uid){
	var categoria = $("#gift_cats_shop").val();
	$("#gift_lastid").val('');
	$("#gifts_load").load('http://www.imeev.com/gifts_loader.php?uid=' + uid + '&cat=' + categoria);
}
function comment_mood_cancel(comment_id,cambio){
	$("#mood_comment_inputs_"+comment_id).hide();
	if(cambio == 0){
		$("#mood_comment_frame_"+comment_id).toggleClass("mood_comment_frame");
	}
}
function comment_historial(comment_id){
	var comments = $("#historial_comment_comments_"+comment_id).val();
	$("#historial_comment_inputs_"+comment_id).toggle();
	if(comments == 0){
		$("#historial_comment_frame_"+comment_id).toggleClass("mood_comment_frame");
	}
}
function comment_historial_cancel(comment_id){
	var comments = $("#historial_comment_comments_"+comment_id).val();
	$("#historial_comment_inputs_"+comment_id).hide();
	if(comments == 0){
		$("#historial_comment_frame_"+comment_id).toggleClass("mood_comment_frame");
	}
}
function load_more_activities(lastid,user){
	$(".morebox").html('<img src="http://www.imeev.com/images/ajax-loader.gif">');
	if(lastid > 0){
		$.ajax({
			type: "POST",
			url: "http://www.imeev.com/ajax/load_more.php?uid="+user,
			data: "lastmsg="+ lastid,
			cache: false,
			success: function(html){
				$("#last-activities").append(html);
				$("#more"+lastid).remove(); // removing old more button
			}
		});
	}else{
		$("#more"+lastid).remove();// no results
	}
}
function next_gift_step(step){
	switch(step){
		case '1':
				$(".step_1").fadeIn(1000);
				$(".step_2").hide();
		break;
		case '2':
			if($("#gift_lastid").val() == 0 || $("#gift_lastid").val() == ""){
				alert("Elige un regalo!");
			}else{
				$(".step_1").hide();
				$(".step_2").fadeIn(1000);
				$(".step_3").hide();
			}
		break;
		case '3':
			if($("#para").val() == 0){
				alert("Elige un amigo!");
			}else{
				$(".step_2").hide();
				$(".step_3").fadeIn(1000);
				$(".step_4").hide();
			}
		break;
		case '4':
			$(".step_3").hide();
			$(".step_4").fadeIn(1000);
			$("#gift_para").text($("#para :selected").text());	
		break;
	}
}
function gift_box_click(boxid,precio_caja,caja,nombre){
	var precio_regalo = $("#gift_price").val();
	var total = (precio_caja * 1)+(precio_regalo * 1);
	$("#gift_box_img").attr("src","http://www.imeev.com/images/gifts/boxes/"+ caja + ".png");
	$("#gift_total").text(total);
	$("#total_price").val(total);
	$("#gift_box_price_val").text(precio_caja);
	$("#box_price").text(precio_caja);
	$("#gift_box_title").text(nombre);
}
function select_gift(giftid,precio,category,regalo,nombre,tipo){	
	var lastid = $("#gift_lastid").val();
	$("#gift_inner_"+lastid).toggleClass("gift_outer_click");
	$("#gift_lastid").val(giftid);
	$("#gift_inner_"+giftid).toggleClass("gift_outer_click");
	$("#gift_total").val(precio);
	$("#gift_price").val(precio);
	$("#gift_price_val").text(precio);
	var precio_regalo = $("#gift_price").val();
	var precio_caja = $("#gift_box_price_val").text();
	var total = (precio_caja * 1)+(precio_regalo * 1);
	$("#gift_total").text(total);
	$("#total_price").val(total);
	$("#gift_title").text(nombre);
	if(tipo == 0){
		$("#gift_img").attr("src","http://www.imeev.com/images/gifts/" + category + "/big/" + regalo);
	}else{
		$("#gift_img").attr("src","http://www.imeev.com/custom_gifts/" + category + "/big/" + regalo);
	}
}
$(document).ready(function (){
	$("#comment-profile").live("click",function(){
		$("#comment-profile").attr('disabled',true);									
		var comment_to = $("#comment-to").val();
		var comment_from = $("#comment-from").val();
		var comment = $("#profile-comment").val();
		$.post("http://www.imeev.com/ajax/publish_profile_comment.php",
		{uid: comment_from,fid: comment_to, comentario: comment},
		function(last){
			$("#activities-holder").html('<div align="center" ><img src="http://www.imeev.com/images/ajax-loader.gif"> Cargando...</div>');
			setTimeout(function(){
				last = last+1;
				$("#activities-holder").load("http://www.imeev.com/ajax/load_more.php?uid="+comment_to+"&lastmsg="+last);
				$("#profile-comment").val('');
				$("#comment-profile").removeAttr("disabled");	
			},1000);
		});
	});
	$("#box_image").click(function (){
		$.post("http://www.imeev.com/update_gift_status.php", {
			llave: $('#llave_gift').text()
		});
	});
	$("#send_gift").click(function (){
			if(
				($("#mypoints").text() * 1) >= ($("#total_price").text() * 1) 
			){
				return true;
			}else{
				alert("No tienes los puntos suficientes!");	
			}
			return false;
	});
	$("#gift_frame").click(function (){ 
		if($("#para_gift").text() == $("#viendo_gift").text()){
			$("#box_image").fadeOut(500);
			setTimeout(function () {
				$("#gift_image").fadeIn(500);
			},1000);
			$("#box_caption").text("");
		}else{
			$("#box_caption").text("No puedes abrir un regalo que no es tuyo!");
		}
   });
	$('#gift_cats').change(function (){
		var categoria = $("#gift_cats").val();
		$("#gift_lastid").val('');
		$("#gifts_load").load('http://www.imeev.com/gifts_loader.php?cat=' + categoria);
	});
	
	$(".mood_comment_publish").click(function(){
		var element = $(this);
		var Id = element.attr("id");
		var comment = $("#mood_comment_textarea_"+Id).val();
		var uid = $("#mood_comment_myid_"+Id).val();
		var fid = $("#mood_comment_fromid_"+Id).val();
		var comments = $("#mood_comment_comments_"+Id).val();
		//var dataString = 'comentario='+ comment + '&aid=' + Id + '&uid=' + uid + '&fid=' + fid + '&comments=' + comments;
		if(comment==''){
			alert("Introduce tu comentario!");
		}else{
			$("#flash_"+Id).show();
			$("#flash_"+Id).fadeIn(400).html('<img src="/images/loading_.gif" align="absmiddle"> cargando...');
			$.post("http://www.imeev.com/ajax/publish_mood_comment.php",
				    {
					 	comentario: comment,
						aid: Id,
						uid: uid,
						fid: fid,
						comments: comments
					},
				 	function(html){
					$("#mood_comment_load_"+Id).html(html);
					$("#flash_"+Id).hide();
				}
			);
		}
		return false;
	});
	$(".historial_comment_publish").live('click',function(){
		var element = $(this);
		var Id = element.attr("id");
		var comment = $("#historial_comment_textarea_"+Id).val();
		var uid = $("#historial_comment_myid_"+Id).val();
		var fid = $("#historial_comment_fromid_"+Id).val();
		var comments = $("#historial_comment_comments_"+Id).val();
		var tipo = $("#historial_comment_type_"+Id).val();
		if(tipo == '' || tipo == null){tipo = 'own';}
		//var dataString = 'comentario='+ comment + '&aid=' + Id + '&uid=' + uid + '&fid=' + fid + '&comments=' + comments;
		if(comment==''){
			alert("Introduce tu comentario!");
		}else{
			$("#flash_"+Id).show();
			$("#flash_"+Id).fadeIn(400).html('<img src="/images/loading_.gif" align="absmiddle"> publicando...');
			$.post( "http://www.imeev.com/ajax/publish_historial_comment.php",
				{
					comentario: comment,
					type: tipo,
					aid: Id,
					uid: uid,
					fid: fid,
					comments: comments
				},
				function(html){
					$("#historial_comment_load_"+Id).html(html);
					$("#flash_"+Id).hide();
				}
			);
		}
	});
	$('#mood_comment_publish').click(function(){
		$.post("http://www.imeev.com/publish_mood_comment.php", {
			comentario: $('#mood_comment_textarea').val(),
			uid: $('#mood_comment_myid').val(),
			aid: $('#mood_comment_id').val()
		});
		uid = $('#fid').val();
		 $("#comments-popup").fadeOut(500); 
		 setTimeout(function () {
		 	$("#comments_frame").load("http://www.imeev.com/profile_pages/comments.php?uid=" + uid);
			$('#comentario').val() = '';
		 }, 1000);
	});
});