var slideshow_pager_timeout;
var slideshowOffset = 0;

jQuery(document).ready(function(){
	jQuery('#slideshow_pager').hide();
	jQuery('.entry, .inner').not('#post-7 .entry').jScrollPane({scrollbarWidth:21, scrollbarMargin:10, dragMaxHeight: 51, dragMinHeight: 51, showArrows: false }) ;
	
	jQuery('img.aligncenter').parents('p').addClass('aligncenter');
	
	jQuery('.totop').click(function () {
		scroll(0,0);
		return false;
	});
	
	jQuery('label.hide, #login label').hide().each(function () {
		jQuery(this).next('input[type="text"], input[type="password"], textarea, select').val(jQuery(this).html());
		jQuery(this).parents('p').find('input[type="text"], input[type="password"], textarea, select').val(jQuery(this).html());
		jQuery(this).next('input[type="text"], input[type="password"], textarea, select').attr('rel',jQuery(this).html());
		jQuery(this).parents('p').find('input[type="text"], input[type="password"], textarea, select').attr('rel',jQuery(this).html());
		jQuery(this).hide();
	});
	jQuery('input[type="text"], input[type="password"], textarea').focus(function () {
		if (jQuery(this).val()==jQuery(this).attr('rel')) jQuery(this).val('');
		jQuery(this).removeClass('invalid');
		jQuery(this).addClass('focus');
		jQuery(this).parents('form').find('.error').hide();
	});
	jQuery('input[type="text"], input[type="password"], textarea').blur(function () {
		if (jQuery(this).val()=='') jQuery(this).val(jQuery(this).attr('rel'));
		jQuery(this).removeClass('focus');
	});
	
	
	try {		
		jQuery('#slideshow').after('<ul id="slideshow_pager"></ul><ul id="slideshow_pager_hide" class="hide"></ul><span title="Poprzedni" id="slideshow_prev"></span><span title="Następny" id="slideshow_next"></span>').before('<span title="Poprzedni" id="slideshow_bck"></span><span title="Następny" id="slideshow_fwd"></span>').cycle({ 
			fx:     'fade', 
			speed:  800, 
			timeout: 0, 
			pager:  '#slideshow_pager',
			next:  '#slideshow_fwd',
			prev:  '#slideshow_bck',
			pagerAnchorBuilder: function(idx, slide) {
				return '<li rel="'+jQuery(slide).index()+'"><a href="#" title=""><span class="title">'+jQuery(slide).attr('title')+'</span><img src="'+jQuery(slide).find('.thumb').attr('src')+'" class="thumb" /><img src="'+jQuery(slide).find('.thumb_active').attr('src')+'" class="thumb_active" /></a></li>'; 
			},
			after: slideOutCall,
			cleartypeNoBg: true
		});
		slideshowCnt = parseInt(jQuery('#slideshow_pager li').size()-1);
		if (slideshowCnt < 0) {
			jQuery('#slideshow_pager').append('<li rel="'+jQuery('#slideshow .slideshow').index()+'"><a><span class="title">'+jQuery('#slideshow .slideshow').attr('title')+'</span><img src="'+jQuery('#slideshow .slideshow').find('.thumb').attr('src')+'" class="thumb" /><img src="'+jQuery('#slideshow .slideshow').find('.thumb_active').attr('src')+'" class="thumb_active" /></a></li>');
		}		
		jQuery('#slideshow_pager li').each(function () {
			idx = jQuery(this).attr('rel');
			idx = jQuery(this).attr('rel');
			jQuery(this).css({'left':parseInt(128*idx)});
			if (idx > 6 ) {
				jQuery('#slideshow_pager_hide').append(jQuery(this));
			}
		});
		hoverInit();
		jQuery('#slideshow_pager a').find('.title').animate({ height: 'hide' }, 0);
		jQuery('#slideshow_pager_hide').hide();
		jQuery('#slideshow_prev').hide();
		var slideshowPrev = 0;
		var slideshowNext = 6;
		if (slideshowCnt <= slideshowNext) { jQuery('#slideshow_next').hide(); } else {jQuery('#slideshow_next').show();}
		
		jQuery('#slideshow_prev').click(function () {
			if ( slideshowPrev > 0) {
				jQuery('#slideshow_next').show();
				slideshowNextMove = jQuery('#slideshow_pager li[rel="'+(slideshowNext)+'"]');
				slideshowNext = slideshowNext-1;
				slideshowPrev = slideshowPrev-1;
				slideshowOffset = 6*(slideshowPrev);
				jQuery('#slideshow_pager').append(jQuery('#slideshow_pager_hide li[rel="'+(slideshowPrev)+'"]'));
				jQuery('#slideshow_pager').animate({ marginLeft: (-128)*(slideshowPrev)+'px' }, 200);
				setTimeout("jQuery('#slideshow_pager_hide').append(slideshowNextMove);",201);
				hoverInit();
				slideOutCall();
				if ( slideshowPrev == 0) {
					jQuery(this).hide();
				}
			} 
			//jQuery('#footer').html('slideshowCnt: '+slideshowCnt+', slideshowNext:'+slideshowNext+', slideshowPrev:'+slideshowPrev);
			return false;
		});
		
		jQuery('#slideshow_next').click(function () {
			if ( slideshowNext < slideshowCnt) {
				jQuery('#slideshow_prev').show();
				slideshowPrevMove = jQuery('#slideshow_pager li[rel="'+(slideshowPrev)+'"]');
				slideshowNext = slideshowNext+1;
				slideshowPrev = slideshowPrev+1;
				slideshowOffset = 5*(slideshowPrev);
				jQuery('#slideshow_pager').append(jQuery('#slideshow_pager_hide li[rel="'+(slideshowNext)+'"]'));
				jQuery('#slideshow_pager').animate({ marginLeft: (-128)*(slideshowPrev)+'px' }, 200);
				setTimeout("jQuery('#slideshow_pager_hide').append(slideshowPrevMove);",201);
				hoverInit();
				slideOutCall();
				if ( slideshowNext == slideshowCnt) {
					jQuery(this).hide();
				}
			} 
			//jQuery('#footer').html('slideshowCnt: '+slideshowCnt+', slideshowNext:'+slideshowNext+', slideshowPrev:'+slideshowPrev);
			return false;
		});
	} catch(err) {}	
	
	
		jQuery('.slideshow').click(function () {
			jQuery('#slideshow_fwd').click();
		});
		
	fancyboxInit();
	
	
	jQuery('#commentform').submit(function () {
		jQuery('.wpcf7-not-valid-tip').remove();
		jQuery('.wpcf7-response-output').remove()
		valid = true;
		emailFilter = /^([a-zA-Z0-9_.-])+@(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,4})+$/;
		jQuery('#commentform .validate').each(function () {
			if (jQuery(this).val()=='' || jQuery(this).val()==jQuery(this).attr('rel') || (jQuery(this).attr('id')=='email' && !emailFilter.test(jQuery(this).val()))) {
				valid = false;
				if (jQuery(this).attr('id')=='email' && !emailFilter.test(jQuery(this).val())) {
					if (jQuery(this).val()=='' || jQuery(this).val()==jQuery(this).attr('rel')) {
						jQuery(this).after('<span class="wpcf7-not-valid-tip hide">Proszę wypełnić wymagane pola.</span>');
					} else {
						jQuery(this).after('<span class="wpcf7-not-valid-tip hide">Adres e-mail wygląda na niepoprawny.</span>');
					}
				} else {
					jQuery(this).after('<span class="wpcf7-not-valid-tip hide">Proszę wypełnić wymagane pola.</span>');
				}
				jQuery(this).next('.wpcf7-not-valid-tip').animate({height:'show'},200);
				jQuery('.wpcf7-not-valid-tip').mouseover(function () {
					jQuery(this).animate({height:'hide'},200);
					setTimeout("jQuery(this).remove()",201);
				});
				jQuery(this).mouseover(function () {
					jQuery(this).next('.wpcf7-not-valid-tip').animate({height:'hide'},200);
					setTimeout("jQuery(this).next('.wpcf7-not-valid-tip').remove()",201);
				});
			}
		});
		if (valid) {
			jQuery('#commentform').append('<div class="wpcf7-response-output wpcf7-mail-sent-ok hide">Komentarz został dodany</div>');
			jQuery('.wpcf7-response-output').animate({height:'show'},200);
			setTimeout("jQuery('.wpcf7-response-output').hide(200)",5000);
			setTimeout("jQuery('.wpcf7-response-output').remove()",5201	);
			return true;
		} else {
			jQuery('#commentform').append('<div class="wpcf7-response-output wpcf7-validation-errors wpcf7-mail-sent-ng hide">Wystąpił błąd. Sprawdź poprawność pól i wyślij ponownie.</div>');
			jQuery('.wpcf7-response-output').animate({height:'show'},200);
			return false;
		}
		return valid;
	});
});


