$.fn.questionPage = function() {
	$('.hide-filters').toggle( function() {
		$('#tracker-filters').hide('slow');
		$(this).text('Show filters');
	},
	function() {
		$('#tracker-filters').show();
		$(this).text('Hide filters');
	});
  $('ol.filter .remove').click( function() {
  	$(this)
  		.parent('li')
  		.remove()
  		.end();
  });
  $('ol.filter .remove-question').click( function() {
  	$(this)
  		.parent()
  		.parent('li')
  		.remove()
  		.end();
  });
  $('#ajaxTabs').tabs({ 
    show: function() {
      $.fn.answerComments();
    }
  });
}

$.fn.inboxPage = function() {
  $('.read-button').click( function() {
    $(this)
      .parent()
      .siblings('.message-body')
      .toggle();
  
  });
}
$.fn.questionForm = function() {
    var data = {
      width: 300,
      data: [
        "How are you?",
        "What is your favorite color?",
        "Who is there?",
        "Why would you?"  
      ],
      multiple: true,
      matchContains: true
    }
  $('#question').autocomplete(data);
  $('#preview-question').click( function() {
	  var q = {
		  question: $('#question').val(),
		  type: $('#html-elements').val(),
		  options: $('#options').val()
	  }
	  $.ajax({ 
	    url: 'ajax_callback.php',
	    data: q,
	    success: function(response) {
	      $("#preview .b-box").empty().prepend(response);
	      $("#preview").show(1000);
	    }
	  });
  });
}
$.fn.answerComments = function() {
  $('.hide-comments').click( function() {
    $(this)
      .parent().parent().parent().parent()
      .hide()
      .end();
  });
  $('.comment-show').toggle(function(){
      $(this).parent().parent().parent().parent().next().fadeIn(1000);
	  },function(){
	  $(this).parent().parent().parent().parent().next().hide();
	  });
	  
	 
}


$.fn.surveyPage = {
	options: {

		htmlSelect: '#html-elements',
		question: '#question',
		optionsForm: '#free-form',
		questionSearch: '#q-search'
	
	},
	init: function() {
		var self = this;
		var data = {
			width: 300,
			data: [
				"How are you?",
				"What is your favorite color?",
				"Who is there?",
				"Why would you?"	
			],
			multiple: true,
			matchContains: true
		}
		$('#attach-existing').click( function() {
			$('#search-target').children('li').each( function() {
				$("#target").prepend($(this));
			});
		});
		$(self.options.questionSearch).autocomplete(data);
		$(self.options.questionSearch).autocomplete("result", function(event, data, formatted) {
			var q = {
				question: formatted,
				type: "textarea",
				survey: true,
				line: true
			}
 			$.ajax({ 
		      url: 'ajax_callback.php',
		      data: q,
		      success: function(response) {
	
		        $("#search-target").prepend(response);
	
		        $('.remove').click( function() {
		          $(this).parent().parent().hide('slow').remove(); 
		        });
		      }
		    });

 			$('#q-search').val(" ");
		});

		self.hideOptions($(self.options.htmlSelect));
		$(self.options.htmlSelect).change( function() {
			self.hideOptions($(this));
		});
	
		$("#attach-question").click( function() {     
	
	    	var data = {
	    	    question: $(self.options.question).val(),
		        type: $('#html-elements').val(),
		        options: $('#options').val()
	    	
	    	};
	    	self.attachQuestion(data);		   
	    });

	
	},
	attachExisting: function(item) {
	var self = this;
		$(".attach-this").click( function() {     

	    	var data = {
	    	    question: item,
		        type: 'textarea'
	    	
	    	};
	    	self.attachQuestion(data);		
	    	$(this).parent().remove();   
	    });
	},
	hideOptions: function(field) {
	    if (field.val() == 'textarea') {
	      $(this.options.optionsForm).hide();
	    }
	    else {
	      $(this.options.optionsForm).show();       
	    }
	},
	attachQuestion: function(q) {
		var self = this;
		q.survey = true;
		q.line = true;
		$('#preview').hide();
		$.ajax({ 
		      url: 'ajax_callback.php',
		      data: q,
		      success: function(response) {
		        $('#none').remove();
	
		        $('#target').append(response);
		        $('#question').val(" ");
		        $('#html-elements').val("textarea");
		        $('#options').val(" ");
		
		      }
		    });
	}
}
