﻿/* Author: 
	Kai Hommel <kaho@db-n.com>, 
	Björn Johann <bjjo@db-n.com>
*/

var LAETTA = {
	defaults: {
		header: '#site_header',
		main: '#main',
		content: '#content',
		footer: '#site_footer',
		shim: '#shim',
		shimHtml: '<div id="shim"></div>'
	},
	init: function() {
		var _this = this;
		_this.currentSlide = 0;
		
		if(checkIE() > 7) { // position the main container in the center of the page for each resolution - only IE > 7 
			_this.buildShim();
			_this.setMainHeight(_this.defaults.main);
			$(window).resize(function() { // on resize call the function setMainHeight
				_this.setMainHeight(_this.defaults.main);
			});
		}
		
		_this.setSupersizedBg(_this.currentSlide);
		_this.setCycle('#slideshow', '#slideshow_nav', _this.currentSlide);
		
		$(window).resize(function() {
			
			$('#slideshow').css({'width':'100%'});
			$('#slideshow').find('slide').css({'width': 'auto', 'height': 'auto'});
			_this.setCycle('#slideshow', '#slideshow_nav', _this.currentSlide);
			
			api.goTo(_this.currentSlide+1);
			
		});
		
		if($('.recipe_search_teaser').length) {
			LAETTA.RecipeSearch.init($('.recipe_search_teaser'));
		}
		
		if($('.tvSpot').length) {
			LAETTA.videoLayer.init($('.tvSpot'));
		}
		
		if($('.jsLayer').length) {
			LAETTA.gernericLayer.init($('.jsLayer'));
		}
		
	},
	buildShim: function() { // function to build invisible shim for center position vertical
		var _this = this;
		$(_this.defaults.shimHtml).insertBefore(_this.defaults.content);
		$(_this.defaults.shim).css({'margin-top': '-'+(parseInt($(_this.defaults.content)[0].offsetHeight) / 2) +'px'});
	},
	setMainHeight: function(id) { // function to set the container #main height by window height 
		var _this = this, height = 0;
		height = $(window).height() - $(_this.defaults.header)[0].offsetHeight - $(_this.defaults.footer)[0].offsetHeight; // minus header height and footer height
		if(height > $(_this.defaults.content)[0].offsetHeight) { // if calculated height > #content height set calculated height to #main else set auto
			$(id).css({'height': height+'px'});
		} else {
			$(id).css({'height': 'auto'});
		}
	},
	setSupersizedBg: function(start) {
		var _this = this, images = [];
		
		$('.slides').find('.slide').each(function(index) {
			var $this = $(this);
			images[index] = {};
			images[index]['image'] = $this.data('background');
		});
		
		$.supersized({
			// Functionality
			autoplay				:	0,			// Slideshow starts playing automatically
			transition              :   1, 			// 0-None, 1-Fade, 2-Slide Top, 3-Slide Right, 4-Slide Bottom, 5-Slide Left, 6-Carousel Right, 7-Carousel Left
			transition_speed		:	250,		// Speed of transition
			new_window				:	0,			// Image links open in new window/tab
			keyboard_nav            :   0,			// Keyboard navigation on/off
			// Size & Position						   
			/*min_width		        :   1280,		// Min width allowed (in pixels)
			min_height		        :   900,		// Min height allowed (in pixels)*/
			horizontal_center       :   1,			// Horizontally center background		   
			// Components							
			slides 					:  	images
		});
	},
	setCycle: function(id, nav, start) {
		var _this = this;
		
		$(id).cycle({
			speed:				500,
			fx:					'fade',
			startingSlide:		start, 
			pause:				true,
			before: function(curr, next, options) {
				var $curr = $(curr), $next = $(next);
				//_this.unbindHandlers();
				if($curr.data('slide') !== $next.data('slide')) {
					_this.currentSlide = options.nextSlide;
					api.goTo($next.data('slide'));
				}
			}/*,
			after: function() {
				_this.bindHandlers(id);
			}*/
		}).cycle('pause');
		
		_this.bindHandlers(id);
		
	},
	bindHandlers: function(id) {
		log("bindHandlers", id);
		var _this = this;
		$('.teaser_group').find('.teaser a').on('mouseenter', function() {
			var $this = $(this);
			$(id).cycle('pause').cycle($this.data('slide')-1);
		});
	}/*,
	unbindHandlers: function() {
		log("unbindHandlers");
		var _this = this;
		$('.teaser_group').find('.teaser a').off('mouseenter');
	}*/
};