jQuery(window).load(function(){
	jQuery('#loader').remove();
	jQuery('.slideshow').each(function () {
		jQuery(this).append('<img src="'+jQuery(this).attr('rel')+'" alt="'+jQuery(this).attr('rel')+'" />');
	});
	itemShow('#slideshow','height',800,0);
	itemShow('#slideshow_pager li','width',800,0);
	fancyboxInit();
});

		
function itemShow(name, effect, duration, timeout) {
	setTimeout("jQuery('"+name+"').animate({"+effect+": 'show'}, "+duration+");",timeout);
}
function imgOver(){ 
	jQuery(this).animate({ marginTop: (-1)*17+'px' }, 200);
}
function imgOut(){ 
	jQuery(this).animate({ marginTop: 0 }, 200);
}
function slideOver(){
	clearTimeout(slideshow_pager_timeout);
	jQuery('#slideshow_pager a.current').removeClass('current');
	jQuery(this).addClass('current');
	idx = jQuery(this).parents('li').attr('rel');
	
	jQuery('#slideshow_pager a').each(function () {
		currIdx = jQuery('#slideshow_pager a.current').parents('li').attr('rel');
		idx = jQuery(this).parents('li').attr('rel');
		wdx = parseInt(125*jQuery(this).parents('li').attr('rel'))+slideshowOffset;
		if ( currIdx < idx ) { wdx = wdx + 24;	} 
		
		if (jQuery(this).hasClass('current')) {
			jQuery(this).parents('li').css({'z-index':2}).animate({ width: '150px', height: '106px', left: wdx}, 200);
			jQuery(this).animate({ width: '150px', height: '106px' }, 200);
			jQuery(this).find('.thumb_active').css({'z-index':2}).animate({ opacity: 'show' }, 400);
			jQuery(this).find('.thumb').css({'z-index':1}).animate({ width: '150px', height: '106px' },200);
			current = jQuery(this);
			setTimeout("current.find('.title').animate({ height: 'show'}, 200);",200);
		} else {
			jQuery(this).parents('li').css({'z-index':1}).animate({ width: '125px', height: '92px', left: wdx}, 200);
			jQuery(this).animate({ width: '125px', height: '92px' }, 200);
			jQuery(this).find('.thumb_active').css({'z-index':1}).animate({ opacity: 'hide' }, 200);
			jQuery(this).find('.thumb').css({'z-index':2}).animate({ width: '125px', height: '92px' }, 200);
			jQuery(this).find('.title').animate({ height: 'hide' }, 0);
		}
	});	
}

