jQuery(document).ready(function(){	
	try {
		Cufon.replace('h1,.featured_details p,.btn,#menu-search,.carousel .more, .carousel-caption p, .carousel-caption .description_btn', { fontFamily: 'MyriadPro', hover: true });
		Cufon.replace('.featured_details h2,#menu>ul>li.menu-link>a', { fontFamily: 'MyriadPro', hover: true, textShadow: '-1px -1px #74a03a' });
		Cufon.replace('.btn_more, .btn_smore', { fontFamily: 'MyriadPro', hover: true, textShadow: '1px 1px #588c21' });
		Cufon.replace('.btn_sred', { fontFamily: 'MyriadPro', hover: true, textShadow: '1px 1px #a46653' });
		Cufon.replace('#callcenter, #navigation ul.fr a', { fontFamily: 'MyriadProBlack', hover: true });
		Cufon.replace('.dark #callcenter, .dark ul.fr a', { fontFamily: 'MyriadProBlack', hover: true });
		Cufon.replace('.menu-children li p.menu-title a,.products .carousel ul li h3 a,h2,h3,#footer a', { fontFamily: 'MyriadProBlack', hover: true});
	} catch(err) {}

	jQuery('.totop').click(function () {
		scroll(0,0);
		return false;
	});

	jQuery('.back').click(function () {
		history.go(-1);
		return false;
	});
	
	if ( jQuery.browser.msie && jQuery.browser.version < 7) {
		jQuery('img[src$=".png"]').each(function() {
			this.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src="+this.src+",sizingMethod='scale')";
		});
	}
	
	jQuery('#searchform_results').append('<div id="searchform_results_after" />');
	
	/*
	jQuery('#searchform input:text').focus(function () {
		jQuery(this).parents('#searchform').css({'background-position':'0 -100px'});
		jQuery('#searchform_results').animate({'height': 'show'}, 200);
	});	
	jQuery('#searchform input:text').blur(function () {
		jQuery(this).parents('#searchform').css({'background-position':'0 0'});
		jQuery('#searchform_results').animate({'height': 'hide'}, 200);
	});	
	*/
	
	try {
		// var cf = new ContentFlow('contentFlow', {
			// reflectionHeight: 0, 
			// circularFlow: true,
			// visibleItems: 1,
			// scaleFactor: 1.1,
			// showControlls : true,
			// relativeItemPosition: "top center",
			// onMakeActive: function (item) {
				// jQuery('.flow .item').css({'margin-top':'-80px'});
				// jQuery('.flow .active').css({'margin-top':'0'});
			// }
		// });
		
		jQuery("#carousel").featureCarousel({
			smallFeatureWidth: 1,
			smallFeatureHeight: 0.85,
			smallFeatureOffset: 35,
			autoPlay: 0
		});
		
		jQuery(".tracker-summation-container,.tracker-individual-container").remove();
	} catch(err) {}
	
	try {
		jQuery('select').msDropDown();
	} catch(err) {}
	
	try {
		jQuery('input:checkbox').checkbox(
		);
		jQuery('input:radio:not(.poll input:radio)').checkbox({cls: 'jquery-radio-checkbox', empty: '/assets/images/empty.png'});
		jQuery('.poll input:radio').checkbox({cls: 'jquery-poll-checkbox', empty: '/assets/images/empty.png'});
	} catch(err) {}
	
	jQuery('#menu>ul>li.menu-parent>a').prepend('<span class="menu-parent-before" />').append('<span class="menu-parent-after" />');
	jQuery('.menu-children').wrap('<div class="menu-children-holder" />').wrap('<div class="menu-children-wrap" />');
	jQuery('.menu-children-wrap').before('<div class="menu-children-before" />').after('<div class="menu-children-after" />').append(jQuery('#menu-search'));
	
	jQuery('.carousel li').mouseenter(function () {
		jQuery(this).children('a').children('img').animate({'height': '254px' }, { duration : 'medium', queue : false});
		jQuery(this).children('h3').find('a').css({ color : '#afc770' });
		try { Cufon.refresh(); } catch(err) {}
	});
	jQuery('.carousel li').mouseleave(function () {
		jQuery(this).children('a').children('img').animate({'height': '236px' }, { duration : 'medium', queue : false});
		jQuery(this).children('h3').find('a').css({ color : '#2F7500' });
		try { Cufon.refresh(); } catch(err) {}
	});
	
	jQuery('#menu>ul>li.menu-parent .menu-children-holder').hide();
	// jQuery('#menu>ul>li.menu-parent').mouseenter(function () {
		// jQuery(this).children('.menu-children-holder').animate({height: 'show'}, 400);
	// });
	// jQuery('#menu>ul>li.menu-parent').mouseleave(function () {
		// jQuery(this).children('.menu-children-holder').animate({height: 'hide'}, 400);
	// });
	jQuery('#menu>ul>li.menu-parent').hoverIntent(menuIn,menuOut, {interval: 0});
	jQuery('#menu>ul>li.menu-parent .menu-children,#menu-search').show();
	
	jQuery('#menu>ul>li.menu-parent').each(function () {
		$this = jQuery(this).find('a:first');
		try {
			Cufon.replace($this, { fontFamily: 'MyriadPro', hover: false, textShadow: '1px 1px #4c9c32', color: '#fff' });
		} catch(err) {}
	});
	jQuery('#menu>ul>li.menu-parent').mouseenter(function () {
		$this = jQuery(this).find('a:first');
		try {
			Cufon.replace($this, { fontFamily: 'MyriadPro', hover: false, textShadow: '1px 1px #fff', color: '#4c9c32' });
		} catch(err) {}
	});
	jQuery('#menu>ul>li.menu-parent').mouseleave(function () {
		$this = jQuery(this).find('a:first');
		try {
			Cufon.replace($this, { fontFamily: 'MyriadPro', hover: false, textShadow: '-1px -1px #74a03a', color: '#fff' });
		} catch(err) {}
	});
	
	jQuery('a.description_btn[href*="#wstap"]').live('click',function() { jQuery('.btn-login').click(); return false; });
	
	jQuery('.featured').hoverIntent(featuredIn,featuredOut, {interval: 0});
	jQuery('#translations').hoverIntent(translationsIn,translationsOut, {interval: 0});
	jQuery('#language').live('click', function () { return false; });
	
	if (jQuery(".carousel li").size()>3) jQuery(".carousel_prev,.carousel_next").show();
	if (jQuery(".carousel li").size()>0) {
		jQuery(".carousel").jCarouselLite({
			btnNext: ".carousel_next",
			btnPrev: ".carousel_prev",
			mouseWheel: true,
			scroll: 1,
			visible: 3,
			circular: false,
			auto : 0
		});
	}
	
	
	jQuery(".carousel_prev").hoverIntent(carouselGo,new Function());
	jQuery(".carousel_next").hoverIntent(carouselGo,new Function());
	
	function carouselGo() {
		jQuery(this).click();
	}
	
	jQuery('#facebook').hoverIntent(fbIn,fbOut);
	
	jQuery('.service_details').each(function () {
		jQuery(this).height(jQuery(this).find('.service_position').height());
	});
	jQuery('.services ul li').hoverIntent(servicesIn,servicesOut);	
	
	if (jQuery("#featured li").size()>4) jQuery("#featured_prev, #featured_next").show();
		if (jQuery("#featured li").size()>0) {
			jQuery("#featured").jCarouselLite({
				btnNext: "#featured_next",
				btnPrev: "#featured_prev",
				mouseWheel: true,
				scroll: 1,
				visible: 4,
				circular: false
			});
	}
	
	if (jQuery(".items_list li").size()>4) jQuery(".item_related .less, .item_related .more").show();
	if (jQuery(".items_list li").size()>0) {
		jQuery(".items_list").jCarouselLite({
			btnNext: ".more",
			btnPrev: ".less",
			mouseWheel: true,
			scroll: 1,
			visible: 4,
			circular: false
		});
	}
	/*
	try {
		jQuery('#item_pack_nav').css({'margin-left':parseInt((jQuery('#item_pack_nav').parent('div').width()-jQuery('#item_pack_nav').width())/2)+'px'});
		jQuery('#item_pack_slideshow').cycle({ 
			fx:     'fade', 
			speed:  800, 
			timeout: 0, 
			pager:  '#item_pack_nav',
			pagerAnchorBuilder: function(index) {
				return '#item_pack_nav a:eq('+index+')';
			},
			cleartypeNoBg: true
		});
	} catch(err) {}*/
	jQuery('#item_pack_nav').css({'margin-left':parseInt((jQuery('#item_pack_nav').parent('div').width()-jQuery('#item_pack_nav').width())/2)+'px'});
	$('#item_pack_nav a').click(function() {
		$('.product_variant').hide();
		$('.product_variant_'+$(this).attr('rel')).show();
		$('#item_pack_nav a').removeClass('activeSlide');
		$(this).addClass('activeSlide');
		return false;
	});
	
	if ( !jQuery.browser.msie || jQuery.browser.version > 8) 
	{
		jQuery('#tymbark, #facebook a, #promotion,#footer img, .loyality a').animate({opacity: 1});
		jQuery('#tymbark, #facebook a, #promotion,#footer img, .loyality a').mouseover(function () { jQuery(this).stop().animate({ opacity: .8 }, 200); });
		jQuery('#tymbark, #facebook a, #promotion,#footer img, .loyality a').mouseout(function () { jQuery(this).stop().animate({ opacity: 1 }, 200); });
		//jQuery('.awards a,.menu-children a').mouseover(function () { jQuery(this).find('img').stop().animate({ opacity: .8 }, 200); });
		//jQuery('.awards a,.menu-children a').mouseout(function () { jQuery(this).find('img').stop().animate({ opacity: 1 }, 200); });
	}

	jQuery('input:text, input:password, textarea').not('#searchform input:text, #pagesearchform input:text').focus(function () {
		if (jQuery(this).val()==jQuery(this).attr('default')) jQuery(this).val('');
		jQuery(this).addClass('active');
		jQuery(this).parents('.input_wrap').addClass('input_wrap_active');
	});
	jQuery('input:text, input:password, textarea').not('#searchform input:text, #pagesearchform input:text').blur(function () {
		if (jQuery(this).val()=='') 
			jQuery(this).val(jQuery(this).attr('default'));
		
		if(jQuery(this).val() == jQuery(this).attr('default'))
			jQuery(this).removeClass('active');
		jQuery(this).parents('.input_wrap').removeClass('input_wrap_active');
	});
	
	jQuery('.select_wrap_256x25 select').change(function () {
		jQuery(this).prev().text(jQuery("option:selected", this).text());
	}).prev().text(jQuery("option:selected", jQuery('.select_wrap_256x25 select')).text());
	
	jQuery('.helper-select select').change(function () {
		jQuery(this).prev().text(jQuery("option:selected", this).text());
	}).prev().text(jQuery("option:selected", jQuery('.helper-select select')).text());
	
	jQuery('#searchform input:text, #pagesearchform input:text').focus(function () {
		if (jQuery(this).val()==jQuery(this).attr('title')) jQuery(this).val('');
		jQuery(this).removeClass('error');
		jQuery(this).addClass('active');
	});
	jQuery('#searchform input:text, #pagesearchform input:text').blur(function () {
		if (jQuery(this).val()=='') jQuery(this).val(jQuery(this).attr('title'));
		jQuery(this).removeClass('active');
	});
	
	jQuery('#refresh-captcha').click(function () {
		$.post('/kontakt,captcha.html',{}, function (r){
				jQuery('#captcha-cont').html(r);
		}, 'html');
	});
	
	jQuery('.poll_answer').live('click', function() {
		 url = $(this).attr('rel');
		 
		 $.post(url, { "data": jQuery('#poll_form').serialize() },
				 function(data){
				   if(data.success)
					   {
					   $elem = jQuery('#poll_form');
					   $cont = jQuery('#poll_container');
					   $elem.fadeOut('medium', function() { $cont.css('display', 'none'); $cont.html(data.html); $cont.fadeIn('medium'); });
					   }
				 },"json");
		 return false;
		});
	
	jQuery('.menu-children li').mouseenter( function() {
		if ( !jQuery.browser.msie || jQuery.browser.version > 8) 
			$(this).children('a').find('img').css({opacity: .8});
		$(this).children('p').find('a').css({ color : '#afc770' });
		try { Cufon.refresh(); } catch(err) {}
	});
	jQuery('.menu-children li').mouseleave(function() {
		if ( !jQuery.browser.msie || jQuery.browser.version > 8) 
			$(this).children('a').find('img').css({'opacity' : 1});
		$(this).children('p').find('a').css({ color : '#2F7500' });
		try { Cufon.refresh(); } catch(err) {}
	});	
	
	if(jQuery('#main-content').length)
	{
		jQuery('.featured').live('click', function() {
			$url = $(this).children('.featured_details').children('.btn').attr('href');
			window.location = $url;
			return false;
		});
	}
	
	jQuery('.service_details').live('click', function() {
		$url = $(this).children('.service_position').children('h2').children('a').attr('href');
		window.location = $url;
		return false;
	});
	
	jQuery('li.submenu a').live('click', function() {
		if($(this).parent('li').hasClass('subsubmenu')) return true;
		$(this).next('ul').toggle();
		if($(this).parent('li').hasClass('no_content')) return false;
	});
	
	try {
		jQuery("#contactform").validate({
			rules: {
				contact_name: {
					required: true,
					minlength: 2,
					notDefault: '#contact_name'
				},
				contact_email: {
					required: true,
					email: true,
					notDefault: '#contact_email'
				},
				contact_message: {
					required: true,
					minlength: 2,
					notDefault: '#contact_message'
				},
				contact_subject: {
					required: true
				}
			},
			errorPlacement: function(error, element){
				jQuery(element).parents('.input_wrap').addClass('input_wrap_error');
				error.insertAfter(jQuery(element).parents('.input_wrap'));
			},
			invalidHandler: function(f, v) {
				jQuery('#contactform .message').hide();
				var errors = v.numberOfInvalids();
				if (errors) {
					jQuery('#contactform').prepend('<div class="message message_send_error"><p>'+jQuery('#contactform').attr('error')+'</p></div><!-- close .message -->').animate({'height':'show'},200);
				}
	        },
			unhighlight: function(element, errorClass) {
				if (this.numberOfInvalids() == 0) {
					$("#contactform .message_send_error").hide();
				}
				jQuery(element).removeClass(errorClass);
				jQuery(element).parents('.input_wrap').removeClass('input_wrap_error');
			}    
	
		});
	} catch(err) {}
	search.init();
	search.bindSearch();
	search.clear();
	search.fill();
	
	if(jQuery('#sok-na-sniadanie').length)
	{
		$("#sok-na-sniadanie").tabs({
			
			select: function(event, ui) {
				
				$content = $('#tab-index-' + ui.index).html();
				$current_tab = $('#tabs-' + (ui.index + 1));
				$current_tab.prepend($content);
				$('.toremove').remove();
				$current_tab.find(":first") .addClass('toremove');
				
			}
		});
	}
	
});


