
$.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; });
};

$(document).ready(function()
{
	$('#q').inputDefaultText({ text: 'Hledaný výraz'});
	$('#email').inputDefaultText({ text: 'Váš e-mail'});
	$('a.external').click(function(){return !window.open($(this).attr("href"))});
	
	$('.related-images a').kfBox();

	/* Zebra tables */
	$("table.params tr:nth-child(even)").addClass("even");

	$('.product-filter h2').bind('click', function(){
		$(this).toggleClass('collapsed').next().slideToggle(400);
	}).not('.expand').toggleClass('collapsed').next().hide();

	/* Featured articles sliding */
	if($.fn.cycle) $('.featured-articles').cycle({
		speed:  1000,
		timeout: 6000,
		pause: 1
	});

	$('.sticker').scrollTop(0);
	setTimeout(function(){ $('.sticker').animate({ scrollTop: 100 }, 500); }, 2500);

	/* "Problémové" iPod style menu na titulce */
	(function()
	{
		var submenuWidth = 218;
		var currentSubmenu = 0;

		var scrollSubmenu = function($submenu, i, speed)
		{
			speed = speed || 300;
			$submenu.animate({scrollLeft: submenuWidth * i}, speed);
			currentSubmenu = i;
		}

		$('#problem-bar .submenu').css('display', 'none');

		$('#problem-bar>ul>li')
			.bind('click', function(event){
					$submenu = $(this).find('.submenu').css({ display: 'block' }).scrollLeft(submenuWidth);
					currentSubmenu = 1;
					$(this).bind('mouseleave', function(){ $submenu.hide();	});
					return false;
				})
			.each(function(){
				$(this)
					.find('.submenu ul')
						.slice(1)
						.append('<li><a href="#" class="back">Zpět</a></li>')
						.find('a.back')
							.bind('click', function(){
								scrollSubmenu($submenu, currentSubmenu - 1);
								return false;
								});
				});

		$('#problem-bar .submenu li').bind('click', function(){
				$(this).parent().find('li').not(this).find('ul').css({ display: 'none' });
				$(this).find('>ul').css({ display: 'block' }).each(function(){ scrollSubmenu($submenu, currentSubmenu + 1); });
				return false;
			}
		);

		$('#problem-bar .submenu li a').not('.has-children').bind('click', function(event){
				event.stopPropagation();
				return true;
			}
		);

	})();

	/* Akční proužek */
	if(typeof actions != 'undefined')
	{
		var actionBar = new ActionBar({ container: '.action-bar', config: actions });
		actionBar.run();
	}
	
	/* Scroller */
	var $prevnext = $('<div class="prevnext"><a href="#" class="btn-prev">předchozí</a> <a href="#" class="btn-next">další</a></div>');
    $('.partners-carousel').append($prevnext);
    var partnersScroller = new kff.widgets.Scroller('.partners-carousel .hscroll-box', {
		scrollWidth: 142 * 2 + 44 * 2,
		carouselWidth: 142 * 2 + 44,
		prev: '.partners-carousel .btn-prev',
		next: '.partners-carousel .btn-next'
	});
	partnersScroller.activate();
	
	if($('body').hasClass('home'))
	{
		var homepageController = new HomepageController();
		homepageController.init();
	}

});

var ActionBar = function(options)
{
	this.options = options;
	this.$actionBar = $(this.options.container);	
}

ActionBar.prototype.renderBar = function()
{
	this.$actionBar
		.addClass('action-bar-active')
		.append('<h2><a href="#"></a></h2><div class="inner"></div>');
	this.$headerLink = this.$actionBar.find('h2 a');
	this.$content = this.$actionBar.find('.inner');
	this.contentBoxes = [];
}

ActionBar.prototype.renderContent = function()
{
	var that = this;
	var html = [];
	$.each(this.options.config, function(i, val){
		html.push('<li><div class="first"><h3>Aktuální akce:</h3><h4><a href="' + val.url + '">' + val.name + '</a></h4></div><div><h3>Do soutěže zbývá:</h3><h4></h4></div></li>');		
	});
	this.$content.append('<ul>' + html.join('') + '</ul>');
	this.$content.find('li').each(function(i){
		that.contentBoxes[i] = $('div:eq(1)', this);
	});
}

ActionBar.prototype.renderTime = function(time)
{
	var hours = parseInt(time / (1000 * 60 * 60));
	var minutes = parseInt(time / (1000 * 60) % 60);
	var seconds = parseInt(time / 1000 % 60);
	if(seconds.toString().length == 1) seconds = '0' + seconds;
	if(minutes.toString().length == 1) minutes = '0' + minutes;
	return hours + ':' + minutes + ':' + seconds;
}

ActionBar.prototype.timer = function()
{
	var that = this;
	$.each(that.options.config, function(i, val){
		var $currentBox = that.contentBoxes[i];
		var timestamp = (new Date()).getTime(); 
		var html = '';
		
		if(timestamp < val.startTime) html = '<h3>Do soutěže zbývá:</h3><h4>' + that.renderTime(val.startTime - timestamp) + '</h4>';
		else if(timestamp < val.endTime) html = '<h3>Do konce soutěže zbývá:</h3><h4>' + that.renderTime(val.endTime - timestamp) + '</h4>';
		else html = '<h3>Soutěž skončila před:</h3><h4>' + that.renderTime(timestamp - val.endTime) + '</h4>';
		
		$currentBox.html(html);		
	});
	
	setTimeout(function(){ that.timer(); }, 250);
}

