/**
 * AJAX Nette Framwork plugin for jQuery
 *
 * @copyright  Copyright (c) 2009, 2010 Jan Marek
 * @copyright  Copyright (c) 2009, 2010 David Grudl
 * @license    MIT
 * @link       http://nette.org/cs/extras/jquery-ajax
 */

/*
if (typeof jQuery != 'function') {
	alert('jQuery was not loaded');
}
*/

    function dots(sel) {
            val = $(sel).text();
            
            if(val == "") val = ".";
            else if(val == ".") val = "..";
            else if(val == "..") val = "...";
            else if(val == "...") val = "";

            $(sel).text(val);
    }

    function showLoading() {

        intval = setInterval(function() {

            $(".loading").show();
            dots('.dots');
            dots('.dots2');
           
            
        }, 200);

       return intval;

    }

    function stopLoading(interval) {

        clearInterval(interval);
        $("#loading").hide();

    }




$.fn.clearForm = function() {
return this.each(function() {
var type = this.type, tag = this.tagName.toLowerCase();
if (tag == 'form')
return $(':input',this).clearForm();
if (type == 'text' || type == 'password' || tag == 'textarea')
this.value = '';
else if (type == 'checkbox' || type == 'radio')
this.checked = false;
else if (tag == 'select')
this.selectedIndex = -1;
});
};

function is_int(input){
    return !isNaN(input)&&parseInt(input)==input;
}

jQuery.fn.extend({
	ajaxSubmit: function (callback) {
		var form;
		var sendValues = {};

		// submit button
		if (this.is(":submit")) {
			form = this.parents("form");
			sendValues[this.attr("name")] = this.val() || "";

		// form
		} else if (this.is("form")) {
			form = this;

		// invalid element, do nothing
		} else {
			return null;
		}

		// validation
		if (form.get(0).onsubmit && !form.get(0).onsubmit()) return null;

		// get values
		var values = form.serializeArray();

		for (var i = 0; i < values.length; i++) {
			var name = values[i].name;

			// multi
			if (name in sendValues) {
				var val = sendValues[name];

				if (!(val instanceof Array)) {
					val = [val];
				}

				val.push(values[i].value);
				sendValues[name] = val;
			} else {
				sendValues[name] = values[i].value;
			}
		}

		// send ajax request
		var ajaxOptions = {
			url: form.attr("action"),
			data: sendValues,
			type: form.attr("method") || "get"
		};

		if (callback) {
			ajaxOptions.success = callback;
		}

		return jQuery.ajax(ajaxOptions);
	}
});

var intval;

(function($) {

	$.nette = {
		success: function(payload)
		{
			// redirect
			if (payload.redirect) {
				window.location.href = payload.redirect;
				return;
			}

			// state
			if (payload.state) {
				$.nette.state = payload.state;
			}

			// snippets
			if (payload.snippets) {
				for (var i in payload.snippets) {
					$.nette.updateSnippet(i, payload.snippets[i]);
				}
			}
		},

		updateSnippet: function(id, html)
		{
			$("#" + id).fadeTo("fast", 0.01, function () {
          $(this).html(html).fadeTo("slow", 1);
      });
		},

		// create animated spinner
		createSpinner: function()
		{
                  
			return this.spinner = $('<div id="ajax-spinner"></div>').ajaxStart(function() {
            
           // loading start here, sample: $(this).show();       

			}).ajaxStop(function() {

                           $(this).hide();

			}).appendTo('body').hide();
		},

		// current page state
		state: null,

		// spinner element
		spinner: null
	};


})(jQuery);



jQuery(function($) {

	$.ajaxSetup({
		success: $.nette.success,
		dataType: 'json'
	});

	$.nette.createSpinner();

	// apply AJAX unobtrusive way
	$('a.ajax').live('click', function(event) {
		event.preventDefault();
		if ($.active) return;
           
                /*$("#ajax-spinner").show().css({
                    left: $(this).position().left + 20,
                    top: $(this).position().top + $(this).height()/2 - 8
                });*/

		$.post(this.href, $.nette.success);

	});

  // odeslání na formulářích
  $("form.ajax").live("submit", function () {
    
         $(this).ajaxSubmit();
         
         /*var width = $(this).position().left + $(this).width() - 50;
         var height = $(this).position().top + 20;
         
         $("#ajax-spinner").show().css({
                left: width,
                top:  height
         });*/

         return false;
  });
  
  $(".tipster_statistics_select").change(function() {
      
      if($(this).val() != 0) {
      
          $.get("?do=applyFilter", {"value": $(this).val()}, function(payload) {

               $.nette.success(payload);    

               return false;
          });
      
      }
  });


});
