var Page = {

	init: function(){
		this.bindEvents();
		if( $('.section_gallery').length == 1 )
		{
			SectionGallery.init();
		}
		
		Page.initialFooterHeight = $('#footer').height();
		Page.adjustContentHeight();
		
		Page.toggleNewTags();
		
		Page.searchFieldHelper();
	},
	
	toggleNewTags: function(){
		$('.new').each(function(){
			var _this = $(this),
				_val  = _this.html();
				
			_val = $.trim(_val);
			
			if( _val.length < 1 )
			{
				_this.remove();
			}
		});
	},
	
	bindEvents: function(){
		
		// PRODUCT INFO GALLERY
		// $('.product_gallery .thumb').click(function(){
		// 	var _this = $(this),
		// 		_parent = _this.parent();
		// 	if( !_parent.hasClass('current') )
		// 	{
		// 		_parent.addClass('current').siblings().removeClass('current');
		// 	}
		// });
		
		// Adjust footer on window resize
		$(window).resize(function(){
			Page.adjustContentHeight();
		});
		
		
		// PRODUCT DETAIL INFO TABS
		$('.info_tabs .heading').click(function(){
			var _this = $(this),
				_parent = _this.parent();
			if( !_parent.hasClass('current') )
			{
				_parent.addClass('current').siblings().removeClass('current');
			}
		});
	},
	
	adjustContentHeight: function(){
		
		
		var _bodyHeight   = $(window).height(),
			_pageHeight   = $('.page .header_menu').outerHeight() + $('.page .content_wrap').outerHeight(),
			_footerHeight = $('#footer').height(),
			_heightDiff   = _bodyHeight - _pageHeight - Page.initialFooterHeight;
		
		if( $('body').hasClass('landing_page') )
		{
			_heightDiff = _bodyHeight - $('.page .intro').outerHeight() - Page.initialFooterHeight;
		}
			
		if( _heightDiff > 0 )
		{
			$('#footer').height(Page.initialFooterHeight+_heightDiff);
		}
	},
	
	searchFieldHelper: function(){
		var defString = "Site Search";
		
		$('#site_search input[name=term]').focus(function(){
			var _this = $(this),
				_val  = _this.val();
				
			_val = $.trim(_val);	
				
			if( _val === defString ){
				_this.val('');
			}
		
		}).blur(function(){
			var _this = $(this),
				_val  = _this.val();
			
			_val = $.trim(_val);
				
			if( _val === '' ){
				_this.val(defString);
			}
		});
	}
};