jQuery(window).load(function(){
});

try {
	jQuery.validator.addMethod("notDefault", function(value, element, param) {
		return this.optional(element) || value != $(param).attr('default');
	});
} catch(err) {}

function hoverIn(){ jQuery(this).children('ul').animate({height: 'toggle'}, 400);}
function hoverOut(){ jQuery(this).children('ul').animate({height: 'toggle'}, 400);}

function featuredIn(){ 
	jQuery(this).children('.featured_details').animate({top: '0'}, 200);
	jQuery(this).children('.featured_details').prev('h2').animate({bottom: '-38px'}, 200);
}
function featuredOut(){ 
	jQuery(this).children('.featured_details').animate({top: '134px'}, 200);
	jQuery(this).children('.featured_details').prev('h2').animate({bottom: '2px'}, 200);
}

function translationsIn(){ jQuery(this).children('ul').animate({height: 'toggle'}, 400);}
function translationsOut(){ jQuery(this).children('ul').animate({height: 'toggle'}, 400);}

function menuIn(){ jQuery('#rotatorNavi').fadeOut(); jQuery(this).children('.menu-children-holder').animate({height: 'show'}, 400);}
function menuOut(){ jQuery('#rotatorNavi').fadeIn(); jQuery(this).children('.menu-children-holder').animate({height: 'hide'}, 400);}

