$(document).ready(function(){
	carouselFooter.init();
	videoInfo.init();
	iconTips.init();
	subIconTips.init();
	//newsTicker.init({auto: true, timer: 10000});
	ticker.init();
	livewire.init();
	carousels.init();
	$('.view-front-page').fancybox();
	proOnly.init();
	$('li.proOnly div').hide();
});
ticker = function(){
	var self = this;
	function init(){
		self.widget = $('#NewsTicker');
		self.itemsList = $('ul.headlines');
		self.pauseButton = $('.play-pause',self.widget);
		self.itemsList.cycle({ 
		fx: 'blindY',
      	startingSlide: 		0,  // start on the slide that was in the markup
		fastOnEvent: 			1,
      	timeout:  				1,
      	speed:    				3000,
		speedIn: 				500,
		speedOut: 				500,
      	prev:    					'#Controls .previous', 
      	next:    					'#Controls .next',
		pause: 						true,
		sync: 						1
    });
		events();
	}
	function events(){
		self.pauseButton.click(function(){
			if ( $(this).hasClass('paused') === false){
				$(this).addClass('paused');
				self.itemsList.cycle('pause');
			} else { 
				$(this).removeClass('paused');
				self.itemsList.cycle('resume');
			}
		});
	}
	return { init:init }
}();

