(function($){
  $.fn.extend({
    center: function (x,y) {
      // var options =  $.extend({transition:300, minX:0, minY:0}, options);
      return this.each(function() {
                if (x == undefined) {
                    x = true;
                }
                if (y == undefined) {
                    y = true;
                }
                var $this = $(this);
                var $window = $(window);
                $this.css({
                    position: "absolute",
                });
                if (x) {
                    var left = ($window.width() - $this.outerWidth())/2+$window.scrollLeft();
                    $this.css('left',left)
                }
                if (!y == false) {
            var top = ($window.height() - $this.outerHeight())/2+$window.scrollTop();   
                    $this.css('top',top);
                }
        // $(this).animate({
        //   top: (top > options.minY ? top : options.minY)+'px',
        //   left: (left > options.minX ? left : options.minX)+'px'
        // }, options.transition);
        return $(this);
      });
    }
  });
})(jQuery);


$(document).ready(function() {
	// ajax transition effect
	jQuery.extend({
		nette: {
			updateSnippet: function (id, html) {
				$("#" + id).fadeTo("fast", 0.3, function () {
					$(this).html(html).fadeTo("fast", 1);
					$.nette.registerAfterUpdate();
				});
			},

			registerAfterUpdate: function() { }
		}
	});

	$("#logo, #sphere").live("click", function () {
		document.location.href = "index.php";
	});

	$("a[rel^='prettyPhoto']").prettyPhoto({
		theme: 'light_rounded',
		modal: true,
		wmode: 'opaque',
		hideflash: true,
		showTitle: false,
		allowresize: true
	});
	
	$("body").live("click", function(){
		$.prettyPhoto.close();
	});

	// slowmotion rozbalovani dropdown menu --------------------------------------
	var toggle = function(direction, display) {
		return function() {
			var self = this;
			var ul = $("ul", this);
			if (ul.css("display") == display && !self["block" + direction]) {
				self["block" + direction] = true;
				ul["slide" + direction](100, function() {
					self["block" + direction] = false;
				});
			}
		};
	}
  
	$("li.hassubmenu").hover(toggle("Down", "none"), toggle("Up", "block"));
	$("li.hassubmenu ul").hide();
  
  
	// flashes
	$("div.flash").livequery(function () {
		var el = $(this);
		el.center();

		
		setTimeout(function () {
			el.animate( {
				"opacity": 0
			}, 1000);
			el.slideUp();
		}, 3000);
	});
	
	
	$("#shopping_cart").live("click", function () {
		document.location.href = $(this).find("a").attr("href");
	});
	
	
	// form send
	$("#frm-basket-orderForm").submit(function () {
		$(this).ajaxSubmit();
		return false;
	});
	// form button send
	$("#frm-basket-orderForm :submit").click(function () {
		$(this).ajaxSubmit();
		return false;
	}); 
	
});