function fbIn(){ jQuery(this).animate({ left: '0' }, 400);}
function fbOut(){ jQuery(this).animate({ left: '-292px' }, 400);}


function servicesIn(){ 
//	jQuery(this).find('.service_details').animate({'height': '100%'}, 200);
//	jQuery(this).find('.service_details p').animate({'height': 'show'}, 200);
}
function servicesOut(){ 
//	jQuery(this).find('.service_details').animate({'height': jQuery(this).find('.service_position').height()-jQuery(this).find('p').height()-10}, 200);
//	jQuery(this).find('.service_details p').animate({'height': 'hide'}, 200);
}

var search = {
	url: '',
	timeoutId : null,
	searchString : '',
	init: function(){
		this.url = $('#searchform').attr('action');
	},
	
	resultShow : function() {
		//$('#searchform input:text').parents('#searchform').css({'background-position':'0 -100px'});
		$('#searchform_results').animate({'height': 'show'}, 200);

	},
	
	resultHide: function(){
			//$('#searchform input:text').parents('#searchform').css({'background-position': '0 0'});
			$('#searchform_results').animate({
				'height': 'hide'
			}, 200);
	},
	
	bindSearch : function() {
		$('#s').keyup(function(event) {
			
			var iKey = event.keyCode;
			if ((iKey !=8 && iKey !=46) && (iKey < 32 || (iKey >= 33 & iKey <= 46) || (iKey >=112 && iKey <= 123))) {	
				
			} else {
				if (search.trim($('#s').val()).length >= 3) {
					search.searchString = $('#s').val();
					/* wyczyść timeout */
					clearTimeout(search.timeoutId);
					/* zainicjuj timeout */
					search.timeoutId = setTimeout(function(){
						search.executeSearch();
					}, 500);
				} else {
					search.resultHide();
					$('#searchform_results').html('');
					$("body").unbind("click", function(e){
				    });
					clearTimeout(search.timeoutId);	
				}
			}
				
		});
	},
	executeSearch : function() {
		$('#search_loader').show();
		$('#searchform_results').load(search.url,{ queryString: search.trim(search.searchString) }, function(data){
			
			search.resultShow();
			clearTimeout(search.timeoutId);
			$("body").bind("click", function(e){
				search.resultHide();
		    	$('#searchform_results').html('');
		    });
			$('#search_loader').hide();
			
			});
	},
	trim : function (str, chars) {
		return this.ltrim(this.rtrim(str, chars), chars);
	},
 
	ltrim : function(str, chars) {
		chars = chars || "\\s";
		return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
	},
 
	rtrim : function(str, chars) {
		chars = chars || "\\s";
		return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
	},
	clear : function() {
		$('#s').focus(function(){
			if ($('#s').val() == $('#s').attr("defaultValue"))
				$('#s').val('');
		});
		
	},
	fill : function() {
		$('#s').blur(function(){
			if ($('#s').val() == '')
				$('#s').val($('#s').attr("defaultValue"));
		});
	}
};

