// center object
jQuery.fn.center = function(){
    this.css("position","absolute");
    this.css("top", ( $(window).height() - this.height() ) / 2+$(window).scrollTop() + "px");
    this.css("left", ( $(window).width() - this.width() ) / 2+$(window).scrollLeft() + "px");
    return this;
}


// pridanie priesvitneho pozadia example: $.overlay(200, 0.5);
jQuery.overlay = jQuery.fn.overlay = function(speed,opacity){
    speed = typeof(speed) != 'undefined' ? speed : 0;
    opacity = typeof(opacity) != 'undefined' ? opacity : 0.5;
    if($("#overlayShadow").length==0){
	$('body').append('<div id="overlayShadow" style="background-color:#000;opacity:0;position:fixed;left:0;right:0;top:0;bottom:0;"></div>');
    }
    $("#overlayShadow").fadeTo(speed,opacity);
    return this;
}

// odstranenie priesvitneho pozadia, example: $.overlayHide(200);
jQuery.overlayHide = jQuery.fn.overlayHide = function(speed){
    speed = typeof(speed) != 'undefined' ? speed : 200;
    $("#overlayShadow").fadeOut(speed,function(){$(this).remove()});
    return this;
}


// AutoScroll preskroluje na triedu ktoru chcem $('.area_name').autoscroll();
jQuery.fn.autoscroll = function(selector){
    $('html,body').animate({ scrollTop:$(selector).offset().top }, 500);
}


// log jQuery events using Firebug and Firefox
// Usage: $('#someDiv').hide().log('div hidden').addClass('someClass');
jQuery.log = jQuery.fn.log = function(msg){
      if (console) {
         console.log("%s: %o", msg, this);
      }
      return this;
};


// ajax checking of avaibality of form value
//   run ajax call on keyUp() to url stored in rel attr of every matched element
//   according to return value from server renders messages next to matched element
jQuery.fn.checkAvaibality = function(){
	var xhr   = null;
	var timer = null;

	$(this).keyup(function(){
		var $this = $(this);

		if ($this.val() == '') {
			if (timer !== null)
				clearTimeout(timer);

			return false;
		}

		if (xhr !== null)
		  xhr.abort();

		if (timer !== null)
		  clearTimeout(timer);

		timer = setTimeout(
			function(){
				$this.after('<div class="spinner '+$this.attr('class')+'"></div>');

				var url = $this.attr('rel') + '/name/' + $this.val();

				xhr = $.getJSON(url, function(data){
					$this.siblings('.spinner.'+$this.attr('class')+', .spinner.txt').remove();
					if (!data) {
						$this.css({border:'1px solid #979797'});
					}
					else {
						$this.css({border:'1px solid #F55'});
						$this.after('<span class="spinner txt">Try another!</span>');
					}
				});
			},
			750
		);

	});
};




// input clearing
// How to use it: $('input').clearDefault();
(function($){
    $.fn.clearDefault = function(){
        return this.each(function(){
            var default_value = $(this).val();
            $(this).focus(function(){
                if ($(this).val() == default_value)
                             $(this).val("");
            });
            $(this).blur(function(){
                if ($(this).val() == "")
                              $(this).val(default_value);
            });
        });
    };
})(jQuery);


// Common binds/effects
$(function(){

	// flashmessages
	$flashmessage = $('.flashmessage');
	if($flashmessage.length){
		$flashmessage.center().fadeIn(500).delay($flashmessage.html().length * 50).fadeOut(1000);
	}


	// logout link
	var $fade = $('#logouttext');
	var oldLogoutCode = $fade.html();
	$('#logout').hover(
		function(){
			$(this).removeAttr('title');
			if ($fade.is(':animated')) { $fade.stop().fadeTo('fast', 1, fillLogout); }
			else { $fade.fadeOut('fast', fillLogout); }

			function fillLogout(){ $fade.html('Logout').fadeIn('fast'); }
		},
		function(){
			$(this).attr('title', 'Logout');
			if ($fade.is(':animated')) { $fade.stop().fadeTo('fast', 1, fillOld); }
			else { $fade.fadeOut('fast', fillOld); }

			function fillOld(){ $fade.html(oldLogoutCode).fadeIn('fast'); }
		}
	);

});