ActionBar.prototype.changeLink = function(currSlideElement, nextSlideElement, options, forwardFlag)
{
	var url = $(nextSlideElement).find('a').attr('href');
	this.$headerLink.attr('href', url);
}

ActionBar.prototype.run = function()
{
	var that = this;
	this.renderBar();
	this.renderContent();
	
	if($.fn.cycle) this.$content.find('ul').cycle({
		speed:  700,
		timeout: 5000,
		pause: 1,
		before: function(currSlideElement, nextSlideElement, options, forwardFlag){
			that.changeLink.call(that, currSlideElement, nextSlideElement, options, forwardFlag);
		}
	});
	
	this.timer();
}


var HomepageController = function()
{
	$('.home-references').after('<div class="reference-slider-box"><a href="#" class="btn-prev">předchozí</a><a href="#" class="btn-next">další</a></div>');
	this.refSlider = new kff.widgets.Slider('.reference-slider-box', {
		steps: 3,
		min: 0,
		max: 3,
		position: 0,
		//change: $.proxy(this.change, this),
		drag: $.proxy(this.change, this),
		speed: 150
	});
	this.timeout = 5000;
	this.active = 3;
	this.activeMotive = 0;
	this.$spBoxes = $('#signpost .sp-box');
	this.$refBoxes = $('.home-references .hscroll-box');
	this.$headerBox = $('.ref-header-box');
	this.$motiveContainers = $('.large-motives *');
	this.$activeContainer = 0;
	this.$motiveImages = window.motiveImages || [
		'../img/large-motive-01.jpg',
		'../img/large-motive-02.jpg',
		'../img/large-motive-01.jpg',
		'../img/large-motive-02.jpg'
	];
	this.$headerImages = window.headerImages || [
		'../img/txt-h2-vybrane-reference.png',
		'../img/txt-h2-spickove-naroky.png',
		'../img/txt-h2-ce-podporuje.png',
		'../img/txt-h2-oborova-reseni.png'
	];
	this.$btnPrev = $('.reference-slider-box .btn-prev');
	this.$btnNext = $('.reference-slider-box .btn-next');
	
	$.xLazyLoader({
		img: [this.$motiveImages[0]],
		success: $.proxy(function(){
			$('.large-motives .m01').css({ backgroundImage: 'url("' + this.$motiveImages[0] + '")'});
		},this)
	});
	
	$.xLazyLoader({
		img: this.$motiveImages,
		success: $.proxy(this.motivesLoad, this)
	});
}

HomepageController.prototype.init = function()
{	
	this.refSlider.init();
	this.$btnPrev.bind('click', $.proxy(function(){
		this.change((this.active - 1 + 4) % 4);
		return false;
	}, this));
	this.$btnNext.bind('click', $.proxy(function(){
		this.change((this.active + 1) % 4);
		return false;
	}, this));
	
	$('.home-references .hscroll-box').css({ overflow: 'hidden' });
	
	this.timer = null;
	// this.animate();
}

HomepageController.prototype.motivesLoad = function()
{
	setInterval($.proxy(function(){
		this.activeMotive = (this.activeMotive + 1) % 4;
		this.changeMotive(this.activeMotive);
	}, this), 8000);
}

HomepageController.prototype.animate = function()
{
	clearTimeout(this.timer);
	this.change((this.active + 1) % 4);
}

HomepageController.prototype.changeMotive = function(i)
{
	this.$motiveContainers.eq((this.$activeContainer + 1) % 2).stop().css({ backgroundImage: 'url("' + this.$motiveImages[i] + '")', opacity: 0, zIndex: 1 }).fadeTo(2000, 1);
	this.$motiveContainers.eq(this.$activeContainer).css({ zIndex: 0 }).stop();
	this.$activeContainer = (this.$activeContainer + 1) % 2;
}

HomepageController.prototype.changeBox = function(i)
{
	var oldI = this.active;
	var newI = i % 4;
	
	setTimeout($.proxy(function(){
		this.$spBoxes.eq(newI).find('.top').stop().animate({ top: 0 }, 1500);
		if(newI != oldI) this.$spBoxes.eq(oldI).find('.top').stop().animate({ top: 30 }, 1500);
	}, this), 200);
}

HomepageController.prototype.changeRefs = function(i)
{
	this.$refBoxes.stop().animate({ scrollLeft: (i * 711) }, 1500);
}

HomepageController.prototype.changeHeaders = function(i)
{
	this.$headerBox.find('img').stop().fadeTo(1500, 0, function(){ $(this).remove(); });
	var $newImg = $('<img/>').attr('src', this.$headerImages[i]).css({opacity: 0});
	this.$headerBox.append($newImg);
	$newImg.fadeTo(1500, 1);
}

HomepageController.prototype.change = function(i)
{
	clearTimeout(this.timer);
	//this.changeBox(i);
	// this.changeMotive(i);
	this.changeRefs(i);
	this.changeHeaders(i);
	this.refSlider.setPosition(i);
	this.active = i;
	// this.timer = setTimeout($.proxy(this.animate, this), this.timeout);
}