var SectionGallery = {
	current: 0,
	count: 0,
	autoPlayMode: true,
	fadeSpeed: 1250,
	autoPlayTime: 4000,
	
	init: function(){
		this._g     = $('.section_gallery');
		this._items = this._g.find('.gallery_item');
		this.count  = this._items.length;
		
		this.createNav();
		
		setTimeout(SectionGallery.autoPlay, 5000);
	},
	
	bindEvents: function(){
		
	},
	
	createNav: function(){
		SectionGallery._nav = SectionGallery._g.find('.nav_dots');
		
		for(var i=0; i<SectionGallery.count;i++)
		{
			SectionGallery._nav.append('<div />');
		}
		SectionGallery._nav.children().first().addClass('current');
		
		SectionGallery._nav.children().click(function(){
			var _index = $(this).index();
			
			SectionGallery.goTo(_index);
			SectionGallery.autoPlayMode = false;
		});
	},
	
	updateNav: function(){
		SectionGallery._nav.children().removeClass('current').eq(SectionGallery.current).addClass('current');
	},
	
	goTo: function(to){
		if( SectionGallery.current > to )
		{
			SectionGallery._items.eq(to).show().children('h3').hide();
			SectionGallery._items.eq(to).children('h3').delay(SectionGallery.fadeSpeed).fadeIn(200);
			SectionGallery._items.eq(SectionGallery.current).fadeOut(SectionGallery.fadeSpeed);
			SectionGallery.current = to;
			SectionGallery.updateNav();
		}
		else if( SectionGallery.current < to )
		{
			SectionGallery._items.eq(to).children('h3').css('display','block');
			SectionGallery._items.eq(SectionGallery.current).children('h3').fadeOut(200, function(){
				SectionGallery.current = to;
				SectionGallery.updateNav();
				SectionGallery._items.eq(to).fadeIn(SectionGallery.fadeSpeed, function(){
					SectionGallery._items.eq(SectionGallery.current).siblings().hide();
				});
			});
			
		}
		
		// if( SectionGallery.current > to )
		// {
		// 	SectionGallery._items.eq(to).show();
		// 	SectionGallery._items.eq(SectionGallery.current).fadeOut(SectionGallery.fadeSpeed);
		// 	SectionGallery.current = to;
		// }
		// else if( SectionGallery.current < to )
		// {
		// 	SectionGallery.current = to;
		// 	SectionGallery._items.eq(to).fadeIn(SectionGallery.fadeSpeed, function(){
		// 		SectionGallery._items.eq(SectionGallery.current).siblings().hide();
		// 	});
		// }
		
		
	},
	
	autoPlay: function(){
		if( SectionGallery.autoPlayMode )
		{
			var _next = SectionGallery.current + 1;
			if( _next > (SectionGallery.count-1) )
			{
				_next = 0;
			}
			SectionGallery.goTo(_next);
			
			setTimeout(SectionGallery.autoPlay, SectionGallery.autoPlayTime);
		}
	}
};


