$(function() {
	var nosubmit = false;
	$(".post_plus").click(function(){
		var post_id = this.id.slice(1)
		$("#post_" + post_id + " .listpost_post").show("fast");
		$("#post_" + post_id).find(".post_plus").hide();
		$("#post_" + post_id).find(".post_minus").show();
		return false;
	});
	
	$(".post_minus").click(function(){
		var post_id = this.id.slice(1)
		$("#post_" + post_id + " .listpost_post").hide("fast");
		$("#post_" + post_id).find(".post_minus").hide();
		$("#post_" + post_id).find(".post_plus").show();
		return false;
	});

    //gestisco il tooltip   
   $(".category select").hover(
        function(){
            $("span#cat" + this.id)
                .css("position", "relative")
                .find("span.tooltip")
                    .addClass("tooltip_show")
                    .removeClass("tooltip")
       },
       function(){  
            $("span#cat" + this.id)
                .css("position", "")
                .find("span.tooltip_show")
                    .addClass("tooltip")
                    .removeClass("tooltip_show")
       }
    );
    //gestisco il tooltip

   $('.category select').change(function(){
        //se cambio il valore, mi da la possibilità di aggiornare
        
        //mi dissocio dalla struttura html
        //$(this).parent().find("input[type='button']").attr("disabled", "")
        $("span#cat" + this.id).find("input[type='button']").attr("disabled", "")
   });
   
    $('.category select.channel_filter').change(function(){
        //faccio il redirect
        var url = "/?" + $(this).parent().find("input[type='hidden'].channel_filter_url").val() + $(this).val()
        window.location.href = url
        return false
   });
   
   $('.category select option').click(function(event) {
      //annulla la selezione se 0
      var current_value = $(this).attr("value")
      var select = $(this).parent()
      if(current_value == 0){
        select.find("option").each(function(){
            var value = $(this).attr("value")
            if(value != 0){
                $(this).attr("selected", "")
            }
        });
      }else{
        select.find("option[value='0']").attr("selected", "")
      }
      return false;
   });
   
    $(".category input[type='button']").click(function(event) {
      //alert('pippo');
      changecategory(event);
      $(this).attr("disabled", "disabled")
      return false;
   });
   
   	//seleziono la catogoria giusta per ogni post
	$('.category').each(function(){	    
	    //oggetto dropdown
	    var select = $(this).find("select")
	    //value dell'hiddenfield che contiene la categoria del post
	    var category = $.trim($(this).find(".selected_category").val())
	    //rimuovo l'hiddenfield	    
	    //$(this).find(".selected_category").remove()
	    //imposto la selezione	    
	    if(category.length > 0){
	        category = category.split(",")
	        for(i=0;i<category.length;i++){
	            select.find("option[value='" + category[i] + "']").attr("selected", "selected")
	        }	        
	    }else{
	        select.find("option").attr("selected", "")
	        select.find("option[value='0']").attr("selected", "selected")
	    }
	});
	
	$(".moderacommento").click(function(event){
	    moderacommento(event);
	    return false;
	});
	
	
	$('.approvelink a').click(function(event) {
		approve(event);
		return false;
	});
	
	$('.suspendlink a').click(function(event) {
		suspend(event);
		return false;
	});
	
	$('.evidencelink').click(function(event) {
		evidence(event);
		return false;
	});
	
	$('.noevidencelink').click(function(event) {
		noevidence(event);
		return false;
	});

	$('.approve_agenda a').click(function(event) {
		approve_agenda(event);
		return false;
	});

	$('.suspend_agenda a').click(function(event) {
		suspend_agenda(event);
		return false;
	});

	
	$("#search_author").keypress(function (e) {
		nosubmit = true;
		if (e.which == 13) {
			$("#search_btn").trigger('click');
		}
	});
	
	$("#search_keyword").keypress(function (e) {
		nosubmit = true;
		if (e.which == 13) {
			$("#search_btn").trigger('click');
		}
	});

	$("#search_btn").click(function() {
		nosubmit = true;
		var url = searchurl;
		var _author = $("#search_author").val();
		var _tag = $("#search_keyword").val();
		var _date = $("#search_data").val();
		if (_author.length>0) {
			url += "&searchauthor=" + _author
		}
		if (_tag.length>0) {
			url += "&tag=" + _tag
		}
		if (_date.length>0) {
			url += "&searchdate=" + _date
		}
		window.location = url
	});
	
	$("#cerca_btn").click(function() {
		nosubmit = true;
		var url = searchurl;
		var _search= $("#cerca").val();
		if (_search.length>0) {
			url += "&search=" + _search
		}
		window.location = url
	});
	
	$("#cerca").keypress(function (e) {
		nosubmit = true;
		if (e.which == 13) {
			$("#cerca_btn").trigger('click');
		}
	});
	
	$("form").submit(function(){
 		if (nosubmit) return false;
	});
	

	
});

