$(document).ready(function(){
    $('a[rel*=blank]').click(function(){
        window.open(this.href);
        return false;
    });
    
    $('a[href=#]').click(function(){
        return false;
    });
    
	// wywolanie rotatorow zdjec
	$('#right_galleries').Slideshow();
	$('#right_products').Slideshow();
    
    
	/*
     * 	Podpisy w polach input
     */
    $('.labeled').each(function(){
        $(this).focus(function(){
            if ($(this).val() == $(this).attr('rel')) {
				$(this).val('');
				$(this).addClass('active');
			}
        })
        $(this).blur(function(){
            if ($(this).val() == '') {
				$(this).val($(this).attr('rel'));
				$(this).removeClass('active');
			}
        })
    })
	
	$('#osrodki').simpleticker();
});

jQuery.fn.Slideshow = function(stoponhover){
	return this.each(function(stoponhover){
		// konfiguracja
		var duration = 3000;
		
		var $items = $(this).find('li').size();
		var $this = $(this);
		var pos = 0;
		
		if( $items<2 ) return false;
		
		function interval(){
            $this.data('int', setInterval(function(){
                if (pos >= $items - 1) {
                    pos = -1;
                }
                slide(++pos);
            }, duration));
        };
		
		// obsluga przelaczania
        function slide(show){
            // ukrycie aktualnego slajdu
            $this.find('li:visible').fadeOut(function(){
                // pokazanie wywolanego slajdu
                $this.find('li:eq('+show+')').fadeIn();
            });
        }
		
		if (stoponhover) {
			// obsluga hovera na slajdach
			$this.hover(function(){
				clearInterval($this.data('int'));
			}, function(){
				interval()
			});
		}
		interval();
	})
}

jQuery.fn.simpleticker = function( options ){
	// dimyslna konfiguracja
	var settings = {
		'duration' : 5000
	};
	
	return this.each(function(){
		if(options) $.extend(settings,options);
		
		var step = $(this).height();
		var content = $(this).find('.content'); 
		var items = content.find('a').size();
		
		if (items > 1) {
			var nextpos = content.css('margin-top').replace('px', '') - step;
			
			function interval(){
				inte = setInterval(function(){
					content.css('margin-top', nextpos + 'px');
					if (-nextpos < content.height() - step) 
						nextpos = nextpos - step;
					else {
						nextpos = 0;
					}
				}, settings.duration);
			};
			
			$(this).hover(function(){
				clearInterval(inte);
			}, function(){
				interval()
			});
			
			interval();
		}
    });
};