var ImageZoom = {
	
	imgWidth: 0,
	imgHeight: 0,
	divWidth: 0,
	divHeight: 0,
	
	_wrapDimensions: {w:0,h:0},
	_imgDimensions: {w:0,h:0},
	_maxLeft: 0,
	_maxTop: 0,
	_lightboxFallback: 50,
	
	init: function(){
		ImageZoom._gallery = $('.image_gallery');
		if( ImageZoom._gallery.length === 1 )
		{
			ImageZoom._items = ImageZoom._gallery.find('.gallery_item');
			ImageZoom._relatedItemWrap = ImageZoom._gallery.find('.related_items');
			ImageZoom._relatedItems = ImageZoom._gallery.find('.related_items .item_pair');
			ImageZoom._items.first().addClass('current');
			ImageZoom.toggleZoomIcon();
			ImageZoom._count = ImageZoom._items.length; // counting gallery items
			
			var _div = ImageZoom._gallery.find('.standard_img').first();
			ImageZoom.divWidth  = _div.width();
			ImageZoom.divHeight = _div.height();
			
			// ImageZoom.addZoomedImages();
			ImageZoom.bindEvents();
			
			ImageZoom.createLightboxGallery();
		}
	},
	
	bindEvents: function(){
		$('#image_gallery_tools').delegate('.tool','click', function(){
			var _this = $(this),
				_itemIndex = ImageZoom._gallery.children('.item_wrap').children('.current').index();
			
			if( _this.hasClass('related_toggle') )
			{
				// TOGGLE RELATED ITEMS
				_this.toggleClass('show_more');
				if( _this.hasClass('show_more') )
				{
					ImageZoom._gallery.addClass('related_hidden');
					ImageZoom._relatedItemWrap.animate({ top : '340px' }, 500, function(){
						ImageZoom._relatedItems.hide();
					});
				}
				else
				{
					ImageZoom._gallery.removeClass('related_hidden');
					ImageZoom._relatedItemWrap.animate({ top : '445px' }, 500);
					ImageZoom._relatedItems.eq(_itemIndex).show();
				}
			}
			else if( _this.hasClass('zoom_in') )
			{
				// ACTIVATE ZOOM MODUS
				ImageZoom._gallery.removeClass('thumbmode');
				ImageZoom.zoomOn();
			}
			else if( _this.hasClass('zoom_out') )
			{
				// DEACTIVATE ZOOM MODUS
				ImageZoom.zoomOff();
			}
			else if(_this.hasClass('thumbs') )
			{
				// THUMBNAIL MODE
				ImageZoom._gallery.removeClass('zoom_on');
				ImageZoom._gallery.addClass('thumbmode');
				ImageZoom._items.show();
				ImageZoom._gallery.find('.arrow').hide();
			}
			else if(_this.hasClass('fullscreen') )
			{
				// LIGHTBOX
				$('body').addClass('lightbox_active');
				ImageZoom._lightbox.children().hide().eq(_itemIndex).show();
			}
		});
		
		// INIT ZOOME IMAGE
		ImageZoom._gallery.delegate('.zoomed_img','mouseenter', function(){
			var _zoomedImg = $(this);
			
			if( _zoomedImg.parents('.zoom_on').length === 1 )
			{
				ImageZoom.initValues(_zoomedImg);
			}
		});
		
		// CALL ZOOMED IMG FN
		ImageZoom._gallery.find('.zoomed_img').mousemove(function(e){
			ImageZoom.positionZoomedImage(e,$(this));
		});

		// ENLARGE THUMB
		ImageZoom._gallery.delegate('.gallery_item','click', function(){
			if( ImageZoom._gallery.hasClass('thumbmode') )
			{
				var _this = $(this),
					_index = _this.index();

				_this.siblings().removeClass('current').hide();
				_this.addClass('current');
				_this.show();
				_this.css('display','block');
				
				ImageZoom._gallery.removeClass('thumbmode');
				ImageZoom._relatedItems.hide();
				ImageZoom._relatedItems.eq(_index).css('display','block');
			}
		});
		
		ImageZoom._gallery.mousemove(function(){
			if( !$(this).hasClass('thumbmode') )
			{
				$(this).find('.arrow').show();
			}
		})
		.mouseleave(function(){
			$(this).find('.arrow').hide();
		});
		
		ImageZoom._gallery.delegate('.arrow','click', function(){
			
			ImageZoom.zoomOff();
			
			if( $(this).hasClass('prev') )
			{
				ImageZoom.prevImage();
			}
			else
			{
				ImageZoom.nextImage();
			}
		});
		
	},
	
	addZoomedImages: function(){
		$('.zoomed_img').each(function(){
			var _this = $(this),
				_a    = _this.children('a');
				
			if( _a.length === 1 )
			{
				_this.html('<img src="' + _a.attr('href') + '" />');
			}
			
		});
	},
	
	initImage: function(_div){
		var _img = _div.children('img').first();
		
		ImageZoom.imgWidth  = _img.width();
		ImageZoom.imgHeight = _img.height();
	},
	
	initValues: function(elem){

		var _img = elem.children('img');
		
		ImageZoom._wrapDimensions.w = elem.width();
		ImageZoom._wrapDimensions.h = elem.height();
		
		ImageZoom._imgDimensions.w = _img.width();
		ImageZoom._imgDimensions.h = _img.height();
		
		ImageZoom._maxLeft = elem.width() - _img.width();
		ImageZoom._maxTop  = elem.height() - _img.height();
		
		// console.log('initValues:');
		// console.log('wrap: '+ImageZoom._wrapDimensions.w+'x'+ImageZoom._wrapDimensions.h);
		// console.log('img: '+ImageZoom._imgDimensions.w+'x'+ImageZoom._imgDimensions.h);
		// console.log('maxLeft: '+ImageZoom._maxLeft+' | maxTop: '+ImageZoom._maxTop);
	},
	
	positionZoomedImage: function(e, elem){
		var xPercentage = (e.pageX-ImageZoom._gallery[0].offsetLeft)/ImageZoom._wrapDimensions.w,
			yPercentage = (e.pageY-ImageZoom._gallery[0].offsetTop)/ImageZoom._wrapDimensions.h,
			newTop,	newLeft;
		
		if( xPercentage > 1 )
		{
			xPercentage = 1;
		}
		
		if( yPercentage > 1 )
		{
			yPercentage = 1;
		}
		
		newTop      = Math.round(yPercentage*ImageZoom._maxTop),
		newLeft     = Math.round(xPercentage*ImageZoom._maxLeft);
		
		// console.log(xPercentage +'/'+yPercentage);
			
		elem.children('img').css({
			top: newTop+'px',
			left: newLeft+'px',
			bottom: 'auto'
		});
	},
	
	prevImage: function(){
		var _current = ImageZoom._items.filter('.current'),
			_index  = _current.index();
		
		_current.removeClass('current');
		if( !ImageZoom._gallery.hasClass('related_hidden') )
		{
			ImageZoom._relatedItems.eq(_index).fadeOut(500);
		}	
			
		if( _index === 0 )
		{
			
			_current.fadeOut(500, function(){
				// ImageZoom._gallery.removeClass('zoom_on');
				ImageZoom._items.last().fadeIn(500).addClass('current');
				ImageZoom.toggleZoomIcon();
				
				
				if( !ImageZoom._gallery.hasClass('related_hidden') )
				{
					ImageZoom._relatedItems.last().fadeIn(500);
				}
			});
		}
		else
		{
			_current.fadeOut(500, function(){
				// ImageZoom._gallery.removeClass('zoom_on');
				_current.prev().fadeIn(500).addClass('current');
				ImageZoom.toggleZoomIcon();
				
				if( !ImageZoom._gallery.hasClass('related_hidden') )
				{
					ImageZoom._relatedItems.eq((_index-1)).fadeIn(500);
				}
			});
			
		}
	},
	
	nextImage: function(){
		var _current = ImageZoom._items.filter('.current'),
			_index  = _current.index();
		
		_current.removeClass('current');
		if( !ImageZoom._gallery.hasClass('related_hidden') )
		{
			ImageZoom._relatedItems.eq(_index).fadeOut(500);
		}
		
			
		if( (_index+1) >= ImageZoom._count )
		{
			_current
			.fadeOut(500, function(){
				// ImageZoom._gallery.removeClass('zoom_on');
				ImageZoom._items.first().fadeIn(500).addClass('current');
				ImageZoom.toggleZoomIcon();
				
				if( !ImageZoom._gallery.hasClass('related_hidden') )
				{
					ImageZoom._relatedItems.first().fadeIn(500);
				}
			});
		}
		else
		{
			_current.fadeOut(500, function(){
				// ImageZoom._gallery.removeClass('zoom_on');
				_current.next().fadeIn(500).addClass('current');
				ImageZoom.toggleZoomIcon();
				
				if( !ImageZoom._gallery.hasClass('related_hidden') )
				{
					ImageZoom._relatedItems.eq((_index+1)).fadeIn(500);
				}
			});
			
		}
	},
	
	toggleZoomIcon: function(){
		var _item = ImageZoom._items.filter('.current');
	
		if( _item.find('.zoomed_img').children('img').length > 0 )
		{
			$('#image_gallery_tools').children().first().show();
		}
		else
		{
			ImageZoom.zoomOff();
			$('#image_gallery_tools').children().first().hide();
		}
		
		ImageZoom.toggleRelatedItems();
	},
	
	toggleRelatedItems: function(){
		var _item    = ImageZoom._items.filter('.current'),
			_index   = _item.index(),
			_related = ImageZoom._relatedItems.eq(_index);
			
		if( _related.children().length === 0 )
		{
			$('#image_gallery_tools').children('.related_toggle').addClass('visuallyhidden');
			ImageZoom._relatedItemWrap.addClass('hidden');
		}
		else
		{
			$('#image_gallery_tools').children('.related_toggle').removeClass('visuallyhidden');
			ImageZoom._relatedItemWrap.removeClass('hidden');
		}
	},
	
	createLightboxGallery: function(){
		$('body').append('<div id="lb_gallery" />');
		ImageZoom._lightbox = $('#lb_gallery');
		
		ImageZoom._items.each(function(){
			var _item  = $(this)
				_img   = _item.children('.standard_img').children('img').attr('src'),
				_title = _item.children('h1').html();
				
			_img   = $.trim(_img);
			_title = $.trim(_title);
				
			ImageZoom._lightbox.append('<div class="item" style=""><img src="'+_img+'"><div class="item_title">'+_title+'</div><div class="arrow prev">PREV</div><div class="arrow next">NEXT</div><div class="close_x"></div></div>');
		});
		
		ImageZoom._lightbox.delegate('.close_x','click', function(){
			$('body').removeClass('lightbox_active');
		});
		ImageZoom._lightbox.delegate('.arrow','click', function(){
			ImageZoom.lightboxGoTo($(this));
		});
		ImageZoom._lightbox
		.delegate('.item','mouseover', function(){
			$(this).find('.arrow').show();
		})
		.delegate('.item','mouseleave', function(){
			$(this).find('.arrow').hide();
		});
		
		setTimeout(ImageZoom.lightboxMarginSetter,500);
	},
	
	lightboxMarginSetter: function(){
		var somethingNotComplete = false;
		
		ImageZoom._lightboxFallback--;
		
		ImageZoom._lightbox.children(':not(.done)').each(function(){
			var _this = $(this),
				_img = _this.children('img');
			
			if( _img[0].complete )
			{
				_this.addClass('done');
				var _w = _img.width(),
					_h = _img.height();
					
				_this.css({
					marginTop: '-'+Math.round((_h+10)/2)+'px',
					marginLeft: '-'+Math.round((_w+10)/2)+'px'
				});
			}
			else
			{
				somethingNotComplete = true;
			}
		});
		
		if( somethingNotComplete && ImageZoom._lightboxFallback > 0 )
		{
			setTimeout(ImageZoom.lightboxMarginSetter,500);
		}
	},
	
	lightboxGoTo: function(_item){
		var _index = _item.parent().index();
			
		if( _item.hasClass('prev') )
		{
			// PREV ITEM
			if( _index > 0 )
			{
				_item.parent().fadeOut(function(){
					_item.parent().prev().fadeIn();
				});
			}
			else
			{
				_item.parent().fadeOut(function(){
					_item.parent().siblings().last().fadeIn();
				});
			}
		}
		else
		{
			// NEXT ITEM
			if( _item.parent().next().length > 0 )
			{
				_item.parent().fadeOut(function(){
					_item.parent().next().fadeIn();
				});
			}
			else
			{
				_item.parent().fadeOut(function(){
					_item.parent().siblings().first().fadeIn();
				});
			}
		}
	},
	
	zoomOn: function(){
		$('#image_gallery_tools').children('.zoom_in').toggleClass('zoom_in zoom_out');
		ImageZoom._gallery.addClass('zoom_on');
	},
	
	zoomOff: function(){
		$('#image_gallery_tools').children('.zoom_out').toggleClass('zoom_in zoom_out');
		ImageZoom._gallery.removeClass('zoom_on');
	}
};
// END IMAGEZOOM

