window.BASE_HREF = window.BASE_HREF || '../';


$.fn.inputDefaultText = function(options)
{
	options = $.extend({
		text: 'Hledany vyraz'
	}, options);

	return this
		.val(options.text)
		.bind('focus', function(){ if(this.value == options.text) this.value = ''; })
		.bind('blur', function(){ if(this.value == '') this.value = options.text; });
};

$.fn.kfEqualizeColumns = function(options)
{
	options = $.extend({
		column: '>li'
	}, options);

	return this.each(function(i)
	{
		var $columns = $(options.column, this);
		var maxHeight = 0;
		$columns
			.height('auto')
			.each(function(){
				var h = $(this).height();
				if(h > maxHeight) maxHeight = h;
			})
			.height(maxHeight);
	});
}

function fontSizeListener($el, callback)
{
	$el = $($el);
	var h = 0;
	var interval = setInterval(function()
	{
		if($el.height() != h){
			h = parseInt($el.height());
			callback();
		}
	}, 200);
};

function equalizeHeights()
{
	$('.home-bottom').kfEqualizeColumns({ column: '.inner' });
	$('.sortiment>ul').kfEqualizeColumns({ column: '>li' });
};

$(document).ready(function()
{

	$('.jshide').hide();
	$.fn.kfBox && $('.lightbox').kfBox();

	$('#q').inputDefaultText({ text: 'start your search'});
	$('#shop-search .inp-text, #dialog-shop .inp-text').inputDefaultText({ text: 'Enter Number'});

        $('.reservation-form input.inp-text').each(function(){
            $(this).inputDefaultText({text: $(this).parent().prev().text()})
        });

	if($.fn.cycle)
	{
		$('.product-rotator').append('<div class="prevnext"><a href="#" class="btn-prev">Předchozí</a><a href="#" class="btn-next">Další</a></div>');
		$('.product-rotator .frames').cycle({
			fx: 'fade',
			speed: 1000,
			timeout: 6000,
			pause: true,
			next: '.product-rotator .btn-next',
			prev: '.product-rotator .btn-prev'
		});

	}
	
	if($.fn.cycle)
	{
		$('.product-rotator-2').append('<div class="prevnext"><a href="#" class="btn-prev">Předchozí</a><a href="#" class="btn-next">Další</a></div>');
		$('.product-rotator-2 .frames').cycle({
			fx: 'fade',
			speed: 1000,
			timeout: 6000,
			pause: true,
			next: '.product-rotator-2 .btn-next',
			prev: '.product-rotator-2 .btn-prev'
		});

	}
	//*
	var imageCycler = new kff.widgets.ImageCycler('.intro-box');
	imageCycler.init();
	imageCycler.start();
	imageCycler.$element
	    .bind('changed', function(){
        	    var link = $(this).find('.frames a').eq(imageCycler.activeImage);
                if(link.length && link.attr('href')){
                    $(this).css('cursor', 'pointer');
                }
                else{
                    $(this).css('cursor', '');
                }
            })
            .trigger('changed')
            .click(function(){
        	    var link = $(this).find('.frames a').eq(imageCycler.activeImage);
                if(link.length && link.attr('href')){
                    window.location = link.attr('href');
                }
            });
//*/
	/* Inicializace scrollovátka obrázků v detailu shopu */
	var $prevnext2 = $('<div class="prevnext"><a href="#" class="btn-prev">předchozí</a> <a href="#" class="btn-next">další</a></div>');
    $('.image-carousel .hscroll-box').before($prevnext2);
    var imageScroller = new kff.widgets.Scroller('.image-carousel .hscroll-box', {
		scrollWidth: 530,
		carouselWidth: 530,
		prev: '.image-carousel .btn-prev',
		next: '.image-carousel .btn-next'
	});
	imageScroller.activate();

	/**
	 *  CalcSlideDialog
	 *  Třída pro dialogové okno nápovědy
	 */
	kff.widgets.CalcSlideDialog = function(options)
	{
		kff.widgets.SlideDialog.call(this, options);
	}

	kff.extend(kff.widgets.CalcSlideDialog, kff.widgets.SlideDialog);

	kff.widgets.CalcSlideDialog.prototype.show = function()
	{
		if(kff.widgets.SlideDialog.shown != null && kff.widgets.SlideDialog.shown != this) kff.widgets.SlideDialog.shown.hide();
		this.$element
			.css(this.dims)
			.css({ position: 'absolute', 'z-index':'9900', left: $('#logo').offset().left, top: $('#btn-calculator').offset().top + $('#btn-calculator').outerHeight() - 1 })
			.hide()
			.slideDown(200);
		kff.widgets.SlideDialog.shown = this;
	}

	/**
	 *  ShopSlideDialog
	 *  Třída pro dialogové okno nápovědy
	 */
	kff.widgets.ShopSlideDialog = function(options)
	{
		kff.widgets.SlideDialog.call(this, options);
	}

	kff.extend(kff.widgets.ShopSlideDialog, kff.widgets.SlideDialog);

	kff.widgets.ShopSlideDialog.prototype.show = function()
	{
		if(kff.widgets.SlideDialog.shown != null && kff.widgets.SlideDialog.shown != this) kff.widgets.SlideDialog.shown.hide();

		var $btn = $('#btn-nearest-shop');
		this.$element
			.css(this.dims)
			.css({ position: 'absolute', 'z-index':'9900', 'left': $btn.offset().left + $btn.outerWidth() - this.$element.outerWidth(), top: $btn.offset().top + $btn.outerHeight() - 1 })
			.hide()
			.slideDown(200);
		kff.widgets.SlideDialog.shown = this;
	}

	/* Slide dialog - kalkulátor */
	var shopSlideDialog = new kff.widgets.ShopSlideDialog({ element: '#dialog-shop' });
	shopSlideDialog.init();

	$('#btn-nearest-shop').bind('click', function(event)
	{
		$(this).toggleClass('expanded');
		$('#btn-calculator').removeClass('expanded');
		shopSlideDialog.toggle();
		return false;
	});


	/* Product detail slider */
	var galleryData = [];
	$('.product-detail-list .product-detail').each(function(){
		var item = { }, $this = $(this);
		item.id = $this.attr('id');
		item.imageUrlFull = $this.find('>.featured-img>img').attr('src');
		item.imageUrlSuperfull = $this.find('>.featured-img>img').attr('data-fullimg');
		item.desc = $this.find('.desc-box').html();
		galleryData.push(item);
	}).remove();

	$('.product-detail-list').append('<div class="product-detail"><div class="featured-img"></div><div class="desc-box"></div></div>');
	$('.product-detail').append('<div class="prevnext"><a href="#" class="prev">Předchozí</a><a href="#" class="next">Další</a></div>');
	$('.product-detail').append('<a href="#" class="zoom">další fotky</a>');

	
	
	var productGallery = new kff.widgets.WoodGallery(
		{
			data: galleryData,
			slideshowSpeed: 5000,
			domHooks: {
				desc: $('.product-detail .desc-box'),
				image: $('.product-detail .featured-img'),
				next: $('.product-detail .next'),
				prev: $('.product-detail .prev')
			}
		}
	);

	productGallery.render();
	
	$('.product-detail a.zoom').bind('click', function()
	{
		$a = $('.product-detail .product-images a');
		if($a.size() > 0)
		{
			$a.kfBox();
			$active = $a.size() >= 2 ? $a.eq(1) : $a.eq(0); 
			$active.trigger('click');
		}
		return false;
	});

	$('.product-detail-list').delegate('.product-detail .btn-demand', 'click', function()
	{
		var $this = $(this);
		var $loader = $('<div class="ajax-loader"></div>');
		var productId = $this.attr('data-productid');
		$this.hide().after($loader);
		$.ajax({
			type: "POST",
			url: "/?ajax&action=addProduct",
			data: "productid=" + productId,
			dataType: 'json',
			success: function(data)
			{
				$('html').animate({ scrollTop: 0 }, 300, function()
				{
					$loader.remove();
					$('.yellow-fade-box').remove();
					$this.show();
					$yellowFadeBox = $('<div class="yellow-fade-box">' + data.message + ' <img src="' + window.BASE_HREF + 'img/btn-remove.png" class="remove"/></div>');
					$('.breadcrumb').after($yellowFadeBox);
					$yellowFadeBox.animate({ backgroundColor: '#f7f7a5' }, 3000, function(){});
					$yellowFadeBox.find('img').bind('click', function(){
						$(this).closest('.yellow-fade-box').remove();
						return false;
					});
					var $bubble = $('#calc-count-bubble').show();
					$bubble.html(data.counter);
				});

			}
 		});
		return false;
	});

	var hash = location.hash;
	hash = hash.replace('#', '');

	var imageIndex = 0;
	$.each(galleryData, function(i, val){
		if(val.id == hash) imageIndex = i;
	});

	productGallery.showImage(imageIndex);

	/*
	$(window).bind( 'hashchange', function(e) {
		var hash = location.hash;
		that.setState(hash);
	});
	*/

	/**
	 *  RecommendSlideDialog
	 *  Třída pro dialogové okno doporučit známému
	 */
	kff.widgets.RecommendSlideDialog = function(options)
	{
		kff.widgets.SlideDialog.call(this, options);
		this.$alignTo = $(this.options.alignTo);
	}

	kff.extend(kff.widgets.RecommendSlideDialog, kff.widgets.SlideDialog);

	kff.widgets.RecommendSlideDialog.prototype.show = function()
	{
		if(kff.widgets.SlideDialog.shown != null && kff.widgets.SlideDialog.shown != this) kff.widgets.SlideDialog.shown.hide();
		this.$innerWrap.css({ position: 'absolute', top: '0', left: '0' }).css(this.dims);
		this.$element.css({ position: 'absolute', left: 'auto', top: null });
		this.$element
			.css(this.dims)
			.css({ position: 'absolute', left: this.$alignTo.offset().left, bottom: Math.floor($('body').outerHeight() - this.$alignTo.offset().top) })
			.hide()
			.slideDown(200);
		kff.widgets.SlideDialog.shown = this;
	}

	var recommendSlideDialog = new kff.widgets.RecommendSlideDialog({ element: '#recommend-box', alignTo: '.btn-recommend' });
	recommendSlideDialog.init();
	$(document).delegate('.btn-recommend', 'click', function(event)
	{
		var url = location.protocol + '//' + location.hostname + location.pathname + '#' + galleryData[productGallery.activeIndex].id;
		$('#rec-url').val(url);
		$(this).toggleClass('expanded');
		recommendSlideDialog.$alignTo = $(this);
		recommendSlideDialog.toggle();
		return false;
	});
	
	/* Rollovery na input a image: */
	var $img = $('<img/>');
	$img.attr('src', window.BASE_HREF + 'img/btn-remove-hover.png'); // preload	
	$('body').delegate('input.btn-remove, img.remove', 'mouseenter', function(){
		var src = $(this).attr('src');
		src = src.replace('.png', '-hover.png');
		$(this).attr('src', src);
	}).delegate('input.btn-remove, img.remove', 'mouseleave', function(){
		var src = $(this).attr('src');
		src = src.replace('-hover.png', '.png');
		$(this).attr('src', src);
	});
});

$(window).bind('load', function()
{
	fontSizeListener('#footer', equalizeHeights);
});

