window.addEvent('domready', function() {
	if ($('carousel')) setHomeCarousel();
	if ($('photography')) setStoryPhotos();
});

function setHomeCarousel() {
	if ($('carousel')) {
		var boxes = $('carousel').getElements('li');
		if (boxes.length > 3) {
			sliderWidth = ((boxes[0].getStyle('width').toInt()) * boxes.length);
			sliderLimit = ((boxes[0].getStyle('width').toInt()) * boxes.length) - $('carousel').getStyle('width').toInt();
			$('carousel').getElement('ul').setStyle('width',sliderWidth);
			
			var arrowLeft = $('highlights-container').getElement('a#left');
			var arrowRight = $('highlights-container').getElement('a#right');		
			
			arrowLeft.addEvent('click', function() {
				slideCarousel('left');
			});												 

			arrowRight.addEvent('click', function() {
				slideCarousel('right');
			});												 

		}
	}
}

function slideCarousel(direction) {
	var el = $('carousel');
	
	if(direction == 'left') {
		var distance = -285;
	} else {
		var distance = 285;		
	}
	
	var myFx = new Fx.Scroll(el, {
		wait: true,
		duration: 200,
		transition: Fx.Transitions.Quad.easeInOut,								 
		offset: {
			'x': distance,
			'y': 0
		}
	}).toTop();
}

function setStoryPhotos() {
	var storyGalleryPeriodical;
	if ($('photography')) {
		var images = $('photography').getElements('.photo');
		if (images.length > 1) 	{
			images.each(function(item, index, array){
				item.getElement('.number').setStyle('visibility', 'visible');
				var left = item.getElement('.left-arrow');
				var right = item.getElement('.right-arrow');
				left.setStyle('visibility', 'visible');
				right.setStyle('visibility', 'visible');
				left.addEvent('click', function(event) {$clear(storyGalleryPeriodical);changeStoryGalleryImage('previous');});
				right.addEvent('click', function(event) {$clear(storyGalleryPeriodical);changeStoryGalleryImage('next');});
			});
			storyGalleryPeriodical = changeStoryGalleryImage.periodical(10000, null); 
		}
	}
}

function changeStoryGalleryImage(x) {
	var images = $('photography').getElements('.photo');
	var current = 1;

	images.each(function(item, index) {
		if (item.getStyle('display') == 'block') {
			item.setStyle('display','none');
			current = index+1;
		}
	});

	var next = 1;
	if (x == 'previous') next = (current > 1) ? current-1 : images.length;
	else next = (current < images.length) ? current + 1 : 1;
	$('photo'+next).setStyle('display', 'block');
}

function showPlayer(url) {
	var win = window.open(url, "rteplayer", "height=560,width=860,status=yes,toolbar=no,menubar=no,location=no");
	win.focus();
} 

function setTopNavEvents() {
	var nav = $('topNav');
	nav.getElements('.topNav-hasMenu').each(function(item, index){
		item.addEvent('mouseenter', function() { 
			var buttonId = this.getProperty('id');
			item.addClass('mouseOn');
			displayTopNavDropdown(buttonId);
		});
		item.addEvent('mouseleave', function() { 
			var buttonId = this.getProperty('id');
			item.removeClass('mouseOn');
			clearTopNavDropdown.delay(50, this, buttonId);
		}); 
		$(item.getProperty('id')+"-menu").addEvent('mouseenter', function() { 
			item.addClass('mouseOn');
		});
		$(item.getProperty('id')+"-menu").addEvent('mouseleave', function() { 
			item.removeClass('mouseOn');
			var buttonId = this.getProperty('id').substring(0, this.getProperty('id').length-5);
			clearTopNavDropdown.delay(50, this, buttonId);
		});
	});
}

function displayTopNavDropdown(id) {
	if ($(id) && $(id+"-menu")) {
		var nav = $('topNav');
		var but = $(id);
		var menu = $(id+"-menu");
		nav.getElements('.open').each(function(item, index) { item.removeClass('open'); });
		but.addClass("open");
		menu.setStyle('left', but.getPosition().x - nav.getPosition().x + 12) ;
		menu.addClass("open");
		var isIE6 = /msie|MSIE 6/.test(navigator.userAgent);
		var dropdown = $('listingsMenu-daySelect');
		if (dropdown && isIE6  && (id == 'topNavTV' || id == 'topNavNews' ))   {
			dropdown.setStyle('visibility', 'hidden');
		}
	}
}

function clearTopNavDropdown(id){
	if ($(id) && $(id+"-menu")) {
		var but = $(id);
		var menu = $(id+"-menu");
		if (!but.hasClass('mouseOn') && !but.hasClass('mouseOn')) {
			but.removeClass("open");
			menu.removeClass("open");
			var isIE6 = /msie|MSIE 6/.test(navigator.userAgent);
			var dropdown = $('listingsMenu-daySelect');
			var testTV = $('topNavTV').hasClass('open');
			var testNews = $('topNavNews').hasClass('open');
			if (dropdown && isIE6 && (id == 'topNavTV' || id == 'topNavNews') && !testTV && !testNews) {
				dropdown.setStyle('visibility', 'visible');
			}
		}
	}
}
