$.fn.formDefaults = function() {
	return this.each(function(){
		$(this).toggleClass('placeholder');
		var value = $(this).val();
		$(this).focus(function(){
			if ($(this).val() == value) {
				$(this).toggleClass('placeholder');
				$(this).val("");
			}
		});
		$(this).blur(function(){
			if ($(this).val() == "") {
				$(this).toggleClass('placeholder');
				$(this).val(value);
			}
		});
	});
}

$(document).ready(function() {
	
	var msie6 = $.browser.msie == true && $.browser.version < 7;
	
	// Form placeholders
		$('#login-user, #search input.text, #newsletter #email, #search-indikace').formDefaults();
		$("#login-pwd")
			.hide()
			.focus(function() {
				if ($('#passdummy').length > 0) {
					$(this).show();
					$('#passdummy').remove();
				}
			});
        $("#login-user").after('<input id="passdummy" type="text" class="text passdummy" name="password_dummy" title="Heslo" value="Heslo">');
        $("#passdummy").focus(function () {
            $(this).remove();
            $("#login-pwd").show().focus();
        });
		
	// All categories
		$('body:not(.home) #cats').hover(function() {
			$(this).addClass('hover').find('.holder').show();
		}, function() {
			$(this).removeClass('hover').find('.holder').hide();
		});
		$('body:not(.home) #toggle-cats').focus(function() {
			$('#cats').addClass('hover').find('.holder').show();
		});
		$('body:not(.home) #cats ul.main li:last a').blur(function() {
			$('#cats').removeClass('hover').find('.holder').hide();
		});
		
	// Products
		$('body').delegate('.buy .count span', 'click', function(e) {
			var $t = $(this);
			var $tInput = $t.parent().prev('input');
			var val = parseInt($tInput.val());
			if ($t.is('.inc')) {
				$tInput.val(val + 1);
			} else if ($tInput.val() > 1) {
				$tInput.val(val - 1);
			}
		});
		// is numeric or empty?
		$('.buy div input').blur(function() {
			var val = $(this).val();
			if (val == '' || val != parseInt(val) || val < 1) $(this).val(1);
		});
		// Hide inactive tabbed listings (HP, catalog) and non-js headings
		$('.js .products2 h2').addClass('ah');
		$('.toggler:not(.default)').hide();
		$('.tabs a').click(function(e) {
			e.preventDefault();
			$(this).parent().addClass('active').siblings('.active').removeClass('active');
			if ($(this).is('.all')) {
				$('.toggler').show();
				$('.products2 h2').removeClass('ah');
			} else {
				$('.products2 h2').addClass('ah');
				$('.toggler:visible').hide();
				$($(this).attr('href')).show();
			}
		});
		
	// Order
		if(!$('#another-ship-addr').is(':checked')) $('#shipAddr').hide();
		//alert($('#another-ship-addr').is(':checked'));
		$('#another-ship-addr').click(function() {
			$('#shipAddr').toggle();
		});
		
		
		//added by petr 
		if(!$('#buy-company').is(':checked')) $('#buyCompanyFS').hide();
		//alert($('#another-ship-addr').is(':checked'));
		$('#buy-company').click(function() {
			$('#buyCompanyFS').toggle();
		});
		
				
		//added by petr
	  //$("a#big_picture").fancybox();
		
		//$('#big_picture').lightBox();
		
		
		$('#transfer input:checked').parents('tr').addClass('active');
		$('#transfer input').click(function() {
			$(this).parents('tbody').find('tr.active').removeClass('active');
			$(this).parents('tr').addClass('active');
		});
		// Disable order submit unless user checks terms of trade
		$('#signup .submit input').toggleClass('disabled');
		$('#signup .submit input.disabled').live('click', function(e) {
			e.preventDefault();
			if ( $(this).prev('p.error').length == 0 )
				$(this).before('<p class="error">Pro pokračování musíte <strong>souhlasit s obchodními podmínkami</strong></p>');
		});
		$('#CondAgree').change(function() {
			if( $(this).is(':checked') )
				$('#signup .submit input').toggleClass('disabled').siblings('p').toggle();
			else
				$('#signup .submit input').toggleClass('disabled').siblings('p').toggle();
		});
		
	// Masonry
		if ($('.indikace ul').length > 0) {
			$('.indikace ul').masonry();
		}
		if ($('.hp-bottom .categories ul').length > 0) {
			$('.hp-bottom .categories ul').masonry();
		}
		
	// Fixed in-text navigation
		if ($('#main .fixnav .nav').length > 0) {

			var $navContainer = $('#main .fixnav .nav');
			var bottomOffset = $('#footer').outerHeight(true) + 30;
			//alert($navContainer.outerHeight() + bottomOffset);
			if (!msie6 && $(window).height() > ($navContainer.outerHeight() + bottomOffset)) {
				var top = $navContainer.offset().top - parseFloat($navContainer.css('margin-top').replace(/auto/, 0));
				$(window).scroll(function(e) {
			    	var y = $(this).scrollTop();
			    	if (y >= top)
	        			$navContainer.addClass('fixed');
					else
						$navContainer.removeClass('fixed');
			    });
			}
			if(window.location.hash) {
				$navContainer.find('a[href="'+ window.location.hash +'"]').addClass('active');
			}
			$('#main .fixnav .nav a')
				.click(function() {
					$(this)
						.addClass('active')
						.parent().siblings('li').find('a.active').removeClass('active');
				})
				.smoothScroll();
		}

	// Contact
		var hereweare = Math.sqrt(Math.pow('Vitalsupport'.length, 2)); hereweare += 3;
		$('.contact .button').before('<input type="hidden" name="c-addt" value="'+ hereweare + '">');
	
	// HP season hits slider
		if ($.fn.scrollable && $('#hits').length > 0) {
			$('#hits')
				.scrollable()
				.navigator({ navi: '.navi' })
				.autoscroll({ steps: 20 });
		}
	
	// HP main section height helper
		$('body.home #main .ic').height($('#cats .main').height() - 9);
	
	// HP indikace
		$('#toggle-indikace').click(function(e) {
			e.preventDefault();
			var $this = $(this),
				$moreButton = $('<p>', {
					'class': 'more',
					html: $('<a>', {
						href: $this.attr('href'),
						text: $this.text() + ' »'
					})
				}).appendTo('#hp-indikace');
			//$this.parents('.bottom').fadeOut(200);
			$this.prev('h2').clone().prependTo('#hp-indikace');
			$('#hp-theme').addClass('hp-indikace-active');
			$('#hp-indikace').show().animate({top: 0}, 500, function() {
				$('#hp-indikace ul').masonry();
				$this.parents('.bottom').hide();
			});
		});

});