jQuery(window).resize(resize);
window.onload = resize;

function resize() 
{
	$header_height  = 176;
	$slider_height  = 315;
	if(jQuery('#main-content').length)
	{
		$content = jQuery('#main-content');
		$content_default = $content.css('top');
		$content_height = $content.height();
		$window_height  = jQuery(window).height();
		
		if($window_height > $header_height + $slider_height + $content_height)
			$content.css('top', $window_height - $content_height);
		else
			$content.css('top', $content_default);
	}
	else if(jQuery('#flash75lat_container').length)
	{
		$flash  = jQuery('#flash75lat_container');
		$flash_height = $flash.height();
		$footer = jQuery('#footer');
		$page = jQuery('#page');
		$footer_height = $footer.height();
		$page_height = $page.height() + $flash_height;
		$window_height  = jQuery(window).height();
		if($window_height > $page_height)
		{
			$padding_top = 0;
			if(jQuery('#flash75lat_container').length)
			{
				$products_height = jQuery('#flash75lat_container').height();
				$padding_top = ( $window_height - $footer_height - $header_height) / 2 - ($products_height / 2);
				if($padding_top < 22) $padding_top = 22;
				jQuery('#flash75lat_container').css('padding-top', $padding_top);
			}
			
			$footer.css({ position : 'absolute', bottom : (($padding_top == 22) ? -22 : 0), marginBottom : (($padding_top == 22) ? 0 : 22)});
		}
		else
			$footer.css({'position' : '', bottom : ''});
	}
	else if(jQuery('#services').length)
	{
		$content = jQuery('#content');
		$footer = jQuery('#footer');
		$page = jQuery('#page');
		$footer_height = $footer.height();
		$page_height = $page.height();
		$content_height = $content.height();
		$window_height  = jQuery(window).height();
		
		if($window_height > $page_height)
		{
			$padding_top = 0;
			if(jQuery('#services').length)
			{
				$products_height = jQuery('#services').height();
				$padding_top = ( $window_height - $footer_height - $header_height) / 2 - ($products_height / 2);
				jQuery('#services').css('padding-top', $padding_top - 30);
				
				$content = jQuery('#content');
				$content_height = $content.height();
			}
			
			$footer.css({ position : 'absolute', bottom : (20 + $content_height + $header_height) - $window_height });
		}
		else
			$footer.css({'position' : '', bottom : ''});
	}
	/*
	else if(jQuery('#product_page').length)
	{
		$content = jQuery('#content');
		$footer = jQuery('#footer');
		$page = jQuery('#page');
		$footer_height = $footer.height();
		$page_height = $page.height();
		$content_height = $content.height();
		$window_height  = jQuery(window).height();
		
		if($window_height > $page_height)
		{
			$padding_top = ( $window_height - $footer_height - $header_height) / 2 - ($content_height / 2);
			jQuery('.product').css('margin-top', $padding_top - 30);
			$content_height = $content.height();
			$footer.css({ position : 'absolute', bottom : (20 + $content_height + $header_height) - $window_height });
		}
		else
			$footer.css({'position' : '', bottom : ''});
	}
	*/
	else if(jQuery('#page').length && !jQuery('#product_page').length)
	{
		$content = jQuery('#content');
		$page = jQuery('#page');
		$footer = jQuery('#footer');
		$footer_height = $footer.height();
		$content_height = $content.height();
		$page_height = $page.height();
		$window_height  = jQuery(window).height();
		
		if($window_height > $page_height)
		{
			$padding_top = 0;
			if(jQuery('.vertical-align').length)
			{
				$products_height = jQuery('.vertical-align').height();
				$padding_top = ( $window_height - $footer_height - $header_height) / 2 - ($products_height / 2);
				jQuery('.vertical-align').css('padding-top', $padding_top - 20);
				
				$content = jQuery('#content');
				$content_height = $content.height();
			}
			$footer.css({ position : 'absolute', bottom : (20 + $content_height + $header_height) - $window_height });
		}
		else
			$footer.css({'position' : '', bottom : ''});
	}
}
function setOverlay(menu,breacrumbs) { 

	try {
		
	} catch(e) { }
	
	if(jQuery('#navigation').length) { jQuery('#navigation').attr('class', menu); try { Cufon.refresh(); } catch(err) {} }  

	try {
		if(jQuery('#breadcrumb').length) { jQuery('#breadcrumb').attr('class', 'breadcrumbs ' + breacrumbs); }
	} catch(e) {
		if(jQuery('#breadcrumb').length) { jQuery('#breadcrumb').attr('class', 'breadcrumbs ' + menu); }
	}
		
}
function checkSearch() { if(jQuery('#s').val() == jQuery('#s').attr('title')) return false; }