//  LIGHTBOX GALLERY
var LBGallery = {
	
	init: function(){
		LBGallery._images = $('.product_gallery .big img');
		
		if( LBGallery._images.length > 0 )
		{
			LBGallery.createGallery();
			LBGallery.bindEvents();
		}
	},
	
	createGallery: function(){
		$('body').append('<div id="lb_gallery" />');
		LBGallery._gallery = $('#lb_gallery');
		
		LBGallery._images.each(function(){
			var _img = $(this),
				_w   = _img.width(),
				_h   = _img.height(),
				_src = _img.attr('src'),
				_topMargin  = (_h+10)/2,
				_leftMargin = (_w+10)/2;
				
			LBGallery._gallery.append('<div class="item" style="margin: -'+_topMargin+'px 0 0 -'+_leftMargin+'px"><img src="'+_src+'"><div class="arrow prev">PREV</div><div class="arrow next">NEXT</div><div class="close_x"></div></div>');
		});
	},
	
	bindEvents: function(){
		$('.product_gallery').delegate('.thumb','click', function(){
			var _thumb = $(this),
				_index = _thumb.parent().index();

			LBGallery.startGallery(_index);
		});
		
		LBGallery._gallery.delegate('.arrow','click',function(){
			LBGallery.goTo($(this));
		});
		
		LBGallery._gallery.delegate('.close_x','click', function(){
			LBGallery.hideGallery();
		});
		
		LBGallery._gallery
		.delegate('.item','mouseover', function(){
			$(this).find('.arrow').show();
		})
		.delegate('.item','mouseleave', function(){
			$(this).find('.arrow').hide();
		});
	},
	
	startGallery: function(_index){
		LBGallery._gallery.show().children().hide().eq(_index).show();
		$('body').addClass('gallery_active');
	},

	hideGallery: function(_index){
		LBGallery._gallery.hide('fast');
		$('body').removeClass('gallery_active');
	},
	
	goTo: function(_item){
		var _index = _item.parent().index();
			
		if( _item.hasClass('prev') )
		{
			// PREV ITEM
			if( _index > 0 )
			{
				_item.parent().fadeOut(function(){
					_item.parent().prev().fadeIn();
				});
			}
			else
			{
				_item.parent().fadeOut(function(){
					LBGallery._gallery.children().last().fadeIn();
				});
			}
		}
		else
		{
			// NEXT ITEM
			if( _item.parent().next().length > 0 )
			{
				_item.parent().fadeOut(function(){
					_item.parent().next().fadeIn();
				});
			}
			else
			{
				_item.parent().fadeOut(function(){
					LBGallery._gallery.children().first().fadeIn();
				});
			}
		}
	}
};

