/////general////// 
/*
* Puts the contents of contents_id
* into the target_id html
*/
function replace_contents(contents_id,target_id)
{
    if($("#"+contents_id).length > 0 && $("#"+target_id).length > 0){
        $("#"+target_id).html($("#"+contents_id).html());    
    }
}


///////////////////////////////////////USERS /////////////////////////////////////////////////////////////////

function login()
{
	
	$("#user-login-feedback").html('A enviar...');
		
	var user = $("#user").val();
    var pass = $("#password").val();
    
    if(user == '' || pass == '')
    {
    	$("#user-login-feedback").html('<span class="error">Por favor introduza o login e a password.</span>');
    	return false;
    }
    
    $("#user-submit").attr("disabled","disabled");
    
    $.ajax({
              type:"POST",
              url: "users/login",
              data: "user="+user+"&password="+pass+"&ajx=1",
              success: function(msg){
                
                 $("#user-panel").html(msg); 
                 
                  if(inforum == true){
					//hack for when we're in the forum
					window.parent.document.location.reload();
					//if (top.frames.length>0)
        			//	setTimeout("top.location = window.location;",100);

     			  }else if(inchat = true){
     			    window.setTimeout(function() {
                        window.location.reload()
                    }, 300);
     			  }
                
              },
              error: function(msg){
                 $("#user-panel").html(msg);
              }

              
     });
     

    

     return false;
}

function logout()
{
	$("#feedback").html('A enviar...');
		
	    
    $.ajax({
              type:"GET",
              url: "users/logout/?xhr=1",
              success: function(msg){
                
                 $("#user-panel").html(msg); 
                 
                 if(inforum == true){
					//hack for when we're in the forum
					window.parent.document.location.reload();

     			  }else if(inchat = true){
     			    //wait a bit for chat
					window.setTimeout(function() {
                        window.location.reload()
                    }, 300);
     			  }
                
              },
              error: function(msg){
                 $("#user-panel").html(msg);
              }

              
     });

     return false;
}


///////////////////////////////////////////  \USERS /////////////////////////////////////////////////////




///////////////////////////////////////////ARTICLES/////////////////////////////////////////////////////////

function submit_comment_to_noticia()
{
		$("#submit-comment").attr("disabled","disabled");
		$("#comment-feedback").html('A enviar...');
		
		var author = $("#comment-author").val();
		var email = $("#comment-email").val();
		var message = $("#comment-message").val();
		var articles_id = $("#articles-id").val();
		var city = $("#comment-city").val();
		
        $.ajax({
              type:"POST",
              url: "noticias/comment",
              data: "author="+author+"&message="+message+"&email="+email+"&articles_id="+articles_id+"&city="+city,
              success: function(msg){
                 $("#comment-feedback").html(msg); 
              },
              error: function(msg){
                 $("#comment-feedback").html(msg);
                 
              }

              
         });
	     $("#submit-comment").attr("enabled","enabled");
         return false;
}
/////////////////////////////////////////// \ ARTICLES /////////////////////////////////////////////////////



///////////////////////////////////////////POLLS///////////////////////////////////////////////////////////

// Global variable definitions
var JSON_ID = "id";
var JSON_ANSWER = "answer";
var JSON_VOTES = "votes";



$(document).ready(function() {

    $("#epollsForm").submit(function(event){
        event.preventDefault();
        
        var myPollId = $('#epolls_id').val();
        var myAnswerId = $('input[@name=answer]:checked').val();

        $("#epoll_container").fadeOut("slow",function() {
        	$("#epoll_container").empty();
        	$.post("votacoes/vote/?ajax_home=1", 
        	   { epolls_id: myPollId, id_answer: myAnswerId }, 
        	   function(data) { 
        	       $("#epoll_container").append(data).fadeIn("slow",function(){
                    animateResults();
                });
            });
        });
    
    });

    if ($("#epollresults").length > 0 ) {
        animateResults();
    }
      
    $("#epollsGetResults").click(function() {
    	$("#epoll_container").fadeOut("slow",function() {
    	    var myPollId = $('#epolls_id').val();
    		$("#epoll_container").empty();
    		$.get("votacoes/resultados/"+myPollId+"/?ajax_home=1",function(data){
                $("#epoll_container").append(data).fadeIn("slow",function(){
                    animateResults();
                });
    		});
    	});
    });

});

function animateResults() {

  $("#pollresults div").each(function(){
      var percentage = $(this).next().text();
      $(this).css({width: "0%"}).animate({
				width: percentage}, 'slow');
  });

}


/////////////////////////////////////////// \POLLS ///////////////////////////////////////////////////////////

/////////////////////////////////////////// \POPUP //////////////////////////////////////////////////////////////////////////////////

function PopUp(page, myWidth, myHeight) {
	newWind=window.open(page,"popUp","width="+myWidth+",height="+myHeight+",toolbar=no,status=no,scrollbars=yes,location=no,menubar=no,directories=no,resizable=no");
	if(newWind.opener == null) {
		newWind.opener = window;
	}
	else {
		newWind.focus();
	}
}


/////////////////////////////////////////// POPUP ///////////////////////////////////////////////////////////


function PopUpNoScroll(url) {
	newwindow=window.open(url,'name','height=700,width=580');
	if (window.focus) {newwindow.focus()}
	return false;
}

