$(function() {
	//fbg.hideFOUT('asap');
	
	// FAQ list
	$('dl.main-content__faq-list', $('#content')).each(function() {
				var jFaq = $(this);
				jFaq.find('>dd').hide();
				jFaq.find('>dt').click(function() {
							$(this).toggleClass('selected').next('dd').toggle();
						});
			});

	// Vertical hover init
	var oVerticalHover = new VerticalHover();

	/**
	 * I'll just leave it here.
	 */
	$(window).keydown(function(e) {
				if (e.keyCode === 84 && e.ctrlKey && e.altKey) {
					$('.sitemap-popup').toggleClass('sitemap-popup_whirl');
				}
			});

	/**
	 * Define gradient color
	 */
	$('.g-gradient-gold').gradientText({
				colors : ['#feb800', '#ffe100']
			});

			
	/**
	 * Footer teaser animation
	 */
	var jFooterTeaser = $('.footer__teaser-body');
	var jFooterTeaserImage = jFooterTeaser.find('img');

	jFooterTeaser.hover(function() {

				if (jFooterTeaserImage.is(':animated'))
					jFooterTeaserImage.stop();

				jFooterTeaserImage.animate({
							top : window.teaserOffset || $(this).height() / 2
						}, 500);

			}, function() {

				if (jFooterTeaserImage.is(':animated'))
					jFooterTeaserImage.stop();

				jFooterTeaserImage.animate({
							top : 0
						}, 200);

			});
			
	// Widget helper init
	var oWidgetHelper = new widgetHelper();
	
	// Placeholders init
//	$('input').addPlaceholder();
});

/**
 * 
 * A helper to implement vertical hover for tables.
 * @param {} options
 * 
 */

var VerticalHover = function(options) {

	var defaults = {
		container : $('.v-hover__container'),
		marker : $('.v-hover__marker')
	}

	this.options = $.extend({}, defaults, options ? options : {});

	this.jContainer = this.options.container;
	this.jMarker = this.options.marker;
	this.sMarkerClass = this.jMarker.attr('class');
	this.oPadding = this.options.padding;

	if (this.jMarker.length == 0) {
		this.jMarker = $('<div></div>').addClass('v-hover__marker')
				.prependTo(this.jContainer);
	}

	/* == Private == */
	var that = this;

	/* == Privileged == */
	this.init = function() {

		that.jContainer.each(function(index) {
					$(this).delegate('td, th', 'mouseenter', {
								container : $(this)
							}, $.proxy(that.highlightColumn, that));
				});

		that.jContainer.bind("mouseleave", $.proxy(that.hideMarkers, that));
	}

	/* == Call init == */
	this.init();
}

VerticalHover.prototype = {
	highlightColumn : function(e) {

		var currentContainer = e.data.container;
		var currentMarker = currentContainer.find('.' + this.sMarkerClass);
		var headerHeight = currentContainer.find('thead').outerHeight();

		var jCell = $(e.currentTarget);

		var markerLeft = jCell.offset().left - currentContainer.offset().left;

		currentMarker.css({
					left : markerLeft,
					// TODO: remove hardcoded value. What's it based on?
					top : headerHeight + 5,
					width : jCell.outerWidth(),
					height : currentContainer.outerHeight() - headerHeight
				});

		if (currentMarker.not(':visible'))
			currentMarker.show();
	},

	hideMarkers : function() {
		this.jMarker.hide();
	},

	isEdgeColumn : function(cell) {
		return ((!cell.next().length) ? 2 : // last
				((!cell.prev().length) ? 1 // first
						: 0)) // none
	}
}


/**
 * Sitemap popup object
 * @param {} options
 */

var SitemapPopup = function(options) {

	var defaults = {
		container : $('.sitemap-popup'),
		trigger : $('.pretty-button_type-sitemap'),
		closer : $('.sitemap-popup__close')
	}
	this.options = $.extend({}, defaults, options ? options : {});

	this.container = this.options.container;
	this.trigger = this.options.trigger;
	this.closer = this.options.closer;

	this.trigger.bind('mouseup', $.proxy(this.open, this));
	this.closer.click($.proxy(this.close, this));
}
SitemapPopup.prototype.open = function() {
	this.container.toggleClass('sitemap-popup_expanded');
}
SitemapPopup.prototype.close = function() {
	this.container.removeClass('sitemap-popup_expanded');
}

$(function() {
	new SitemapPopup;
});


/**
 * Helper object for slider widgets. Takes care of focus-on-drag behavior and hint bubbles.
 * @param {} options
 */