var FormHelper = {

	init: function(){
		FormHelper._form = $('.standard_input_form');
		
		if( FormHelper._form.length > 0 )
		{
			FormHelper.bindEvents();
		}
	},
	
	bindEvents: function(){
		FormHelper._form.submit(function(e){
			if( FormHelper.validate($(this).find('.required')) )
			{
				return true;
			}
			else
			{
				return false;
			}
		});
	},
	
	validate: function(fields){
		errorFound = false;
		
		FormHelper._form.find('.error').removeClass('error');
		
		fields.each(function(){
			var _field = $(this),
				_val   = _field.val(),
				_name  = _field.attr('name'),
				_type  = _field.attr('type');
				
			_val = $.trim(_val);
				
			if( _field.hasClass('email') && !_val.match(/^([a-zA-Z0-9_.-])+@([a-zA-Z0-9_.-])+\.([a-zA-Z])+([a-zA-Z])+/) )
			{
				errorFound = true;
				_field.parents('.form_item').find('label').first().addClass('error');
			}
			else if( _type === 'checkbox' )
			{
				if( $('input[name='+_name+']:checked').length === 0 )
				{
					_field.parents('.form_item').find('label').first().addClass('error');
				}
			}
			else if( _val.length === 0 )
			{
				errorFound = true;
				_field.parents('.form_item').find('label').first().addClass('error');
			}
		});
		
		if( errorFound )
		{
			return false;
		}
		else
		{
			return true;
		}
	}
};

