(function($){
	$(document).ready(function(){
		$("#accordion").accordion({ 
	    	autoHeight: false 
		});

		initTeaser("carousel", "teaser");

		$('.partners .slides').cycle({
			fx: 'fade' // choose your transition type, ex: fade, scrollUp, shuffle, etc...
		});

		$('.featured .slides').cycle({
			fx: 'fade', // choose your transition type, ex: fade, scrollUp, shuffle, etc...
			next: '.featured .btn-next'
		});

		$('.btn-reserve').click(function(){
			goToByScroll('contact-form');
		});

		$('#s').attr('placeholder','Zde zadejte hledaný výraz...');

		$('#services').appendTo('.menu-services');

		$('.menu-services').hover(
			function(){
				$(this).find('a').addClass('hover');
				$('#services').fadeIn();
			},
			function(){
				$(this).find('a').removeClass('hover');
				$('#services').fadeOut();
			}
		);

		$('.more-photos').click(function(){
			/*var curHeight = $(this).parent().find('.photos').height();
			$(this).parent().find('.photos').css('height', 'auto');
			var autoHeight = $(this).parent().find('.photos').height();
			$(this).parent().find('.photos').height(curHeight).animate({height: autoHeight}, 500);*/
			if($(this).hasClass('open')){
				oldHeight = $(this).parent().height();
				$(this).parent().find('.photos').css('height', '90px');
				$(this).removeClass('open');
				$(this).html('další fotografie');
				$('.galleries').css('max-height', "+="+($(this).parent().height() - oldHeight));
			}
			else
			{
				oldHeight = $(this).parent().height();
				$(this).parent().find('.photos').css('height', 'auto');
				$(this).addClass('open');				
				$(this).html('zavřít');
				$('.galleries').css('max-height', "+="+($(this).parent().height() - oldHeight));
			}
					
		});

		$('.more-galleries').click(function(){
			oldHeight = $(this).parent().find('.galleries').height();
			$(this).parent().find('.galleries').css('max-height', '+=870');
			if (oldHeight + 870 > $(this).parent().find('.galleries').height())
			{
				$(this).hide();
			}
			return false;
		});
	});

	function carousel_itemVisibleInCallback(carousel, item, i, state, evt) {
		var idx = carousel.index(i, carousel_itemList.length);
		carousel.add(i, carousel_getItemHTML(carousel_itemList[idx - 1]));
	}

	function carousel_itemVisibleOutCallback(carousel, item, i, state, evt) {
		carousel.remove(i);
	}

	function carousel_getItemHTML(item) {
		return '<div class="scrollerteaser"><img src="'+item.url+'" /><b>'+item.title+'</b>'+item.subtitle+'</div>';
	}

	var active1 = 0;
	var active2 = 0;
	var active3 = 0;
	var stprev = 0;
	function current_active(carousel, item, i, state, evt) {
		var max = carousel_itemList.length;
		if(state == "init") {
			if(active1 == 0) {
				active1 = i;
			} else {
				if(active2 == 0) {
					active2 = i;
				} else {
					if(active3 == 0) {
						active3 = i;
					}
				}
			}
		}
		if(state == "next") {
			active1 = active1 + 1;
			active2 = active2 + 1;
			active3 = active3 + 1;
			if(active1 > max) { active1 = 1; }
			if(active2 > max) { active2 = 1; }
			if(active3 > max) { active3 = 1; }
		}
		if(state == "prev") {
			active1 = active1 - 1;
			active2 = active2 - 1;
			active3 = active3 - 1;
			if(active1 < 1) { active1 = max; }
			if(active2 < 1) { active2 = max; }
			if(active3 < 1) { active3 = max; }
		}

		if(active1 > 0 && active2 > 0 && active3 > 0) {
			if(stprev != 0) { $("#teaserimg"+stprev).fadeTo(750, 0).css("z-index", "10"); }
			$("#teaserimg"+active2).fadeTo(500, 1, function() { $(this).css("filter", ""); }).css("z-index", "100");
			stprev = active2;
		}
	}

	function carousel_initCallback(carousel) {
		jQuery('.jcarousel-list li').live('click', function() {
			carousel.scroll(jQuery(this).attr('jcarouselindex')-1);
			carousel.startAuto(0);
			return false;
		});

		carousel.buttonNext.bind('click', function() {
			carousel.startAuto(0);
		});

		carousel.buttonPrev.bind('click', function() {
			carousel.startAuto(0);
		});
	}

	function initTeaser(carousel, teaser) {
		jQuery("#"+carousel).jcarousel({
			auto: 8,
			wrap: 'circular',
			scroll: 1,
			initCallback: carousel_initCallback,
			itemVisibleInCallback: {
				onBeforeAnimation: carousel_itemVisibleInCallback,
				onAfterAnimation: current_active
			},
			itemVisibleOutCallback: {onAfterAnimation: carousel_itemVisibleOutCallback}
		});
		$("#"+teaser+" > div").each(function(i) {
			if(i == 1) {
				$(this).attr("id", "teaserimg"+(i+1)).css("z-index", "100");
			} else {
				$(this).attr("id", "teaserimg"+(i+1)).fadeTo(1, 0).show().css("z-index", "10");
			}
		});
		$("#"+teaser+" > div > h1").each(function() {
			pos = "lines"+($("br", $(this)).size()+1);
			$(this).html("<span>"+($(this).html().replace(/<br>/gi, "</span><br /><span>"))+"</span>").addClass(pos);
		});
		$("#"+teaser+" > div > p").each(function() {
			$(this).html("<span>"+($(this).html().replace(/<br>/gi, "</span><br /><span>"))+"</span>");
		});
	}

	function goToByScroll(id){
	     	$('html,body').animate({scrollTop: $("#"+id).offset().top},'slow');
	}

})(jQuery);
