(function($) {

	$(function() {
		
		var isIE =  $('html').hasClass('ie');
		var isIE6 =  $('html').hasClass('ie6');
		
		
		//Headlinea
		(function(){
			var lis = $('#headlines .headlines-menu li')
			var headlines = $('#headlines .headlines-wrapper li');
			var backgrounds = $('#headlines .backgrounds li');
			var animating = false;
			var current = lis.filter(':first');
			window.swapInterval = false;
			
			if( isIE ) { 
				
				var change = function(li) {
					if( !animating && ( !current || li.data('id') != current.data('id') )  ) {
						
						$(backgrounds.get( current.data('id') )).css({'z-index': 1});
						$(backgrounds.get( li.data('id') )).css({'z-index': 2, 'opacity': 0}).animate({'opacity': 1}, 'slow');
						
						
						$(headlines.get( current.data('id') )).hide();
						$(headlines.get( li.data('id')  )).show();
						
						current.css('filter', '').show();
						current.data('clone').css('filter', '').hide();
						
						
						li.hide();
						li.data('clone').show();
						
						animating = false;
						current = li;
						
					}
				}
				
			} else { 
			
			
				var change = function(li) {
					if( !animating && ( !current || li.data('id') != current.data('id') )  ) {
						animating = true;
						
						current.animate({'opacity': 1}, 'slow');
						current.data('clone').animate({'opacity': 0}, 'slow');
						
						
						if( isIE ) { 
							var h = $(headlines.get( current.data('id') ));
							h.animate({'opacity': 0}, 'slow');
							h.find('p').animate({'opacity': 0}, 'slow');
							h.find('h2').animate({'opacity': 0}, 'slow');
						} else {
							
							$(headlines.get( current.data('id') )).animate({'opacity': 0}, 'slow', function(){
								$(this).css('display', 'none');
							});
						}
						
						var oldBackground = $(backgrounds.get( current.data('id') )).css({'z-index': 1});
						
						li.animate({'opacity': 0}, 'slow');
						li.data('clone').animate({'opacity': 1}, 'slow', function(){
							animating = false;
							current = li;
						});
						
						if( isIE ) { 
							var h = $(headlines.get( li.data('id') ));
							h.animate({'opacity': 1}, 'slow');
							h.find('p').animate({'opacity': 1}, 'slow');
							h.find('h2').animate({'opacity': 1}, 'slow');
						} else {
							$(headlines.get( li.data('id') )).css('display', 'block').animate({'opacity': 1}, 'slow');
						}
						$(backgrounds.get( li.data('id') )).css({'z-index': 2, 'opacity': 0}).animate({'opacity': 1}, 'slow', function() {
							oldBackground.css({'opacity': 0});
						});
						
					}
				}
			
			}
			
			$('a.false').live('click', function(){return false;})
			
			var currentID = 0;
			backgrounds.each(function(id) {
				if( $(this).hasClass('current') ) {
					currentID = id;
				}
			});
			var current = $(lis.get(currentID));
			
			headlines.each(function(id) {
				
				var li = $(this);
				li.css({
					'position': 'absolute',
					'left': 0,
					'top': 0,
					'width': '100%',
					'opacity': id == currentID ? 1 : 0,
					'cursor': 'pointer',
					'display': isIE || id == currentID ? 'block' : 'none'
				}).data('link', li.find('a').attr('href') );
				
				li.click(function() {
					var url = $(this).data('link');
					//console.log(this, $(this).find('a'));
					if(url)
						window.location = url;
					return false;
				});
				
				
				if( isIE ) {
					li.css('filter', '');
					if( id != currentID ) {
						li.hide();
					}
					//li.find('p').css('opacity', id == currentID ? 1 : 0);
					//li.find('h2').css('opacity', id == currentID ? 1 : 0);
				}
				
			});
			
			
			backgrounds.each(function(id) {
				
				var li = $(this);
				li.css({
					'position': 'absolute',
					'left': 0,
					'top': 0,
					'width': '100%',
					'opacity': id == currentID ? 1 : 0
				});
				
			});
			
			
			lis.each(function(id) {
				var li = $(this)
				var clone = li.clone(true).addClass('current').appendTo(li.parent());
				var isCurrent = id == currentID;
				
				if( li.hasClass('no-link') ) {
					li.find('a').css('cursor', 'default');
					clone.find('a').css('cursor', 'default');
				}
				
				if( id == 0 ) {
					clone.addClass('current-first');
					li.addClass('first');
				} else if( id == lis.length - 1 ) {
					clone.addClass('current-last');
					li.addClass('last');
				}
				
				li
					.data('clone', clone)
					.removeClass('current').removeClass('current-first').removeClass('current-last')
					.data('id', id)
				;
				var liHeight = 46;
				
				var props = {
					'position': 'absolute',
					'left': 0,
					'top': id * liHeight,
					'width': '100%',
					'z-index': 1
				};
				
				if(isCurrent) {
					if( isIE ) {
						li.hide();
					} else {
						props.opacity = 0;
					}
				}
				
				li.css(props);
				
				
				props = {
					'position': 'absolute',
					'left': 0,
					'top': id * liHeight,
					'width': '100%',
					'z-index': 2
				}
				
				if( isIE ) {
					if( isCurrent ) {
						clone.show();
					} else {
						clone.hide();
					}
				} else {
					if( isCurrent ) {
						props.opacity = 1;
					} else {
						props.opacity = 0;
					}
				}
				
				clone.css(props).data('li', li).click(function(){
					$(this).data('li').click();
					return false;
				});
				
				
				//Good damn IE fix - do not touch
				if(  document.all && !document.opera ) {
					
					
					li.html( '<span>'+ li.html() +'</span>' );
					clone.html( '<span>'+ clone.html() +'</span>' );
					var lia = li.find('a');
					var clonea = clone.find('a');
					
					
					var bgUrl = lia.css('background-image');
					bgUrl = bgUrl.substr( 5, bgUrl.length - 7 );
					
					var cloneBgUrl = clonea.css('background-image');
					cloneBgUrl = cloneBgUrl.substr( 5, cloneBgUrl.length - 7 );
					
					lia.css('background', 'none');
					clonea.css('background', 'none');
					
					li.find('span').css('filter','progid:DXImageTransform.Microsoft.AlphaImageLoader(src="'+ bgUrl +'", sizingMethod="scale")');
					clone.find('span').css('filter', 'progid:DXImageTransform.Microsoft.AlphaImageLoader(src="'+ cloneBgUrl +'", sizingMethod="scale")');
					
					
				}
				
				
			});
			
			
			lis.click(function() {
				
				if( !animating && !$(this).hasClass('no-link') ) {
					clearInterval(swapInterval);
					change($(this));
				}
				
				return false;
			});
			
			
			swapInterval = setInterval(function() { 
				
				if(current.get(0) == lis.last().get(0)) {
					change( lis.first() );
				} else {
					change( current.next() );
				}
				
			}, 7500);
			
		})();
		
		
		//Events
		$('#events li').each(function(){
			
			var $this = $(this);
			var wrapper = $this.find('.image-wrapper').css({'height': 117});
			var cover = wrapper.find('.cover').css({'display': 'block', 'opacity': 0});
			var p = $this.find('p').css({'display': 'block', 'opacity': 0});
			$this.data('wrapper', wrapper);
			$this.data('cover', cover);
			
			$this.hover(
				function() {
					
					wrapper.stop().animate({'height': 50}, 'fast');
					cover.stop().animate({'opacity': 0.2}, 'fast');
					p.stop().animate({'opacity': 1}, 'fast');
					
				}, function() {
					
					wrapper.stop().animate({'height': 117}, 'fast');
					cover.stop().animate({'opacity': 0}, 'fast');
					p.stop().animate({'opacity': 0}, 'fast');
					
				}
			).css('cursor', 'pointer').click(function() {
				window.location = $(this).find('.view-more').attr('href');
				return false;
			});
			
		});
		
		
		//Latest News
		(function(){
			
			var wrapper = $('#latest-news');
			var ul = wrapper.find('ul');
			var lis = ul.find('li');
			var pause = wrapper.find('a.pause');
			var next = wrapper.find('a.next');
			var prev = wrapper.find('a.prev');
			var animate = false;
			var current = 0;
			var playing = true;
			var swapInterval;
			var scrollInterval;
			var scrollLeft = 0;
			var maxScrollLeft = 0;
			var scrollTimeout;
			
			lis.css({
				'position': 'absolute',
				'left': 0,
				'top': 0,
				'display': 'none'
			});
			
			var nextFct = function() {
				next.click();
			};
			
			
			ul.hover(function() {
				//stopScroll();
			}, function() {
				//restartScroll();
			});
			
			
			var restartScroll = function() {
				var ul = lis.parent().get(0);
				scrollInterval = setInterval(function() {
					scrollLeft += 0.5;
					if( scrollLeft > maxScrollLeft ) {
						
						$(lis.get(current)).animate({'opacity': 0}, 'slow', function() {
							ul.scrollLeft = 0;
							$(lis.get(current)).animate({'opacity': 1}, 'slow');
							startScroll();
						});
						
						stopScroll();
					} else {
						ul.scrollLeft = scrollLeft;
					}
				}, 10);
			};
			
			
			var startScroll = function() {
				return false;
				scrollLeft = 0;
				maxScrollLeft = $(lis.get(0)).width() - lis.parent().width();
				lis.parent().get(0).scrollLeft = scrollLeft;
				
				restartScroll();
				
			};
			
			var stopScroll = function() {
				
				clearInterval(scrollInterval);
				clearTimeout(scrollTimeout);
				
			};
			
			
			var show = function(current, old) {
				
				stopScroll();
				
				$( lis.get(old) ).animate({'opacity': 0}, 'fast', function(){
					$(this).css('display', 'none');
					lis.parent().get(0).scrollLeft = 0;
					$( lis.get(current) ).css({'top': 0, 'display': 'block', 'opacity': 0}).animate({'opacity': 1}, 'slow', function() {
						scrollTimeout = setTimeout(function() {
							startScroll();
						}, 1000);
						
						animate = false;
					});
				});
				
			}
			
			
			next.click(function() {
				
				if( !animate ) {
					animate = true;
					var old = current;
					if( ++current >= lis.length - 1 ) {
						current = 0;
					}
					show(current, old);
				}
				
				return false;
				
			});
			
			
			prev.click(function() {
				
				if( !animate ) {
					animate = true;
					var old = current;
					if( --current < 0 ) {
						current = lis.length - 1;
					}
					show(current, old);
				}
				
				return false;
				
			});
			
			pause.click(function() {
				
				if( playing ) {
					playing = false;
					pause.addClass('play');
					clearInterval(swapInterval);
				} else {
					playing = true;
					pause.removeClass('play');
					swapInterval = setInterval(nextFct, 10000);
				}
				
				return false;
				
			});
			
			
			swapInterval = setInterval(nextFct, 10000);
			$( lis.get(current) ).css('display', 'block');
			scrollTimeout = setTimeout(function() {
				startScroll();
			}, 1000);
			
			
			
		})();
		
		
		//Input default text
		window.check_inputs = function() {
			$('.default-text').each(function(){
				if(!this._default_text){
					this._default_text = $(this).val();
					if(this.type == 'password'){
						this._text_input = document.createElement('input');
						
						this._text_input.type = 'text';
						this._text_input.className = this.className;
						this._text_input._text_password = this;
						$(this._text_input).val($(this).val());
						$(this).val('');
						
						$(this).removeClass('has-default-text');
						
						$(this._text_input).css({
							'position': 'absolute',
							'top': $(this).position().top + 'px',
							'left': $(this).position().left + 'px',
							'margin-top': '0',
							'display': 'block'
							});
						
						this.parentNode.appendChild(this._text_input);
						
						$(this.parentNode).css({'height': $(this.parentNode).outerHeight() + 'px'});
						
						$(this._text_input).focus(function() {
							$(this).css({'display': 'none'});
							$(this._text_password).focus();
						});
						
						$(this).focus(function(){
							if( $(this._text_input).css('display') == 'block' ) {
								$(this._text_input).focus();
							}
						});
						$(this).change(function(){
							if( $(this._text_input).css('display') == 'block' ) {
								$(this._text_input).focus();
							}
						});
						
						
						$(this).blur(function(){
							if( $(this).val() == '')
								$(this._text_input).css({'display': 'block'});
						});
						
						}
					else {
						$(this).addClass('has-default-text');
						$(this).focus(function() {
							if( $(this).val() == this._default_text) {
								this.value = '';
								$(this).removeClass('has-default-text');
							}
							return true;
						});
						$(this).blur(function() {
							if( $(this).val() == '') {
								$(this).addClass('has-default-text');
								$(this).val(this._default_text);
							}
							return true;
						});
						}
					}
			});
		};
		window.check_inputs();
		
		
		//Main menu
		(function(){
			
			var show = function() {
				var $this = $(this);
				
				clearTimeout($this.data('timeout'));
				$this.data('timeout', false);
				$this.data('ul').stop().css('display', 'block').animate({ 'opacity': 1}, 'fast');
				$this.addClass('opened');
				
				
			}
			
			var hide = function() {
				var $this = $(this);
				clearTimeout($this.data('timeout'));
				
				$this.data('timeout', setTimeout(function() {
					$this.data('ul').stop().animate({ 'opacity': 0}, 'fast', function() { $(this).css('display', 'none'); });
					$this.removeClass('opened');
				}, 100));
				
			}
			
			
			var mainMenu = $('#main-menu');
			mainMenu.find('li').each(function(){
				var $this = $(this);
				
				if( $this.hasClass('has-submenu') ) {
					var ul = $this.find('ul:first').css({ 'opacity': 0, 'display': 'none'});
					var left = - ul.width() / 2 + $this.width() / 2;
					
					if( $this.position().left - ul.width() / 2 + $this.width() / 2 + ul.width() > mainMenu.width()) {
						left -= $this.position().left - ul.width() / 2 + $this.width() / 2 + ul.width() - mainMenu.width();
					}
					
					$this
						.data('timeout', false)
						.data('ul', ul.css({'left': parseInt( left )}))
						.hover(show, hide)
						.find('a:first').click(function(){ return false; }).css({'cursor': 'default'})
					
					;
				}
				
				
			});
			
		})();
		
		
		//
		$('.community-box img').each(function() {
			
			$this = $(this);
			
			var bw = this.src;
			
			var c = $('<img class="colored">').css('opacity', 0).attr('src', this.alt).insertAfter($this);
			
			
			c.parent().hover(
				function() { c.stop().animate({'opacity': 1}, 'fast'); },
				function() { c.stop().animate({'opacity': 0}, 'fast'); }
			);
			
		});
		
	
	
		
		Cufon.replace('.cufon', {'hover': true});
		Cufon.replace('.cufon-light', {'hover': true, 'fontFamily': 'vga-light'});
		
		
		$('.lightbox-ajax').fancybox({
			'width': 570,
			'height': 470,
			'autoScale': false,
			'autoDimensions': false,
			'onComplete': function(){
				Cufon.refresh();
			},
			'scrolling': 'no'
		});
		
		
		$('.contact-lightbox').fancybox({
			'width': 570,
			'height': 470,
			'autoScale': false,
			'autoDimensions': false,
			'href': '/corporate/contact',
			'onComplete': function(){
				Cufon.refresh();
			},
			'scrolling': 'no'
		});
		
		
		
		$('.plus-link.video').each(function(){
			var size = this.rel.split('x');
			
			$(this).fancybox({
				'width': parseInt(size[0]),
				'height': parseInt(size[1]),
				'autoScale': false,
				'autoDimensions': false,
				'type': 'iframe',
				'onComplete': function(){
					Cufon.refresh();
				}
			});
		})
		
		
		var selectors = [
			'#headlines .headlines-wrapper', 
			//'.sectors li', 
			//'.sectors li a', 
			'.sectors .shadow', 
			'#events .image-wrapper .top-shadow',
			'#events .items .bottom-border',
			'#events .items a.view-more',
			'#sidebar .testimonials',
			'#sidebar .testimonials blockquote',
			'#sidebar .speaker .mask-shadow',
			'#sidebar .speaker .mask-top',
			'#sidebar .speaker .mask-bottom',
			'.community-box .top',
			'#background-wrapper'
		];
		
		if( isIE6 ) {
			
			
			$('.sectors li').each(function() {
				
				$this = $(this);
				$this.data('class', $this.attr('class') + '-hover' );
				
				var bgUrl = $this.css('background-image');
				bgUrl = bgUrl.substr( 5, bgUrl.length - 7 );
				
				$this
					.css('filter', 'progid:DXImageTransform.Microsoft.AlphaImageLoader(src="'+ bgUrl +'", sizingMethod="crop")')
					.css('background-image', 'none');
				
				$this.append($('<span>'));
				var a = $this.find('span');
				bgUrl = a.css('background-image');
				bgUrl = bgUrl.substr( 5, bgUrl.length - 7 );
				
				a	
					.css('filter', 'progid:DXImageTransform.Microsoft.AlphaImageLoader(src="'+ bgUrl +'", sizingMethod="crop")')
					.css('background-image', 'none');
				
				
				$this.hover(function() {
					$(this).addClass( $(this).data('class') );
				}, function() {
					
					$(this).removeClass( $(this).data('class') );
				
				
				});
				
			});
			
			
			
			var mask = $('#sidebar .speaker .mask-shadow');
			mask.height( mask.parent().height() );
			DD_belatedPNG.fix(selectors.join(','));
			
		}
		
		$('#events .items li:nth-child(5n)').addClass('last-in-row');
		
		if( $('#sector').length ) {
			$('#events .items li').removeClass('last-in-row');
			$('#sector #events .items li:nth-child(3n)').addClass('last-in-row');
		}
		
		
		
	});

})(jQuery);
