(function($) {
    
    //Anket URL
	$(function(){

		form = $('form.crud');
		
		$('input[name="title"]', form).keyup(function(){
			$.post(BASE_URL + 'ajax/url_title', { title : $(this).val() }, function(slug){
				$('input[name="slug"]', form).val( slug );
			});
		});
		
	});
    
  
  //Anket Ajax Form  
  $(function(){
	var loader=$('#anketloader');
	var pollcontainer=$('#pollcontainer');
	loader.fadeIn();
    
	//Anket Formunu Yükle
	$.get(BASE_URL + 'anket/ajax_poll', '', function(data, status){
		pollcontainer.html(data);
		animateResults(pollcontainer);
		pollcontainer.find('#viewresult').click(function(){	  
			//Kullanıcı sonuçları görmek isterse göster
			loader.fadeIn();
			$.get(BASE_URL + 'anket/ajax_results', '', function(data, status){
				pollcontainer.fadeOut(1000, function(){
					$(this).html(data);
					animateResults(this);
				});
				loader.fadeOut();
			});
			//durumunu koru
			return false;
		}).end()
		.find('#anket_form').submit(function(){
			var selected_val=$(this).find('input[@name=answer]:checked').val();
			if(selected_val){
				//Bir değer seçti ise değeri gönder
				loader.fadeIn();
				$.post(BASE_URL + 'anket/ajax_vote', $(this).serialize(), function(data, status){
					$('#pollcontainer').fadeOut(100, function(){
						$(this).html(data);
						animateResults(this);
						loader.fadeOut();
					});
				});
			} else { alert('Bir seçenek belirtiniz!') ;}
			//form durumunu koru
			return false;
		});
		loader.fadeOut();
	});
	
	function animateResults(data){
		$(data).find('.bar').hide().end().fadeIn('slow', function(){
							$(this).find('.bar').each(function(){
							    $(this).show();
								var bar_width=$(this).css('width');
								$(this).css('width', '0').animate({ width: bar_width }, 1000);
							});
		});
	}
	
  });
    
})(jQuery);