var widgetHelper = function(options) {
	var defaults = {
		container : '.slider-widget',
		widget : '.zf-slider',
		control : '.zf-slider-control',
		input : '.slider-widget__input',
		bubble : '.slider-widget__bubble',
		focusClass : 'focus'
	}

	/* == Public == */
	
	this.options = $.extend({}, defaults, options ? options : {});

	this.sContainer = this.options.container;
	this.sWidget = this.options.widget;
	this.sControl = this.options.control;
	this.sInput = this.options.input;
	this.sBubble = this.options.bubble;
	
	this.jWidgets = $(this.sWidget);
	this.jInputs = $(this.sInput);
	
	
	this.sFocusClass = this.options.focusClass;

	
	/* == Private == */
	var that = this;

	/**
	 * Adds .focus class to the container when the slider is being dragged
	 */
	var focusOnDrag = function(event) {
		event.stopPropagation();
		
		var jInput = $(this).parents(that.sWidget)
							.find(that.sInput)
							.addClass(that.sFocusClass);

		var unfocusSlider = function(e) {
			jInput.removeClass(that.sFocusClass);
		}

		$('html,' + that.sControl).one('mouseup',
				unfocusSlider);
	}

	/* == Privileged == */
	this.init = function() {
		if (typeof ZForms !== 'undefined') {
			// Bind ZForms events
			ZForms.attachObserver(ZForms.EVENT_TYPE_ON_INIT, function() {
				
				// Bind focus on drag				
				$(that.sControl).bind('mousedown', focusOnDrag);
				
				// Bind slider change event
				that.jInputs.each(function(index){
					var jInput = $(this),
						jForm = jInput.closest('form.zf'),
						jContainer = jInput.closest(that.sContainer),
						jWidget = jInput.closest(that.sWidget),
						oForm = ZForms.getFormById(jForm.attr('id')),
						oSlider = oForm.getWidgetById( jWidget.attr('id') );
					
					if(typeof oSlider !== 'undefined') { // Ignore if no slider
						var iMin = oSlider.dMin,
							iMax = oSlider.dMax,
							iRange = iMax - iMin;
							
						jInput.data('min_zone', false);
						jInput.data('max_zone', false);
						
						ZForms.attachObserver(ZForms.EVENT_TYPE_ON_CHANGE, 
							function(sEventType, oWidget) {
								var iVal = oWidget.getValue().get();
								if(iVal < iMin){
									that.showHint(jContainer, jInput, 'outofminrange');
									
								} else if (iVal < ( iMin + iRange / 20 ) ) {
									if( !jInput.data('min_zone') ) {
										jInput.data('min_zone', true);
										that.showHint(jContainer, jInput, 'min');
									}
									
								} else if (iVal > iMax) {
									that.showHint(jContainer, jInput, 'outofmaxrange');
									
								} else if (iVal > ( iMax - iRange / 20 ) ) {
									if( !jInput.data('max_zone') ) {
										jInput.data('max_zone', true);
										that.showHint(jContainer, jInput, 'max');
									}
									
								} else {
									jInput.data('min_zone', false);
									jInput.data('max_zone', false);
									that.hideHint();
								}
								
							}, oForm.getWidgetById( $(jInput).attr('id') ) 
						);
					}
				});
			});
		}
		
		$(this.sBubble).live('mousedown', function(event){
			event.stopPropagation();
		});
		
	}

	/* == Call init == */
	this.init();
}

/* == Public again == */
widgetHelper.prototype = {
	showHint : function(container, input, type) {
		var that = this;
		
		var jBubble = container.find('.slider-widget__bubble_' + type);
		var iHeight = $('body').height();
		
		var jClone = $('body').children('#' + input.attr('id') + '_slider-widget__bubble_' + type);
		
		if ( !jClone.length )
			jClone = jBubble.clone(true)
							.hide()
							.attr('id', input.attr('id') + '_slider-widget__bubble_' + type)
							.appendTo('body');
							
		jClone.css({
			position: 'absolute',
			bottom: iHeight - input.offset().top - input.outerHeight() / 2 + 29,
			left: input.offset().left - 154 ///TODO: remove hardcoded values
		});
		
		if (!jClone.is(':visible')) {

			jClone.fadeIn(300);
			
			$('body').one('mousedown', function(event){
				// To avoid capturing link click
				if(event.target.href){
					return true;
				}
				that.hideHint();
			});
			
		}
		
	},
	
	hideHint : function() {
		$('body').find('.slider-widget__bubble:visible').fadeOut(100);
	}
}
