openGallery=0;

Cufon.replace('.cufon, #main-menu, .domki .title div', {hover:true});
Cufon.replace('.cufon.title', { textShadow: '#555 1px 2px' } );

$(document).ready(function () {

	// if ( $.browser.msie ) { $('#top .tekst ').css({ 'filter':'glow(color=#000,strength=3)', 'zoom':'1' }); }
	
	$('#content .text p img').each(function() {
		$(this).wrap('<a href="'+$(this).attr('src')+'" rel="photoViewer[gal2]" title="" />');
	});
	
	$('a[rel^="photoViewer"]').prettyPhoto({theme: 'facebook', social_tools: '', deeplinking:false, show_title:false });
	$('a[rel^="photoViewerOne"]').prettyPhoto({theme: 'facebook', social_tools: '', deeplinking:false, show_title:false, markup: '<div class="pp_pic_holder"><div class="ppt">&nbsp;</div><div class="pp_top"><div class="pp_left"></div><div class="pp_middle"></div><div class="pp_right"></div></div><div class="pp_content_container"><div class="pp_left"><div class="pp_right"><div class="pp_content"><div class="pp_loaderIcon"></div><div class="pp_fade"><a href="#" class="pp_expand" title="Expand the image">Expand</a><div id="pp_full_res"></div><div class="pp_details"><p class="pp_description"></p><div class="pp_social">{pp_social}</div><a class="pp_close" href="#">Close</a></div></div></div></div></div></div><div class="pp_bottom"><div class="pp_left"></div><div class="pp_middle"></div><div class="pp_right"></div></div></div><div class="pp_overlay"></div>',gallery_markup:''});
	
	// // $('#slider').coinslider({width: 990, height: 250, navigation: false, links: false});
	
	$('#cycle').cycle({ 
		timeout: 3000,
		pause: 1
	});	

	$('.carousel .content div').jCarouselLite({ 
			btnPrev:   '.carousel .prev', 
			btnNext:   '.carousel .next', 
			speed: 500,
			visible: 1,
			auto: 10000
	});
	
	$('#top .news').hover(
		function() { $('#top .pic .prev, #top .pic .next').show(); },
		function() { $('#top .pic .prev, #top .pic .next').hide(); }
	).jCarouselLite({ 
			btnPrev:   '#top .pic .prev', 
			btnNext:   '#top .pic .next', 
			speed: 300,
			visible: 1,
			afterEnd: function() { Cufon.replace('#top h3'); $('#top .text .jscroll').jScrollPane(); },
			auto: 10000
	});
	
	$('#top .text .jscroll').each( function() {
		$(this).jScrollPane({verticalGutter:10, autoReinitialise:true, showArrows:true});
	});

	if ( $('.members').length ) {
		$(document).click( function(event) { 
			if(!$(event.target).is('.members *')) $('.members .slide').slideUp(500, 'easeInOutCirc'); 
		});
		$('.members .button').live( 'click', function(event) {
			$('.members .slide').slideToggle(500, 'easeInOutCirc');
		});
	}
	
	$('#logowanie').live( 'submit', function() {
		// $('.members').load( '/ .members>*', $(this).serializeArray() );
		$.post( $(this).attr('action'), $(this).serializeArray(), function(data) { 
			if ( ! $(data).find('.members .main').length )
				$('.members').html( $(data).find('.members').html() );
			else 
				window.location.href = '/kalendarz,show_slide';
		});
		return false;
	});
	$('.members .live').live( 'click', function() {
		$('.members').load( $(this).attr('href')+' .members>*', $(this).serializeArray() );
		return false;
	});
	
	
	$('.news-list li:odd').addClass('odd');
	
/*
		$('#main-menu>ul>li').hover(
			function(){
				$(this).data('hover', 1);
				if ($(this).find('.submenu').length) {

					if ($(this).attr('h') == undefined) {
						pos = $(this).position();
						var left = pos.left-2;
						// var top = pos.top + $(this).height() / 2; 
						var top = pos.top + 33; 
						var width = $(this).outerWidth(); 
						$(this).attr('h', $(this).find('.submenu').height()+50);
						var height = $(this).attr('h'); 
						$(this).css({ '-webkit-border-bottom-right-radius': 0, '-webkit-border-bottom-left-radius': 0, '-moz-border-radius-bottomright': 0, '-moz-border-radius-bottomleft': 0, 'border-bottom-right-radius': 0, 'border-bottom-left-radius': 0 })
							   .find('.submenu').css( { "left":left, "top":top, "width":width, height:height } )
					}
					
					$(this).addClass('hover').find('.submenu').stop(true,true).slideDown( 500, "easeInSine" );
				} else {
					$(this).addClass('hover');
				}
				
			},
			function(){
				$(this).data('hover', 0);
				if ($(this).find('.submenu').length) {
					$(this).find('.submenu').stop(true,true).slideUp( 300, "easeOutSine", function() { 
						if ( ! $(this).closest('li').data('hover') ) {
							$(this).closest('li').removeClass('hover');
						}
					} );
				} else {
					$(this).removeClass('hover');
				}
				
			}
		);
*/		
		$('#main-menu>ul>li').hover(
			function(){
				if ($(this).find('.submenu').length) { $(this).css({ '-webkit-border-bottom-right-radius': 0, '-webkit-border-bottom-left-radius': 0, '-moz-border-radius-bottomright': 0, '-moz-border-radius-bottomleft': 0, 'border-bottom-right-radius': 0, 'border-bottom-left-radius': 0 }); }
				pos = $(this).position();
				$(this).addClass('hover');
				$(this).find('.submenu').css( { "left":pos.left } ).show();
			},
			function(){
				$(this).find('.submenu').hide();
				$(this).removeClass('hover');
			}
		);
		
		
	// KALNDEARZ jquery ui tabs, tooltip
	
	var d=new Date();
	sel_day = d.getDay(); sel_day=sel_day-1; if (sel_day<0) sel_day=6;
	$('.tabs').tabs( {
		create: function(event, ui) { Cufon.replace($(this).find('.cufon')); },
		select: function(event, ui) { Cufon.replace($(this).find('.cufon')); },
		show: function(event, ui) { Cufon.replace($(this).find('.cufon')); },
		selected: sel_day
	});
	
	$('.tabs .cufon a').hover( function() { $(this).addClass('hover'); Cufon.replace($(this).closest('.cufon')); } , function() { $(this).removeClass('hover'); Cufon.replace($(this).closest('.cufon')); } );
	$('.tabs .cufon').mousemove( function() { Cufon.replace($(this).closest('.cufon')); } )
	
	$('span.ex').tooltip( { track: true, fade: 250, extraClass: 'tooltip', left:-250 } );
	
	// ---- end --- KALENDARZ
	
	// GALERIA
	
	$('div.navigation').css({'width' : '300px', 'margin' : '0 auto'});
	$('#gallery').css('display', 'block');
	
	var onMouseOutOpacity = 0.67;
	$('ul.thumbs li').opacityrollover({
			mouseOutOpacity:   onMouseOutOpacity,
			mouseOverOpacity:  1.0,
			fadeSpeed:         'fast',
			exemptionSelector: '.selected'
	});
	
	if ( $('#thumbs li').length ) {
				var gallery = $('#thumbs').galleriffic({
					delay:                     2500,
					numThumbs:                 15,
					preloadAhead:              10,
					enableTopPager:            true,
					enableBottomPager:         true,
					maxPagesToShow:            7,
					imageContainerSel:         '#slideshow',
					controlsContainerSel:      '#controls',
					captionContainerSel:       '#caption',
					loadingContainerSel:       '#loading',
					renderSSControls:          true,
					renderNavControls:         true,
					playLinkText:              'Pokaz slajdów - Start',
					pauseLinkText:             'Pokaz slajdów - Pausa',
					prevLinkText:              '&lsaquo; Poprzednie',
					nextLinkText:              'Następne &rsaquo;',
					nextPageLinkText:          'Dalej &rsaquo;',
					prevPageLinkText:          '&lsaquo; Wstecz',
					enableHistory:             false,
					autoStart:                 false,
					syncTransitions:           true,
					defaultTransitionDuration: 900,
					onSlideChange:             function(prevIndex, nextIndex) {
						// 'this' refers to the gallery, which is an extension of $('#thumbs')
						this.find('ul.thumbs').children()
							.eq(prevIndex).fadeTo('fast', onMouseOutOpacity).end()
							.eq(nextIndex).fadeTo('fast', 1.0);
					},
					onPageTransitionOut:       function(callback) {
						this.fadeTo('fast', 0.0, callback); wyrownaj();
					},
					onPageTransitionIn:        function() {
						this.fadeTo('fast', 1.0);
					}
				});
	}

	
	// ---- end --- GALERIA
	
	$("a.newWindow, .reklama a").click(function() {
	   window.open(this.href);
	   return false;
	});
	$('a[rel^="newWindow"]').click(function() {
	   window.open(this.href);
	   return false;
	});
	// DOWNLOAD linki
	$(".download-content-list a").click(function() {
	   window.open(this.href);
	   return false;
	});

	
	$("a.jqbookmark").click(function(event){
		event.preventDefault(); // prevent the anchor tag from sending the user off to the link
		var url = this.href;
		var title = this.title;

		if (window.sidebar) { // Mozilla Firefox Bookmark
			window.sidebar.addPanel(title, url,"");
		} else if( window.external ) { // IE Favorite
			window.external.AddFavorite( url, title);
		} else if(window.opera) { // Opera 7+
			return false; // do nothing - the rel="sidebar" should do the trick
		} else { // for Safari, Konq etc - browsers who do not support bookmarking scripts (that i could find anyway)
			 alert('Niestety, ta przeglądarka nie obsługuje żądanej akcji,'
			 + ' proszę dodać stronę do zakładek ręcznie.');
		}
	});
	
	$("form.sprawdz, form.newsletter").submit(function(){
        // na początku zakładamy, że błędu nie ma
        var error_form=false;

        $(this).find("input,textarea,select").each(function(){
            // id elementu formularza
            var id=$(this).attr("id");

            // szukamy treść <label> powiązanego z tym elementem
            var tresc_label=$("label[for='"+id+"']").text();
            // czy ta treść zawiera gwiazdkę?
            var szukane_reg=new RegExp("\\*","i");
            if(szukane_reg.test(tresc_label)){
                // OK, ten element formularza musi być wypełniony
                if( ($(this).is("input") && $(this).val()=="") ||
                    ($(this).is("textarea") && $(this).val()=="") ||
                    ($(this).is("select") && $(this).val()=="none")) {
                        $(this).addClass("error");
                        error_form=true;
                } else {$(this).removeClass("error");}

                // A jeśli element to checkbox, to zaznaczamy jego <label> jako error
                if ($(this).attr("type")=="checkbox" && $("input#"+id+":checked").val()===undefined){
                    $("form.sprawdz label[for="+id+"]").addClass("error");
                } else {$("form.sprawdz label[for="+id+"]").removeClass("error");}
            }
        });
        return !error_form;
    });
	
	$("#newsletter .submit").live('click', function(){ 
		$("#addingToNewsletter").text("Proszę czekać...");
		var  mail = encodeURI($('#newsletter input:[name=mail]').val());
		$.ajax({type: "POST",url: "newsletter/php/add_mail.php",data: ({mail : mail}),success: function(data){ $('#addingToNewsletter').html(data).show(); }});
		return false;
	});	
	
	wyrownaj();
	
});

function wyrownaj() {
    var menu=$("#content .menu").height();
    var content=$("#content .content").height();
    var max=Math.max(menu,content);
	if (max!=content) $("#content .content").css("min-height",max);
	else $("#content .menu").css("min-height",max);
	
	if ( $('.tabs').length ) $('.tabs').height( max - $('.tabs').position().top );
}