var Promotions = {

	init: function(){
		if( $('body').hasClass('section_home_page') )
		{
			Promotions._div = $('.promotion_wrap .inner');
			Promotions._btn = $('.show_promotions');
			Promotions.initOffset = Promotions._div.offset();
			Promotions.maxLeft = $(window).width();			
			
			// Promotions._div.appendTo('.page').css({
			// 	position: 'absolute',
			// 	top: Promotions.initOffset.top+'px',
			// 	left: Promotions.initOffset.left+'px'
			// });
			
			Promotions.bindEvents();
			
			// setTimeout(Promotions.hidePromotions,5000);
		}
	},
	
	bindEvents: function(){
		Promotions._btn.click(function(){
			Promotions.showPromotions();
		});
		
		Promotions._div.delegate('.promobox','click',function(){
			var _this = $(this),
				_link = _this.children('.promobox_link').children('a');
				
			window.location = _link.attr('href');
		});
	},
	
	hidePromotions: function(){
		Promotions._div.animate({ left: '2000px', opacity: 0 }, 750, 'easeInCirc', function(){
			Promotions._btn.fadeIn(500);
		});
	},
	
	showPromotions: function(){
		Promotions._btn.fadeOut(200, function(){
			Promotions._div.animate({ left: '0px', opacity: 1 }, 500, 'easeOutCirc');
		});
		setTimeout(Promotions.hidePromotions,15000);
	}
};


