var BASE_HREF = BASE_HREF || '../';

kff.widgets.scrollMotive = function(element, options){
	var hash = $(window.location.hash).index();	
	
	this.doc = $(document);
	this.element = $(element);
	this.frames = this.element.find('.frame');
	this.current = (hash == -1) ? 0 : hash;
	this.length = this.frames.length;
	this.options = $.extend({ 
		btnMenu: '.frame-menu a',
		btnNext: '.frame-pager .next',
		btnPrev: '.frame-pager .prev',
		auto: true,
		timeout: 5000
	}, options);
	
};
kff.widgets.scrollMotive.prototype.init = function() {
	if(this.length < 2){
		return;
	}
	this.element.attr('scrollLeft', this.frames.eq(this.current).attr('offsetLeft'));
	this.activeMenu();
	this.doc
		.delegate(this.options.btnNext, 'mousedown', $.proxy(this.next, this))
		.delegate(this.options.btnPrev, 'mousedown', $.proxy(this.prev, this))
		.delegate(this.options.btnMenu, 'mousedown', $.proxy(this.menu, this))
		.delegate(this.options.btnNext + ', '+ this.options.btnPrev + ', ' + this.options.btnMenu, 'click', function(){return false;});
	this.resetInterval();
};
kff.widgets.scrollMotive.prototype.interval = function()
{
	this.next();
	this.resetInterval();
}
kff.widgets.scrollMotive.prototype.resetInterval = function()
{
	if(this.options.auto){
		clearTimeout(this.mainTimer);
		this.mainTimer = setTimeout($.proxy(this.interval, this), this.options.timeout);
	}
}
kff.widgets.scrollMotive.prototype.scroll = function() {
	this.element.stop().animate({
		scrollLeft: this.frames.eq(this.current).attr('offsetLeft')
	}, 500);
	this.activeMenu();
	this.resetInterval();	
};
kff.widgets.scrollMotive.prototype.activeMenu = function() {
	$(this.options.btnMenu)
		.parent()
			.removeClass('active')
			.end()
		.eq(this.current)
			.parent()
			.addClass('active');	
};
kff.widgets.scrollMotive.prototype.next = function() {
	this.current = (this.current + 1) % this.length;
	this.scroll();
};
kff.widgets.scrollMotive.prototype.prev = function() {
	this.current = (this.current - 1 + this.length) % this.length;
	this.scroll();
};
kff.widgets.scrollMotive.prototype.menu = function(e) {
	this.current = $(e.currentTarget).parent().index();
	this.scroll();
};


$.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()
{
	$('.page-home #main').kfEqualizeColumns({ column: '.std-box' });
};

$.fn.inputDefaultText = function(options)
{
	options = $.extend({
		text: 'Hledany vyraz'
	}, options);
	
	return this.each(function()
	{
		var $this = $(this);
		if($this.val() == options.text || $this.val() == '') $this.val(options.text);
		$this
			.bind('focus', function(){ if($(this).val() == options.text) $(this).val(''); })
			.bind('blur', function(){ if($(this).val() == '') $(this).val(options.text); });
		
		$this.closest('form').bind('submit', function(){
			if($this.val() == options.text) $this.val('');
		});
	});
};

$(document).ready(function()
{
	var langCode = $("html").attr("xml:lang") || 'cs';
	var lang = {
		'cs': {
			'Hledaný výraz': 'Hledaný výraz'
		},
		'en': {
			'Hledaný výraz': 'Search term'
		},
		'de': {
			'Hledaný výraz': 'Suchbegriff'
		}		
	};

	$('a.lightbox').kfBox();
	$('.att-images').each(function(){ 
		$(this).find('a').kfBox();
	});
	$('a.external').click(function(){return !window.open($(this).attr("href"))});
	$("a.print").bind("click", function() { window.print(); return false; });
	$('table tr:nth-child(even)').addClass('even');
	$('#q').inputDefaultText({ text: lang[langCode]['Hledaný výraz']});
	$('#q-products').inputDefaultText({ text: lang[langCode]['Hledaný výraz']});
	
	motive = new kff.widgets.scrollMotive('.frames');
	motive.init();

	/*
	$('#demandform').each(function(){

		var lInput = $('.col-twt input:text', this);
		var rInput = $('.col-tht input:text', this);
		var check = $('#demandform-partner', this);
		
		check
			.bind('control', function(){
				if($(this).attr('checked')){
					lInput.attr('disabled', true).addClass('disabled');
					rInput.attr('disabled', false).removeClass('disabled');
				}
				else{
					lInput.attr('disabled', false).removeClass('disabled');
					rInput.attr('disabled', true).addClass('disabled');	
				}
			})
			.bind('change', function(){
				$(this).trigger('control');
			})
			.trigger('control');
	})
	*/
	
	$('.data-grid').each(function(index) {
		
		var $info = $('#more-info').attr('tabindex', '0');
		
		$(this)
			.delegate('.title a', 'click', function(){
				$(this).closest('tr').find('ul').slideToggle();
				return false;	
			})
			.delegate('.phone', 'hover', function(e){
				if(e.type == 'mouseover'){
					$info
						.css({
							'top': $(this).offset().top + $(this).width(),
							'left': $(this).offset().left + $(this).height()	
						})
						.show()
						.focus();	
				}
				else{
					$info.hide();
					$(this).focus();
				}
			})
			.delegate('.phone', 'click', function(){
				return false;	
			})
			.delegate('.desc a', 'click', function(){
				$(this).parent().hide();
				$(this).closest('tr').find('.complet-desc').show();
				return false;	
			})
	});

});

$(window).bind('load', function()
{
	fontSizeListener('#footer', equalizeHeights);
});
