$(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();
	
	// obsluga kalendarza
	$.datepicker.setDefaults({
		closeText: 'Zamknij',
		prevText: '&#x3c;Poprzedni',
		nextText: 'Następny&#x3e;',
		currentText: 'Dziś',
		monthNames: ['Styczeń', 'Luty', 'Marzec', 'Kwiecień', 'Maj', 'Czerwiec', 'Lipiec', 'Sierpień', 'Wrzesień', 'Październik', 'Listopad', 'Grudzień'],
		monthNamesShort: ['Sty', 'Lu', 'Mar', 'Kw', 'Maj', 'Cze', 'Lip', 'Sie', 'Wrz', 'Pa', 'Lis', 'Gru'],
		dayNames: ['Niedziela', 'Poniedzialek', 'Wtorek', 'Środa', 'Czwartek', 'Piątek', 'Sobota'],
		dayNamesShort: ['Nie', 'Pn', 'Wt', 'Śr', 'Czw', 'Pt', 'So'],
		dayNamesMin: ['N', 'Pn', 'Wt', 'Śr', 'Cz', 'Pt', 'So'],
		dateFormat: 'yy-mm-dd',
		firstDay: 1,
		showOtherMonths: true,
		isRTL: false
	});
    $.ajax({
        url: "/ajax/",
        dataType: "json",
        success: function(calendarEvents){
            $('.calendarwidget').datepicker({
                numberOfMonths: [1, 1],
                showCurrentAtPos: 0,
                beforeShowDay: function(date){
                    for (i = 0; i < calendarEvents.length; i++) {
                        if (date.getMonth() == calendarEvents[i][0] - 1 &&
                        date.getDate() == calendarEvents[i][1] &&
                        date.getFullYear() == calendarEvents[i][2]) {
                            return [true, 'ui-state-active', 'Zobacz imprezy i wydarzenia dla tego dnia'];
                        }
                    }
                    return [false, 'ui-state'];//enable all other days
                },
                onSelect: function(outd, $this){				
					Shadowbox.open({
						player: "iframe",
						title: '',
						content: "/events-" + outd,
						width: 600,
						height: 400
					});
                }
            });
        }
    });
    
    
	/*
     * 	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');
			}
        })
    })

});

jQuery.fn.Slideshow = function(stoponhover){
	return this.each(function(stoponhover){
		// konfiguracja
		var duration = 5000;
		
		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();
	})
}