// INTROMENU
var IntroMenu = {

	opening: false,
	currentDropdown: -1,
	backgroundNumber: 0,
	backgroundSize: '',
	promosClicked: false,
	
	init: function(){
		if( $('body').hasClass('landing_page') )
		{
			// intromenu_wrap
			IntroMenu._wrap      = $('#intromenu_wrap');
			IntroMenu._dropdowns = IntroMenu._wrap.find('.dropdown');
			
			IntroMenu.bindEvents();
			
			IntroMenu.addRandomBg();
			
			IntroMenu.initPromoWrap();
			
			setTimeout(IntroMenu.fadeOverlayToMenu,3000);
		}
	},
	
	bindEvents: function(){
		
		$('.intromenu_item').hoverIntent(function(){
			var _this  = $(this),
				_index = _this.index();
			
			_this.children('a').addClass('current').removeClass('decurrent');
			_this.siblings('.intromenu_item').children('a').addClass('decurrent').removeClass('current');
			
			if( !IntroMenu.opening )
			{
				IntroMenu.opening = true;
				
				IntroMenu._wrap.animate({height: '290px'},500, function(){
					IntroMenu.showDropdown((_index-1));
				});
			}
			else
			{
				IntroMenu.showDropdown((_index-1));
			}
		},function(){});
		
		IntroMenu._wrap.mouseleave(function(){
			IntroMenu.opening = false;
			IntroMenu._wrap.animate({height: '115px'},300);
			if( IntroMenu.currentDropdown >=0 )
			{
				IntroMenu._dropdowns.eq(IntroMenu.currentDropdown).fadeOut(150);
				IntroMenu.currentDropdown = -1;
			}
			
			$('.intromenu_item').children('a').removeClass('current decurrent');
		});
		$('.dropdown').click(function(e){
			e.preventDefault();
			var _this = $(this),
				_href = _this.siblings('a').attr('href');
			if( _href && _href.length > 0 )
			{
				window.location = _href;
			}
			
		});
		
		$(window).resize(function(){
			IntroMenu.checkBgImageSize();
		});
		
		$('.hide_promos_button').click(function(){
			IntroMenu.promosClicked = true;
			IntroMenu.hidePromos();
		});
		
		$('.show_promos_button').click(function(){
			IntroMenu.promosClicked = true;
			IntroMenu.showPromos();
		});
		
		$('.intro_promotions').delegate('.promobox','click',function(){
			var _this = $(this),
				_link = _this.children('.promobox_link').children('a');
				
			window.location = _link.attr('href');
		});
	},
	
	showDropdown: function(nr){
		// console.log('showing dropdown '+nr+'/ old: '+IntroMenu.currentDropdown);
		if( IntroMenu.currentDropdown === nr )
		{
			// same dropdown
		}
		else if( IntroMenu.currentDropdown >=0 )
		{
			IntroMenu._dropdowns.eq(IntroMenu.currentDropdown).fadeOut(100, function(){
				IntroMenu.currentDropdown = nr;
				IntroMenu._dropdowns.eq(nr).fadeIn(300);
			});
		}
		else
		{
			IntroMenu.currentDropdown = nr;
			IntroMenu._dropdowns.eq(nr).fadeIn(300);
		}
	},
	
	addRandomBg: function(){
		var _zufall      = 1,
			_size        = 'large',
			_windowWidth = $(window).width();
		
		if( $.cookie('bgNumber') )
		{
			_zufall = parseInt($.cookie('bgNumber'),10) + 1;
		}
		
		if( _zufall > 4 )
		{
			_zufall = 1;
		}
		
		$.cookie('bgNumber', _zufall);
		
		
		
		IntroMenu.backgroundNumber = _zufall;
		IntroMenu.backgroundSize = _size;
		
		if( _windowWidth < 1001 )
		{
			_size = 'small';
		}
		else if( _windowWidth < 1501 )
		{
			_size = 'medium';
		}
		
		
		$('.intro').css({
			backgroundImage: 'url(/templates/cdk_www/img/bgs/landing_page/img'+_zufall+'_'+_size+'.jpeg)'
		});
		
		// cache other sizes images
		$('body')
			.append('<img src="/templates/cdk_www/img/bgs/landing_page/img'+_zufall+'_large.jpeg" style="display:none;">')
			.append('<img src="/templates/cdk_www/img/bgs/landing_page/img'+_zufall+'_medium.jpeg" style="display:none;">')
			.append('<img src="/templates/cdk_www/img/bgs/landing_page/img'+_zufall+'_small.jpeg" style="display:none;">');
	},
	
	checkBgImageSize: function(){
		var _windowWidth = $(window).width(),
			_size        = 'large';
		
		if( _windowWidth < 1001 )
		{
			_size = 'small';
		}
		else if( _windowWidth < 1501 )
		{
			_size = 'medium';
		}
		
		if( _size !== IntroMenu.backgroundSize )
		{
			IntroMenu.backgroundSize = _size;
			
			$('.intro').css({
				backgroundImage: 'url(/templates/cdk_www/img/bgs/landing_page/img'+IntroMenu.backgroundNumber+'_'+_size+'.jpeg)'
			});
		}
	},
	
	fadeOverlayToMenu: function(){
		$('.intro .overlay').fadeOut(500, function(){
			$('.intromenu').fadeIn(500);
			IntroMenu.showPromos();
			setTimeout(IntroMenu.autoHidePromos,10000);
		});
	},
	
	autoHidePromos: function(){
		if( !IntroMenu.promosClicked ) {
			IntroMenu.hidePromos();
		}
	},
	
	hidePromos: function(){
		var promoWrap = $('.intro_promotions');
		
		promoWrap.children('.inner').animate({'left':'150%', 'opacity':0 },500,'easeInCirc',function(){
			promoWrap.animate({ 'opacity':0, 'height':'0px', 'top':'487px' },300, function(){
				$('.show_promos_button').fadeIn(150);
			});
		});
	},
	
	initPromoWrap: function(){
		var promoWrap = $('.intro_promotions');
		
		promoWrap.css({
			opacity: 0,
			height: '0px',
			top: '487px'
		})
		.children('.inner')
			.css({
				left: '150%',
				opacity: 0
			});
			
		promoWrap.show();
		$('.show_promos_button').fadeIn(100);
	},
	
	showPromos: function(){
		var promoWrap = $('.intro_promotions'),
			showButton = $('.show_promos_button');
		
		showButton.fadeOut(150, function(){
			promoWrap.animate({ 'opacity':1, 'height':'145px', 'top':'415px' }, 300, function(){
				promoWrap.children('.inner').animate({ 'left':'50%', 'opacity':1 },500,'easeOutCirc');
			});
		});
	}
};

// end : INTROMENU

$(function(){
	
	Page.init();
	ImageZoom.init();
	LBGallery.init();
	FormHelper.init();
	Promotions.init();
	IntroMenu.init();
		
});