LAETTA.RecipeSearch = {
	defaults: {
		id: 'recipe_search', 
		toggleAttr: 'toggle',
		toggleCont: 'togglecontent',
		toggleBoxClass: 'toggle_box_link',
		toggleContentClass: 'toggle_content_link',
		categories: '.recipe_search_categories',
		label: '.cat_group',
		speed: 0
	},
	init: function(e, options) {
		var _this = this;
				
		if(options) {
			$.extend(_this.defaults,options);
		}
		
		$(e).identify(_this.defaults.id);
		
		$(e).each(function() {
			var id = '#'+$(this).attr('id');
			_this.box = $(id);
			_this.setRecipeSearch(id);
		});
	},
	setRecipeSearch: function(id) {
		var _this = this, selectBox = _this.box.find('select');
		
		if(_this.box.data(_this.defaults.toggleAttr) === true) {
			_this.addToggleLink(id, _this.defaults.toggleBoxClass);
		}
		
		_this.box.find(_this.defaults.categories).each(function() {
			var $this = $(this);
			_this.buildDropdown($this);
			// set selected item
			var $current = $this.find('input:checked');
			if($current.length){
				$this.find(_this.defaults.label).html($current.next().text());
			};
		});
		
	},
	buildDropdown: function(cat) {
		var _this = this;
		
		cat.identify('cat');
		
		cat.find('label').click(function(e) {
			cat.find(_this.defaults.label).html($(this).text());
			_this.handleToggle('#'+cat.attr('id'), _this.defaults.speed);
		});
		
		_this.addToggleLink('#'+cat.attr('id'), _this.defaults.toggleContentClass);
		
	},
	addToggleLink: function(id, className) {
		var _this = this, box = $(id);
		
		box.append('<a href="javascript:void(0);" class="'+className+' '+className+'_arrow">toggle</a>');
		
		box.find('.'+className+':eq(0), .'+className+'_arrow:eq(0)').live('click', function(e) {
			e.preventDefault();
			var content = $(this).parents(id);
			_this.handleToggle(content, _this.defaults.speed);
		});
		
		_this.handleToggle(id, 0);
	},
	handleToggle: function(id, speed) {
		var _this = this, box = $(id), toggleContent = box.data(_this.defaults.toggleCont);
		if(toggleContent) {
			if(box.hasClass('open')) {
				box.removeClass('open');
				box.find(toggleContent).stop(false, true).slideUp(speed, function() { box.find(toggleContent).hide(); });
			} else {
				box.addClass('open');
				box.find(toggleContent).stop(false, true).slideDown(speed, function() { box.find(toggleContent).show(); });
			}
		} else {
			log("nothing to toggle, set data-toggleContent to id");
		}
	}
};

LAETTA.videoLayer = {
	init: function(link){
		link.fancybox({
			padding:0,
			onComplete:function(){
				$('#tvSpot video').VideoJS({controlsHiding:false});
				$('.vjs-fullscreen-control').live('click', function(){
					$('#fancybox-close').css('position', ($('#tvSpot .vjs-fullscreen').length)? 'fixed':'absolute');
				})
			}
		});
	}
};

LAETTA.gernericLayer = {
	init: function(link){
		link.fancybox({
			padding:0,
			overlayOpacity:0.3,
			onStart: function(){
				$('#fancybox-wrap').addClass('layer');
			}
		})
	}
};

var Unilever = {};

Unilever.Layer = {
	basic: function() {
		var BasicLayer = $('#basicLayer');
		$('a.openlayer').live('click', function(e) {
			var uri = $(this).attr('href');
			e.preventDefault();
			BasicLayer.show();
			$('#main').addClass('overlay');
			BasicLayer.find('.box-layer').load(uri + ' #content');
			
			BasicLayer.find('a.openInLayer').live('click', function(e) {
				var uri = $(this).attr('href');
				e.preventDefault();
				BasicLayer.find('.box-layer').load(uri + ' #content');
			});
		});
		BasicLayer.find('a.close').live('click', function(e) {
			e.preventDefault();
			BasicLayer.find('.box-layer').html('Lade...');
			BasicLayer.hide();
			$('#main').removeClass('overlay');
		});
	},
    brand: function() {
        var BrandsLayer = $('#unileverBrands');
        $('a#linkUnileverGroup').live('click', function(e) {
            e.preventDefault();
            BrandsLayer.show();
			$('#main').addClass('overlay');
            BrandsLayer.find('a.close').live('click', function(e) {
                e.preventDefault();
                BrandsLayer.hide();
				$('#main').removeClass('overlay');
            });
        });
    },
    code: function() {
        var CodeLayer = $('#codeHelp');
        $('a.captchaHelpLayerTrigger').live ('click', function(e) {
            e.preventDefault();
            CodeLayer.show();
			$('#main').addClass('overlay');
            CodeLayer.find('a.close').live('click', function(e) {
                e.preventDefault();
                CodeLayer.hide();
				$('#main').removeClass('overlay');
            });
        });
    }
};

/* Sweepstake */
Unilever.ExclusiveData = {
	all: 'exclusive-all',
	only: 'exclusive-only',
	init: function() {
		var _this = this;
		$('input.exclusive').live('change', function() {
			_this.checkFormFields($(this));
		});
	},
	checkFormFields: function(e) {
		var _this = this;
		if(e.attr('checked') === 'checked') {
			if(e.hasClass(_this.all)) {
				$('input.'+_this.only).attr('checked', false);
			} else {
				$('input.'+_this.all).attr('checked', false);
			}
		}		
	}
};

$(function() {
	LAETTA.init();
	
	Unilever.Layer.basic();
    Unilever.Layer.brand();
    Unilever.Layer.code();
    
  if($('input.exclusive').length) {
		Unilever.ExclusiveData.init();
	}
});