carouselFooter = function(){
	var o = this;
	function init(){
		o.widget = $('.carouselFooter');
		o.items = $('li a', o.widget);
		o.itemParents = $('li',o.widget);
		inject();
		render();
		events();
	}
	function inject(){
		o.widget.append('<div id="carouselFooterOutput"></div>');
		o.output = $('#carouselFooterOutput',o.widget);
		o.output.css({
			width: '554px',
			height: '30px',
			padding: '9px',
			color: '#ffffff',
			display: 'none',
			position: 'absolute',
			zIndex: '40',
			top: '-50px',
			left: '0px',
			borderTop: '1px #474948 solid',
			borderBottom: '1px #2a2b2b solid',
			background: 'url(\'/graphics/new/ui-features/dark-opacity-bg.png\') 0 0 repeat'
		});
	}
	function render(){
		o.itemParents.eq(0).addClass('hover');
		//o.output.html('<a href="' + $('a:first', o.widget).attr('href') + '">' + $('a:first', o.widget).attr('title') + '</a>');
		o.output.html($('.tip-content',o.itemParents.eq(0)).html());
		o.output.show();
	}
	function events(){
		o.items.hover(
			function (){
				o.output.hide();
				o.itemParents.removeClass('hover');
				var $parent = $(this).parent();
				$parent.addClass('hover');
				o.output.html($('.tip-content',$parent).html());
				o.output.show();
			},
			function (){}
		);
	}
	return { init: init }
}();
videoInfo = function(){
	var o = this;
	function init(){
		o.widget = $('.featureBody');
		o.items = $('li', o.widget);
		events();
	}
	function events(){
		o.items.hover(
			function (){
				$('.timeStamp',this).hide();
				$('.videoInfo',this).show();
			},
			function (){
				$('.timeStamp',this).show();
				$('.videoInfo',this).hide();
			}
		);
	}
	return { init: init }
}();
proOnly = function(){
	var o = this;
	function init(){
		o.items = $('li.proOnly');
		events();
	}
	function events(){
		o.items.hover(
			function (){
				$(this.children).next().show();
			},
			function (){
				$(this.children).next().hide();
			}
		);
	}
	return { init: init }
}();
iconTips = function(){
	var o = {};
	function init(){
		o.items = $('.icon-tip');
		events();
		render();
	}
	function render(){
		$('.story').each(function(){
			$('.icon-tip',this).eq(0).addClass('icon-tip-hover');
		});
	}
	function events(){
		o.items.hover(
			function(){
				var offset = $(this).position();
				var $sectionItems = $('.icon-tip',$(this).parent());
				
				$sectionItems.removeClass('icon-tip-hover');
				$(this).addClass('icon-tip-hover');
				var $storyParent = $(this).parent().parent();
				$('.icon-tip-content',this).css({left: '-'+ offset.left + 'px'});
			},
			function(){
			}
		);
	}
	return { init:init }
}();
subIconTips = function(){
	var o = {};
	function init(){
		o.items = $('.subIcon-tip');
		o.items.append('<div class="arrow"></div>');
		events();
	}
	function events(){
		o.items.hover(
			function(){
				var offset = $(this).position();
				var $sectionItems = $('.subIcon-tip',$(this).parent());
				
				$sectionItems.removeClass('subIcon-tip-hover');
				$(this).addClass('subIcon-tip-hover');
				var $storyParent = $(this).parent().parent();
				$('.subIcon-tip-content',this).css({left: '-'+ offset.left + 'px'});
			},
			function(){
				o.items.removeClass('subIcon-tip-hover');
			}
		);
	}
	return { init:init }
}();
livewire = function(){
	var o = {};
	function init(){
		o.widget = $('#livewire_content');
		o.items = $('li',o.widget);
		events();
	}
	function events(){
		o.items.click(function(){
			window.location($('a.divlink',this).attr('href'));
		});
	}
	return { init: init }
}();
newsTicker = function(){
	var o = {};
	var animateRunning = 0;
	function init(options){
		o.config = options;
		o.widget = $('#NewsTicker');
		o.itemsList = $('ul.headlines li', o.widget);
		o.itemsListLinks = $('ul.headlines li a', o.widget);
		o.itemsTotal = (o.itemsList.size() - 1); //Array adjust
		o.current = 0;
		o.paused = false;
		o.nextButton = $('.next', o.widget);
		o.prevButton = $('.previous', o.widget);
		o.playPauseButton = $('.play-pause', o.widget);
		o.widget.append('<div id="UnravelBar"><img src="/graphics/new/ui-features/underscore.gif" alt=""/></div>');
		o.unravelBar = $('#UnravelBar',o.widget);
		o.unravelBarImg = $('img',o.unravelBar);
		o.offset = '40';
		render();
		if(options.auto === true) {
			var timer = function(){ if(options.timer) { return options.timer; } else { return false; } }();
			startScroll(timer);
		}
		events();
	}
	function render(){
		o.unravelBar.css({background: '#1a1a1a', position: 'absolute', zIndex: '1000', top: '0', left: o.offset +'px', height: '24px', width: '1000px', display: "block"});
		o.unravelBarImg.css({position: 'absolute', bottom: '5px', left: '2px', zIndex: '1001'});
		o.itemsList.css({width: '700px'});
	}
	function startScroll(length){
		if (o.interval) {
			window.clearInterval(o.interval);
			o.interval = window.setInterval(function(){
			if (animateCheck()) { next({effects: true}); } },length);
		}
		else {
			if (animateCheck()) { next({effects: true}); } 
			o.interval = window.setInterval(function(){ if (animateCheck()) { next({effects: true}); } },length);
		}
	}
	function pause(){
		//Stop auto scroll once UI clicked
		if (o.interval) {
			window.clearInterval(o.interval);
			o.paused = true;
			o.playPauseButton.addClass('play-pause-play');
		}
	}
	function events(){
		o.nextButton.click(function(){ pause(); clearAnimations(); next({effects: false}); });
		o.prevButton.click(function(){ pause(); clearAnimations(); prev({effects: false}); });
		o.playPauseButton.click(function(){ pauseCheck(); });
		o.itemsListLinks.hover(
			function(){
				pause();
				clearAnimations();
			},
			function(){
				pauseCheck();
			}
		);
	}
	function pauseCheck(){
		if (o.paused === false) {
			//pause
			o.playPauseButton.addClass('play-pause-play');
			pause();
			o.paused = true;
		} else {
			var timer = function(){
				if(o.config.timer) { return o.config.timer; } else { return false; }
			}();
			startScroll(timer);
			o.playPauseButton.removeClass('play-pause-play');
			o.paused = false;
		}
	}
	function animateCheck(f){
		//Do not run if any animations are currently running
		if (animateRunning > 0) {
			return true;
		} else {
			return true;
		}
	};
	function clearAnimations(){
		o.itemsList.stop(true,false);
		o.unravelBar.stop(true,true);
	}
	function runAnimation(settings){
		o.animateRunning = 2;
		resetUnravelBar();
		o.previous = o.current;
		if (settings.next === true){
			//Going forwards/next
				if (o.current >= o.itemsTotal){
					//Loop back to beginning if at last image
					o.targetItem = 0;
					o.current = 0;
				} else {
					o.targetItem = o.current + 1;
					o.current++;
				}
		} else {
			//Going backwards/previous
			if (o.current == 0){
				//Loop from the end if at the start and going backwards
				o.current = o.itemsTotal;
				o.targetItem = o.current;
			} else {
				o.current--;
				o.targetItem = o.current;
			}
		}
		
		if (settings.effects === true){
			//Hide current headline to make way for the next
			o.itemsList.eq(o.previous).animate({opacity: 'hide'},500,function(){
				 	o.animateRunning--;
					//Set headline object
					o.headline = {};
					o.headline._item = o.itemsList.eq(o.targetItem);
					o.headline._time = $('.headline-time',o.headline._item);
					o.headline._text = $('a',o.headline._item);

					//Hide headline items to fade in later, time, text etc
					o.headline._time.hide();
					o.headline._text.hide();
					o.headline._item.show();
					//Fade the time in then render the headline out
					o.headline._time.show();
					window.setTimeout(function(){ runHeadline();},2000);
					function runHeadline(){
						o.headline._text.show();
						o.textLength = parseInt($('a',o.itemsList.eq(o.targetItem)).width());
						o.unravelBar.show();
						o.unravelBar.animate({left: (o.textLength + parseInt(o.offset)) + 'px'},(o.textLength * 10),function(){
							o.animateRunning--;
							o.unravelBar.animate({opacity: 'hide'},1000);
						});
					}
			});
		} else {
			console.log('tester');
			o.itemsList.eq(o.previous).hide();
			o.itemsList.eq(o.current).show();
			o.unravelBar.hide();
		}
		
	}
	function resetUnravelBar(){
		o.unravelBar.hide();
		o.unravelBar.css({left: o.offset + 'px'});
		o.unravelBar.stop(false,false);
	}
	function next(options){
		if(o.current < o.imagesTotal) {
			runAnimation({loop: false, next: true, effects: options.effects});
		} else {
			runAnimation({loop: true, next: true, effects: options.effects});
		}
	}
	function prev(options){
		if(o.current > 0) {
				runAnimation({loop: false, next: false, effects: options.effects});
			} else {
				runAnimation({loop: true, next: false, effects: options.effects});
		}
	}
	return { init:init, animateRunning: animateRunning }
}();
carousels = function(){
	var o = {};
	function init(){
		o.current = 
		$('.scrollable').serialScroll({
				items:'div.scrollable-item',
				prev:'.prevPage',
				next:'.nextPage',
				offset:0, //when scrolling to photo, stop 230 before reaching it (from the left)
				start:0, //as we are centering it, start at the 2nd
				duration:300,
				force:true,
				stop:true,
				lock:false,
				cycle:false, //don't pull back once you reach the end
				jump: false, //click on the images to scroll to them
				step: 6,
				onBefore: function(){
					o.items.removeClass('scrollable-item-hover');
					$('img',o.items).css({border: '1px #ffffff solid'});
					o.output.html('');
				},
				onAfter: function(e,ui){
					var $first = $(e);
					$first.addClass('scrollable-item-hover');
					$('img',$first).css({border: '1px #282828 solid'});
					o.output.html($('.hover-content',$first).html());
				}
			});
		o.items = $('.videoContent .scrollable-item');
		o.output = $('.output');
		events();
	}
	function events(){
		o.items.hover(
			function() {
				//clear other items
				o.items.removeClass('scrollable-item-hover');
				$('img',o.items).css({border: '1px #121212 solid'});
				o.output.html('');
				
				$(this).addClass('scrollable-item-hover');
				$('img',this).css({border: '1px #ffffff solid'});
				o.output.html($('.hover-content',this).html());
				
			},
			function() {
				//do nothing 
			}
		)
	}
	return { init: init }
}();


