$(document).ready(function() { 
 
	//dotiahneme ak nemame 
	if($.promt == undefined) importScript("/modules/jquery/js/jquery-impromptu.3.0.min.js");
	
    $.listen('click','a.addPollButton',function(event) 
    {
       var pollID = $(this).attr("rel");
       var pollQuestion = checkString($(this).parent().find(".pollQuestion").val());
       var thisObj = $(this); 
       var thisObjResult = thisObj.parent().find(".err"); 
       if(!pollQuestion) {
    	   thisObjResult.html("Vyplnte otazku ankety");
    	   return false;
       } else {
    	   thisObjResult.html("");
       }
       
         $.ajax({  
          type: "POST",
          url: "/modules/poll/ajax.php", 
          data:"w=addPoll&pollID="+pollID+"&pollQuestion="+pollQuestion,
          beforeSend: function()
          { 
        	 thisObjResult.html("Přidávám anketu");
          },   
          success: function(msg)
          {  
        	  if(msg) {
            	  thisObj.parent().find(".pollQuestion").remove();
            	  thisObj.parent().find(".question").html(pollQuestion);
            	  thisObj.html("");
            	  thisObjResult.html("Anketa přidaná"); 
            	  thisObj.parent().parent().find(".options").html("<input type='text' value='' class='addOptionText'><a href='javascript://' class='addOption' rel='"+msg+"'>Přidej možnost</a>");
        	  } else {
        		  thisObjResult.html("Anketu se nepodařilo přidat, zkuste to znovu");
        	  }
           }   
       });
        
    });
    
    //addOption
    $.listen('click','a.addOption',function(event) 
    	    {
    	       var pollID = $(this).attr("rel");
    	       var addOptionText = checkString($(this).parent().find(".addOptionText").val());
    	       var thisObj = $(this); 
    	       var thisObjResult = thisObj.parent().find(".err"); 
    	       if(!addOptionText) {
    	    	   thisObjResult.html("Text možnosti nesmí být prázdný.");
    	    	   return false;
    	       } else {
    	    	   thisObjResult.html("");
    	       }
    	       
    	         $.ajax({  
    	          type: "POST",
    	          url: "/modules/poll/ajax.php", 
    	          data:"w=addOption&pollID="+pollID+"&addOptionTitle="+addOptionText,
    	          beforeSend: function()
    	          { 
    	        	 thisObjResult.html("Přidávám možnost");
    	          },   
    	          success: function(msg)
    	          {    
					 thisObj.parent().parent().find(".options").html(msg); 
    	          }   
    	       });
    	        
    	    });
    
    //addOptShow
   /* $.listen('click','a.addOptShow',function(event) 
    { 
    	$("#pollList .addOptionText").hide();
    	$("#pollList .addOption").hide();
    	$(this).parent().find(".addOptionText").show("slow");
    	$(this).parent().find(".addOption").show("slow");
    });*/
    //dellOption
    $.listen('click','a.dellOption',function(event) 
    	    {
    	       var optionID = $(this).attr("rel"); 
    	       $(this).parent().hide("slow");  

    	         $.ajax({  
    	          type: "POST",
    	          url: "/modules/poll/ajax.php", 
    	          data:"w=dellOption&optionID="+optionID,
    	          beforeSend: function()
    	          { 
    	        	 
    	          },   
    	          success: function(msg)
    	          {    
					  
    	          }   
    	       });
    	        
    	    });
    //editOption
    $.listen('click','a.editOption',function(event) 
    {
    	var liObj = $(this).parent();
    	var optionTitle = liObj.find(".optionTitle").html();
    	var optionID = liObj.find("a.editOption").attr("rel");    	 
    	liObj.html("<span class='optionTitle'><input type='text' class='newOptionTitle' value='"+optionTitle+"' /></span>"
    			+ " <a href='javascript://' class='updateOption' rel='"+optionID+"'>Uprav</a>"); 
    	        
    }); 
    
    //updateOption
    $.listen('click','a.updateOption',function(event) 
    {
       var optionID = $(this).attr("rel");
       var optionTitle = checkString($(this).parent().find(".newOptionTitle").val());
       var thisParent = $(this).parent();
      
       $.ajax({  
           type: "POST",
           url: "/modules/poll/ajax.php", 
           data:"w=updateOption&optionID="+optionID+"&optionTitle="+optionTitle,
           beforeSend: function()
           { 
    	    thisParent.find().html("ukladám..");
           },   
           success: function(msg)
           {    
        	 thisParent.find(".optionTitle").html(optionTitle);
        	 thisParent.find("a").remove();
        	 thisParent.append(" <a href='javascript://' class='dellOption' rel='"+optionID+"'>smaž</a> - "+
								     "<a href='javascript://' class='editOption' rel='"+optionID+"'>edituj</a>");
           }   
        });
    }); 
    
    //optionVote
    $.listen('click','a.optionVote',function(event) 
    	    {
    	       var data = $(this).attr("rel");
    	       var thisOption = $(this);
    	       
    	       $.ajax({  
    	           type: "POST",
    	           url: "/modules/poll/ajax.php", 
    	           data:"w=optionVote&data="+data,
    	           beforeSend: function()
    	           { 
    	    	   		thisOption.parent().find(".optionVoteResult").html("propočítávam..");
    	           },   
    	           success: function(msg)
    	           {  
    	        	   thisOption.parent().find(".optionVoteResult").html("done");
    	        	   thisOption.parent().parent().parent().parent().find(".options").html(msg); 
    	           }   
    	        });
    	    });     
    //dellPollR = naozaj zmazat? 
    $.listen('click','a.dellPollR',function(event) 
    	    {
    	       var pollID = $(this).attr("rel");  
    	       msg = "Skutečně si přejete smazat anketu?" +
    	         		"<input type='hidden' id='pollID' name='pollID' value='"+pollID+"' />";
    	       $.prompt(msg,{ buttons: { 'ANO': true, 'NE':false }, callback: dellPoll }); 
    	   
    	    });  
     
   //updPollQuestion     
    $.listen('click','a.updPollQuestion',function(event) 
    {    var h3Obj = $(this).parent().find("h3");
    	 var title = h3Obj.html();
    	 $(this).html("ulož");
    	 $(this).attr("class", "saveNewName");
    	 h3Obj.html("<input type='text' value='"+title+"' />");
    });
    
    $.listen('click','a.saveNewName',function(event) 
    {
    	var aObj = $(this);
    	var pollID = aObj.attr("rel");
	    var h3Obj = aObj.parent().find("h3");
	    var title = h3Obj.find("input").val();
	    
 
	    
	       $.ajax({  
	           type: "POST",
	           url: "/modules/poll/ajax.php", 
	           data:"w=saveNewPollName&pollID="+pollID+"&pollQuestion="+title,
	           beforeSend: function()
	           { 
	        	   h3Obj.html("ukladám..");
		           aObj.html("wait");
		           aObj.attr("class", "null");
	           },   
	           success: function(msg)
	           {
	        	  if(msg) {
	        		   h3Obj.html(title);
			           aObj.html("uprav");
			           aObj.attr("class", "updPollQuestion");
	        	  } else {
		        	   h3Obj.html("nastala chyba..."); 
	        	  }
	        	}   
	        });
    });
    
});
//dellPoll
function dellPoll(v,m,f){
   
     
    if(v)
    {
    	var poll = $(".dellPollR[rel='"+f.pollID+"']").parent(); 
    	poll.hide("slow"); 
    	
        $.ajax({  
         type: "POST",
         url: "/modules/poll/ajax.php", 
         data:"w=dellPoll&pollID="+f.pollID,
         beforeSend: function()
         { 
       	 
         },   
         success: function(msg)
         {    
          
         }   
      });	
    } 
}

function checkString(string) {
	return string.replace("&", " and ");
}

 


