(function($) {

	$(function() {
		
		$('li.none > a').css('cursor', 'default').click(function(){return false;})
		
		var isIE =  $('html').hasClass('ie');
		var isIE6 =  $('html').hasClass('ie6');
		
		
		$('ul.speakers').each(function() {
			
			var ul = $(this);
			var li = ul.find('li');
			
			ul.find('li:nth-child(4n)').addClass('last-in-row');
			
			if( li.length == 3 ) {
				ul.addClass('spaced-out');
			}
			
		});		
		
		//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);
			
			
			
		})();
		
		
		//Main menu
		$('#main-menu li').hover(function() {
			
			$(this).addClass('opened').children('ul').show();
		
		}, function() {
			
			$(this).removeClass('opened').children('ul').hide();
		
		});
		
		//agenda
		/*
		(function(){
			var wrapper = $('#agenda-content-wrapper');
			if( wrapper.length ) {
				var imgs = wrapper.find('img');
				if( imgs.length ) {
					
					imgs.each(function(){
						
						var w = this.width + 20
						$(this).css('margin-left', -w);
						$(this).parents('div:first').css('padding-left', w);
						
					});
					
					//wrapper.children
					
				}
			}
		})();
		
		*/
		
		//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();
		
		
		/*
		$('#event-banner').each(function(){
			var wrapper = $(this);
			var p = wrapper.find('p:first');
			
			var links = wrapper.find('.links');
			var bottom = wrapper.height() - p.height() - parseInt(p.css('padding-top')) - links.height() - 20;
			
			if( bottom > 40 ) {
				bottom = 40;
			}
			
			links.css('bottom', bottom);
			
		});
		*/
		
		$('#sponsors').each(function(){
			var wrapper = $(this);
			var lis = wrapper.find('li');
			for(var i = 0, c = lis.length; i < c; i += 2) {
				var l1 = lis.get(i);
				var l2 = lis.get(i + 1);
				if( l1 && l2 ) {
					l1 = $(l1);
					l2 = $(l2);
					var height = Math.max(l1.height(), l2.height());
					l1.height(height);
					l2.height(height);
				}
			}
		});
		
		window.addFontClass = function(font) {
			
			var selectors = [
				'#event-banner p strong',
				'#event-banner p em',
				'.article h2',
				'.page-title',
				'.buttons a',
				'.article table caption',
				'.time-remaining .widget-content',
				'.discounts .widget-content'
			];
			$(selectors.join(',')).addClass(font);
			
			
		}
		
		if( !$('#newsletter-signup').length ) {
			$('.user-controls').addClass('no-newsletter');
		}
		
		if( isIE6 ) {
			var selectors = [
				'#main-menu li',
				'#event-banner .links a span em',
				'.time-remaining .widget-content',
				'#footer .footer-note',
				'#newsletter-signup .input-submit ',
				'#latest-news .controllers a'
			];
			
			$('#event-banner .links a span').each(function(){ 
				$(this).append( $('<em>').css('background-image', $(this).css('background-image')) ).css('background-image', 'none');
			});
			
			DD_belatedPNG.fix(selectors.join(','));
		}
		
		$("#sidebar-wrapper .widget:first").addClass("first-widget");
		
		/*
		$('#sidebar-wrapper .widget').each(function(){
			
			var $this = $(this);
			var next = $this.next();
			
			if( (next.hasClass('widget') || next.hasClass('wiget-link')) && parseInt(next.css('border-top-width')) ) {
				$this.css('border-bottom', 'none');
			}
			
		});
		
		
		$('.widget').each(function(){
			
			var prev = $(this).prev();
			var $this = $(this);
			
			if( prev.length && parseInt(prev.css('margin-bottom'), 10) == 0 && parseInt(prev.css('border-bottom-width'), 10) != 0  ) {
				prev.css('border-bottom-width', 0);
			}
			
		});
		*/
		$('.widget h3').each(function() {
			var $this = $(this);
			if( $this.html() == '' ) {
				$this.remove();
			}
		});
		
		$('.wiget-link').each(function() {
			var prev = $(this).prev();
			var $this = $(this);
			
			if( prev.length && parseInt(prev.css('margin-bottom'), 10) == 0 && parseInt(prev.css('border-bottom-width'), 10) != 0 ) {	
				prev.css('border-bottom-width', 0);
			}
		});
		
		
		var mainMenu = $('#main-menu');
		var mainMenuItems = $('#main-menu').children('li').children('a');
		var width = 0;
		mainMenuItems.each(function() {
			width += $(this).width();
		});
		
		var marginWidth = parseInt( (mainMenu.width() - width) / mainMenuItems.length, 10 );
		$('#main-menu a').each(function() {
			$(this).css({
				'padding-right': marginWidth / 2 - 2,
				'padding-left': marginWidth / 2 - 2
			});
		});
			
		//$('.article .speakers li:nth-child(4)').addClass('last-in-row');
		
	});

})(jQuery);
