jQuery(document).ready(function(){
	offset = 0;

	jQuery('#top .menu li').each(function () {
		offset = offset + jQuery(this).width()+30;
	});	
	offset = jQuery('#top .menu').width() - offset;
	if ( offset > 0) {
		jQuery('#top .menu li:first').css({'margin-left':parseInt(offset/2+90)+'px'});	
	}

	timeOut = 200;	
	
	try {
		Cufon.replace('h1,.sidebar h2, .content_home h2, .content_home h3', { fontFamily: 'Square721BT' });
		Cufon.replace('.menu a', { fontFamily: 'Square721BT', hover: true });
	} catch(err) {}
	
	jQuery('.totop').click(function () {
		scroll(0,0);
		return false;
	});	
	
	jQuery('.entry_team').each(function () {
		jQuery('#'+jQuery(this).parents('.post').attr('id')+'-sm').addClass('current_page_item');
		return false;
	});	
	
	if(jQuery.cookie('header1')) { h1 = parseInt(jQuery.cookie('header1')); } else { h1 = 0; jQuery.cookie('header1',0); }
	if(jQuery.cookie('header2')) { h2 = parseInt(jQuery.cookie('header2')); } else { h2 = 0; jQuery.cookie('header2',0); }
	if(jQuery.cookie('header3')) { h3 = parseInt(jQuery.cookie('header3')); } else { h3 = 0; jQuery.cookie('header3',0); }
	if(jQuery.cookie('header4')) { h4 = parseInt(jQuery.cookie('header4')); } else { h4 = 0; jQuery.cookie('header4',0); }
	if(jQuery.cookie('header5')) { h5 = parseInt(jQuery.cookie('header5')); } else { h5 = 0; jQuery.cookie('header5',0); }
	if(jQuery.cookie('header')) { hlast = parseInt(jQuery.cookie('header')); } else { hlast = 0; h = 0; jQuery.cookie('header',0); }
	ha = new Array(h1,h2,h3,h4);
	h = aRand(4,ha,hlast);
	
	// try {
		// jQuery('#header_slider').cycle({
			// fx:     'fade',
			// speed:  0,
			// timeout: 0,
			// startingSlide: h,
			// cleartypeNoBg: true
		// });
	// } catch(err) {}
	
	try {
		jQuery('#slides').before('<div id="slider_nav"></div><a href="next" id="slider_next"></a><a href="prev" id="slider_prev"></a>').cycle({ 
			fx:     'scrollHorz', 
			speed:  timeOut*3, 
			timeout: 10000, 
			prev:  '#slider_prev', 
			next:  '#slider_next',
			after:  function(currSlideElement, nextSlideElement, options, forwardFlag) {
				var posl = (-1)*options.currSlide*1140;
				jQuery('#home').css({ 'background-position': posl+'px 0' });
				jQuery('.column_featured').removeClass('column_featured');
				jQuery('.slide-'+options.currSlide).addClass('column_featured');
			},
			cleartypeNoBg: true, 
			pager:  '#slider_nav'
		});
	} catch(err) {}
	
	try {
		if (jQuery('#news_list_paged .news_list_page').size() > 1) {
			jQuery('#news_list_paged').after('<div id="news_list_paged_wrap"><div id="news_list_paged_center"><a href="prev" id="news_list_prev" /><div id="news_list_nav" /><a href="next" id="news_list_next" /></div></div>')
			jQuery('#news_list_paged').cycle({ 
				fx:     'fade', 
				speed:  timeOut*3, 
				timeout: 0, 
				prev:  '#news_list_prev', 
				next:  '#news_list_next', 
				pager: '#news_list_nav',
				cleartypeNoBg: true
			});
		}
	} catch(err) {}
	
	jQuery('label.hide').each(function () {
		jQuery(this).next('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).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('active');
	});
	jQuery('input[type="text"], input[type="password"], textarea').blur(function () {
		if (jQuery(this).val()=='') jQuery(this).val(jQuery(this).attr('rel'));
		jQuery(this).removeClass('active');
	});
	
	
	if ( jQuery.browser.msie ) {
		if (jQuery.browser.version == '6.0') {
		}
		if (jQuery.browser.version == '7.0') {
		}
	} 	
	if ( /chrome/.test( navigator.userAgent.toLowerCase() ) ) {
	} 	
	if ( /safari/.test( navigator.userAgent.toLowerCase() ) ) {
	} 
	if ( jQuery.browser.opera ) {
	} 	
	
	jQuery('.filter_year').each(function () {
		if (!jQuery('#filter_year option[value="'+jQuery(this).html()+'"]').size() > 0) {
			jQuery('#filter_year').append('<option value="'+jQuery(this).html()+'">'+jQuery(this).html()+'</option>');
		}
		jQuery('#filter_year').val('%');
	});
	jQuery('#filter_year').change(function () {
		if (jQuery('#filter_year :selected').size() == jQuery('#filter_year option').size() ) {
			jQuery(this).find('option').removeAttr('selected');
			jQuery(this).val(jQuery(this).find('option').first().attr('value'));
		}
		if (jQuery('#filter_year :selected').size() > 1 ) {
			jQuery(this).find('option').first().removeAttr('selected');
		}
	});	
	
	jQuery('.filter_country').each(function () {
		if (!jQuery('#filter_country option[value="'+jQuery(this).html()+'"]').size() > 0) {
			jQuery('#filter_country').append('<option value="'+jQuery(this).html()+'">'+jQuery(this).html()+'</option>');
		}
		jQuery('#filter_country').val('%');
	});
	jQuery('#filter_country').change(function () {
		if (jQuery('#filter_country :selected').size() == jQuery('#filter_country option').size() ) {
			jQuery(this).find('option').removeAttr('selected');
			jQuery(this).val(jQuery(this).find('option').first().attr('value'));
		}
		if (jQuery('#filter_country :selected').size() > 1 ) {
			jQuery(this).find('option').first().removeAttr('selected');
		}
	});	
	
	jQuery('#filter_amount').each(function () {
		jQuery(this).val(jQuery(this).find('option').first().attr('value'));
	});
	jQuery('#filter_amount').change(function () {
		if (jQuery('#filter_amount :selected').size() == jQuery('#filter_amount option').size() ) {
			jQuery(this).find('option').removeAttr('selected');
			jQuery(this).val(jQuery(this).find('option').first().attr('value'));
		}
		if (jQuery('#filter_amount :selected').size() > 1 ) {
			jQuery(this).find('option').first().removeAttr('selected');
		}
	});	
	// jQuery('#filter_amount option').click(function () {
		// jQuery(this).parents('select').removeAttr('selected');
		// jQuery(this).parents('select').val(jQuery(this).val());
	// });	
	// jQuery('#filter_amount option').mouseup(function () {
		// jQuery(this).parents('select').removeAttr('selected');
		// jQuery(this).parents('select').val(jQuery(this).val());
	// });	

	jQuery('#filter_amount_min').each(function () {
		jQuery(this).val(jQuery(this).find('option').first().attr('value'));
	});
	jQuery('#filter_amount_min').change(function () {
		currentVal = parseInt(jQuery(this).val());		
		jQuery('#filter_amount_max option').each(function () {
			(jQuery(this).attr('value') > currentVal || jQuery(this).attr('value') < 0 || currentVal < 0) ? jQuery(this).show() : jQuery(this).hide();
		});
	});	
	jQuery('#filter_amount_max').each(function () {
		jQuery(this).val(jQuery(this).find('option').last().attr('value'));
	});
	jQuery('#filter_amount_max').change(function () {
		currentVal = parseInt(jQuery(this).val());
		jQuery('#filter_amount_min option').each(function () {
			(jQuery(this).attr('value') < currentVal || jQuery(this).attr('value') < 0 || currentVal < 0) ? jQuery(this).show() : jQuery(this).hide();
		});
	});
	
	try {
		var ss = 0;
		(jQuery.cookie('currSlide')) ? ss = jQuery.cookie('currSlide') : ss = 0;
		jQuery('#references_page').cycle({
			fx:     'fade',
			speed:  400,
			timeout: 0,
			prev: '#references_page_prev',
			next: '#references_page_next',
			cleartypeNoBg: true,
			startingSlide: ss,
			after:  function(currSlideElement, nextSlideElement, options, forwardFlag) {
				jQuery('#references_page_page').html(parseInt(options.currSlide+1)+' / '+parseInt(options.slideCount));
			}
		});
		jQuery('#references_page').height(jQuery('.post_thumb_page:first').height());
	} catch(err) {}
	
	jQuery('#top .menu li').show();
	
	// jQuery('.post_thumb .hide').each(function () {
		// cVal = '';
		// jQuery(this).find('span').each(function () {
			// cVal = cVal+'-'+jQuery(this).html().replace('000000','')+'-';
		// });
		// cVal = cVal.replace('0--10-','poniżej 10-').replace('100--300-','powyżej 100-');
		// jQuery(this).parents('.post_thumb').prepend('<span style="float:left; margin: 0 0 -15px; position: relative; z-index: 9;">'+cVal+'</span>');
	// });
});

jQuery(window).load(function(){
	itemShow('#top','height',400,0);
	itemShow('#content','height',800,401);
	itemShow('#footer','height',400,1202);
	setTimeout("initThumbNav();",1202);
});

function initFancybox() {
	jQuery('a.fancybox').fancybox({
		'overlayOpacity': '0.7',
		'overlayColor': '#000',
		'titleShow'  : false,
		onComplete: function(currentArray, currentIndex){ 
			jQuery("#fancybox-title-main").prepend('<strong id="fancybox-counter"><span>'+(currentIndex+1)+'</span>/'+currentArray.length+'.&nbsp;</strong>');
		},
		onCleanup: function(currentArray, currentIndex){ 
			jQuery("#fancybox-counter").remove();
		}
	});
}

function tStonePagination() {
try {
	elements = jQuery('#post_thumb .post_thumb');
	elementsLen = elements.length;
	elementsPerDiv = 9;
	for (var i = 0;i < elementsLen;i+=elementsPerDiv){
		 elements.filter(':eq('+i+'),:lt('+(i+elementsPerDiv)+'):gt('+i+')').wrapAll('<div class="post_thumb_page"></div>');
	};
	
	if (elementsLen > 9) {
		jQuery('.navigation').show();
		jQuery('#post_thumb').cycle({
			fx:     'fade',
			speed:  400,
			timeout: 0,
			prev: '#post_thumb_prev',
			next: '#post_thumb_next',
			cleartypeNoBg: true,
			after:  function(currSlideElement, nextSlideElement, options, forwardFlag) {
				jQuery('#post_thumb_page').html(parseInt(options.currSlide+1)+' / '+parseInt(options.slideCount));
				jQuery('.post_thumb .fancybox').removeAttr('rel');
				jQuery('.post_thumb_page').eq(options.currSlide).find('.fancybox').attr('rel','exp');
			}
		});
	} else {
		jQuery('.navigation').hide();
	}
	jQuery('#post_thumb').height(jQuery('.post_thumb_page:first').height());
	initFancybox();
} catch(err) {}
}

// Function to get the Max value in Array
Array.max = function( array ){
return Math.max.apply( Math, array );
};

// Function to get the Min value in Array
Array.min = function( array ){
return Math.min.apply( Math, array );
};

function aRand(range,ha,hlast) {
	hmin = Array.min(ha);
	hmax = Array.max(ha);
	//alert(hmin+':'+hmax)
	success = false;
	for (i=1;i<99;i++) {
		//jQuery('body').prepend(Math.floor(Math.random()*range));
		if (!success) { 
			h = Math.floor(Math.random()*range);
			//jQuery('body').prepend('<p>'+h+':'+ha[h]+':'+hmin+':'+hmax+':'+hlast+'</p>');
			if (((ha[h] == hmin) && (h!=hlast)) || (hmin == hmax)) {
				success = true; 
				hlast = h;
			}
		}
	}
	switch (h)
	{
	case 0:
	  h1 = parseInt(h1+1);
	  jQuery.cookie('header1',h1);
	  break
	case 1:
	  h2 = parseInt(h2+1);
	  jQuery.cookie('header2',h2);
	  break
	case 2:
	  h3 = parseInt(h3+1);
	  jQuery.cookie('header3',h3);
	  break
	case 3:
	  h4 = parseInt(h4+1);
	  jQuery.cookie('header4',h4);
	  break
	case 4:
	  h5 = parseInt(h5+1);
	  jQuery.cookie('header5',h5);
	  break
	}		
	ha = new Array(h1,h2,h3,h4,h5);
	jQuery.cookie('header',hlast);
	//jQuery('body').prepend('<p>'+ha+':'+Array.min(ha)+':'+Array.max(ha)+':'+h+':'+hlast+'</p>');
	jQuery('#header_slider').append('<img src="wp-content/uploads/header0'+(parseInt(h)+1)+'.jpg" alt="" />');
	return h;
}
		
function itemShow(name, effect, duration, timeout) {
	setTimeout("jQuery('"+name+"').animate({"+effect+": 'show'}, "+duration+");",timeout);
}

function initThumbNav() {
	/*wyszukiwanie*/
	var expElements = jQuery('#post_thumb .post_thumb');
	
	jQuery('#exp_filter').submit(function () {
		jQuery('#post_thumb').html(expElements);
		jQuery('.post_thumb').removeClass('filter_hidden').show();
		jQuery('#post_thumb_empty').remove();
		filterYear = jQuery('#filter_year').val();
		//filterMonth = jQuery('#filter_month').val();
		filterCountry = jQuery('#filter_country').val();
		filterAmountMin = parseInt(jQuery('#filter_amount_min').val());
		filterAmountMax = parseInt(jQuery('#filter_amount_max').val());
		filterAmount = jQuery('#filter_amount').val();
		jQuery('.filter_year').each(function () {
			if (filterYear != '%' && !jQuery(this).parents('.post_thumb').hasClass('filter_hidden')) {
				if (jQuery.inArray(jQuery(this).attr('rel'), filterYear) < 0) {
					jQuery(this).parents('.post_thumb').addClass('filter_hidden').hide();
				} else {
					jQuery(this).parents('.post_thumb').show();
				}
			} else {
				jQuery(this).parents('.post_thumb').show();
			}
		});
		
		// jQuery('.filter_month').each(function () {
			// if (filterMonth != '%' && !jQuery(this).parents('.post_thumb').hasClass('filter_hidden')) {
				// if (jQuery.inArray(jQuery(this).attr('rel'), filterMonth) < 0) {
					// jQuery(this).parents('.post_thumb').addClass('filter_hidden').hide();
				// } else {
					// jQuery(this).parents('.post_thumb').show();
				// }
			// } else {
				// jQuery(this).parents('.post_thumb').show();
			// }
		// });
		
		jQuery('.filter_country').each(function () {
			if (filterCountry != '%' && !jQuery(this).parents('.post_thumb').hasClass('filter_hidden')) {
				if (jQuery.inArray(jQuery(this).attr('rel'), filterCountry) < 0) {
					jQuery(this).parents('.post_thumb').addClass('filter_hidden').hide();
				} else {
					jQuery(this).parents('.post_thumb').show();
				}
			} else {
				jQuery(this).parents('.post_thumb').show();
			}
		});
		
		// jQuery('.filter_amount_from').each(function () {
			// currentFrom = parseInt(jQuery(this).attr('rel'));
			// currentTo = parseInt(jQuery(this).next('.filter_amount_to').attr('rel'));
			// if ((filterAmountMin < 0 || filterAmountMax < 0) && !jQuery(this).parents('.post_thumb').hasClass('filter_hidden')) {
				// if (filterAmountMin < 0 && filterAmountMax < 0) {
					// jQuery(this).parents('.post_thumb').show();
				// } else if (filterAmountMin < 0 && filterAmountMax > 0) {
					// if ( currentTo <= filterAmountMax) {
						// jQuery(this).parents('.post_thumb').show();
					// } else {
						// jQuery(this).parents('.post_thumb').addClass('filter_hidden').hide();
					// }		
				// } else if (filterAmountMax < 0 && filterAmountMin > 0) {
					// if ( currentFrom >= filterAmountMin) {
						// jQuery(this).parents('.post_thumb').show();
					// } else {
						// jQuery(this).parents('.post_thumb').addClass('filter_hidden').hide();
					// }
				// }
			// } else {
				// if ( currentFrom >= filterAmountMin && currentTo <= filterAmountMax && !jQuery(this).parents('.post_thumb').hasClass('filter_hidden')) {
					// jQuery(this).parents('.post_thumb').show();
				// } else {
					// jQuery(this).parents('.post_thumb').addClass('filter_hidden').hide();
				// }
			// }
		// });
		jQuery('.filter_amount').each(function () {
			if (filterAmount != '%' && !jQuery(this).parents('.post_thumb').hasClass('filter_hidden')) {
				if (jQuery.inArray(jQuery(this).attr('rel'), filterAmount) < 0) {
					jQuery(this).parents('.post_thumb').addClass('filter_hidden').hide();
				} else {
					jQuery(this).parents('.post_thumb').show();
				}
			} else {
				jQuery(this).parents('.post_thumb').show();
			}
		});
		
		jQuery('.post_thumb a').attr('rel','exp');
		jQuery('.filter_hidden a').removeAttr('rel');
		if (jQuery('.post_thumb').not('.filter_hidden').size() < 1) {
			jQuery('#post_thumb').append('<h1 id="post_thumb_empty">'+filterEmpty +'</h1>');
		}
		
		jQuery('.filter_hidden').remove();
		tStonePagination();
		return false;
	});
	jQuery('#exp_filter').submit();
	jQuery('.entry a img').parents('a').addClass('fancybox').attr('rel','entry');
	jQuery('a.fancybox').append('<span class="zoom"></span>');
	initFancybox();
	
	try {
		jQuery('#references').cycle({
			fx:     'fade',
			speed:  800,
			timeout: 13000,
			cleartypeNoBg: true,
			after:  function(currSlideElement, nextSlideElement, options, forwardFlag) {
				jQuery.cookie('currSlide',parseInt(options.currSlide));
			}
		});
	} catch(err) {}
}