function slideOut(){ 
	jQuery(this).find('.title').animate({ height: 'hide' }, 0);
	slideshow_pager_timeout = setTimeout("slideOutCall()",10);
}
function slideOutCall(){ 
	jQuery('#slideshow_pager a.current').removeClass('current');
	jQuery('#slideshow_pager a').each(function () {
		wdx = parseInt(128*jQuery(this).parents('li').attr('rel'));
		jQuery(this).parents('li').css({'z-index':1}).animate({ width: '128px', height: '92px', left: wdx}, 200);
		jQuery(this).animate({ width: '128px', height: '92px' }, 200);
		jQuery(this).find('.thumb_active').css({'z-index':1}).animate({ opacity: 'hide' }, 200);
		jQuery(this).find('.thumb').css({'z-index':2}).animate({ width: '128px', height: '92px' }, 200);
		jQuery(this).find('.title').animate({ height: 'hide' }, 0);
	});
}

function fontsInit(){ 
}

function hoverInit(){ 
	jQuery('#slideshow_pager a').hoverIntent(slideOver,slideOut,{sensitivity: 1,timeout: 100});
}

function fancyboxInit(){ 
	jQuery('.fancybox').fancybox({
		'scrolling': 'no', 
		'titleShow': false, 
		'overlayOpacity':'.6',
		'overlayColor':'#000'//, 'onComplete': 
	});
}