function changecategory(event) {
	var el = (event.target || event.srcElement);
	var parent = $(el).parent()
	var post_id = parent.attr("id").slice(3);
    var category = $(parent).find("select").val() + ""
   $.get("/groups/changecategory.ashx", {postid: post_id, groupid: ID_GROUP, category: category}, function(xml){
      return false;
	});
}


function moderacommento(event){
	var link = (event.target || event.srcElement);
	var id_comment = link.id.slice(1).split("__")[0]
	var type = link.id.split("__")[1]
	$.get(
	    "/groups/moderacommento.ashx",
	    {c: id_comment, type: type},
	    function(ok){
	        if(ok > 0){
	            var previous_state = $(link).parent().find("input[type='hidden']").val()
	            if(previous_state == 0){
	                $(link).html("ripristina")
	                $(link).parent().find("input[type='hidden']").attr("value", "1")
	            } else if(previous_state == 1){
	                $(link).html("elimina")
	                $(link).parent().find("input[type='hidden']").attr("value", "0")
	            }
	        } else {
	            //errore
	        }
	    }
	);
    
}


function suspend(event) {
   var el = (event.target || event.srcElement);
   var link = el.parentNode;
   var post_id = link.id.slice(3);
   $.get("/groups/approvepost.ashx?", {postid: post_id, groupid: ID_GROUP, approve: "false"}, function(xml){
      $(link).removeClass("suspendlink");
      $(link).addClass("approvelink");
      $(el).unbind();
      $(el).click(function(event) {
         approve(event);
         return false;
      });
      $(el).html("Approva post");
      $("#post_" + post_id).addClass("suspended");
      $("#el_" + post_id + " a").html("");
      $("#el_" + post_id + " a").unbind();
   })
}

function approve(event) {
	var el = (event.target || event.srcElement);
	var link = el.parentNode;
	var post_id = link.id.slice(3);
	$.get("/groups/approvepost.ashx?", {postid: post_id, groupid: ID_GROUP, approve: "true"}, function(xml){
		$(link).removeClass("approvelink");
		$(link).addClass("suspendlink");
		$(el).unbind();
		$(el).click(function(event) {
			suspend(event);
			return false;
		});
		$(el).html("Sospendi post");
		$("#post_" + post_id).removeClass("suspended");
		$("#post_" + post_id).removeClass("evidenced");
		$("#el_" + post_id + " a").html("Metti post in evidenza");
		$("#el_" + post_id + " a").unbind();
		$("#el_" + post_id + " a").click(function(event) {
			evidence(event);
			return false;
		});
	})
}

function evidence(event) {
	var el = (event.target || event.srcElement);
	var link = el.parentNode
	var post_id = link.id.slice(3);
	$.post("/groups/evidencepost.ashx?", {postid: post_id, groupid: ID_GROUP, remove: "false"}, function(xml){
			$(link).removeClass("evidencelink");
			$(link).addClass("noevidencelink");
			$(el).unbind()
			$(el).click(function(event) {
				noevidence(event);
				return false;
			});
			$(el).html("Togli post dall'evidenza");
			$("#post_" + post_id).removeClass("suspended");
			$("#post_" + post_id).addClass("evidenced");
	});
}

function noevidence(event) {
	var el = (event.target || event.srcElement);
	var link = el.parentNode;
	var post_id = link.id.slice(3);
	$.post("/groups/evidencepost.ashx?", {postid: post_id, groupid: ID_GROUP, remove: "true"}, function(xml) {
			$(link).removeClass("noevidencelink");
			$(link).addClass("evidencelink");
			$(el).unbind();
			$(el).click(function(event) {
				evidence(event);
				return false;
			});
			$(el).html("Metti post in evidenza");
			$("#post_" + post_id).removeClass("suspended");
			$("#post_" + post_id).removeClass("evidenced");
	});
}


function suspend_agenda(event) {
	var el = (event.target || event.srcElement);
	var link = el.parentNode;
	var event_id = link.id.slice(3);
	$.get("/groups/approveagenda.ashx?", {eventid: event_id, groupid: ID_GROUP, approve: "false"}, function(xml){
		$(link).removeClass("suspend_agenda");
		$(link).addClass("approve_agenda");
		$(el).unbind();
		$(el).click(function(event) {
			approve_agenda(event);
			return false;
		});
		$(el).html("approva");
		$("#event_" + event_id).addClass("suspended");
	})
}

function approve_agenda(event) {
	var el = (event.target || event.srcElement);
	var link = el.parentNode;
	var event_id = link.id.slice(3);
	$.get("/groups/approveagenda.ashx?", {eventid: event_id, groupid: ID_GROUP, approve: "true"}, function(xml){
		$(link).removeClass("approve_agenda");
		$(link).addClass("suspend_agenda");
		$(el).unbind();
		$(el).click(function(event) {
			suspend_agenda(event);
			return false;
		});
		$(el).html("sospendi");
		$("#event_" + event_id).removeClass("suspended");
	})
}
