var carousel = {
	'element': false,
	'nav': {
		'lrg_arrow_openwidth': 200,
		'lrg_arrow_closedwidth': 68,
		'sml_arrow_openwidth': 180,
		'sml_arrow_closedwidth': 56,
		'animduration': 300,
		'curimg': 0,
		'info_over':false,
		'info_over_timeout':false,
		'video_over':false,
		'video_over_timeout':false
	}
};
var videoplayer = false;

Cufon.replace('#navigation', { fontFamily: 'telenor_reg', hover: true });
Cufon.replace(['h3', '#nexttext', '#prevtext', '.thumb_hover_text'], { fontFamily: 'telenor_reg' });
Cufon.replace(['h1', 'h2', '#infobox'], { fontFamily: 'telenor_lite' });
$(document).ready(function(){
	Cufon.now();
	$(".colbox").colorbox();
	if ($('#carousel').length > 0) {
		$('#prevtext').html($('#carousel_img li img').last().attr('title'));
		$('.large #infobox').html($('#carousel_img li img:first-child').attr('title'));
		$('#nexttext').html($('#carousel_img li:first-child').next().children('img:first-child').attr('title'));
		Cufon.refresh();
		if ($('#carousel').hasClass('large')) {
			var arrowopenwidt = carousel.nav.lrg_arrow_openwidth;
			var arrowclosedwidth = carousel.nav.lrg_arrow_closedwidth;
		}
		if ($('#carousel').hasClass('small')) {
			var arrowopenwidt = carousel.nav.sml_arrow_openwidth;
			var arrowclosedwidth = carousel.nav.sml_arrow_closedwidth;
		}
		
		$('#nextbox').hover(
			function() {
				Cufon.refresh();
				$('#nextbox').animate({
						'width':arrowopenwidt
					}, {
						duration: carousel.nav.animduration,
						queue: false
					}
				);
			},
			function() {
				$('#nextbox').animate({
						'width':arrowclosedwidth
					}, {
						duration: carousel.nav.animduration,
						queue: false
					}
				);
			}
		);
		$('#carousel_next').click(
			function () {
				swapcarousel('next');
			}
		);
		$('#prevbox').hover(
			function() {
				Cufon.refresh();
				$('#prevbox').animate({
						'width': arrowopenwidt
					}, {
						duration: carousel.nav.animduration,
						queue: false
					}
				);
	
			},
			function() {
				$('#prevbox').animate({
						'width': arrowclosedwidth
					}, {
						duration: carousel.nav.animduration,
						queue: false
					}
				);
			}
		);
		$('#carousel_prev').click(
			function() {
				swapcarousel('prev');
			}
		);
		$('#carousel_info').hover(
			function() {
				if ($('.current img').hasClass('vjs-poster')) {
					
				} else {
					Cufon.refresh();
					carousel.nav.info_over = true;
					clearTimeout(carousel.nav.info_over_timeout);
					carousel.nav.info_over_timeout = setTimeout('hoverinfobox("over")', 200);
				}
			},
			function() {
				carousel.nav.info_over = false;
				clearTimeout(carousel.nav.info_over_timeout);
				carousel.nav.info_over_timeout = setTimeout('hoverinfobox("out")', 2000);
			}
		);
		$('#carousel_info').click(
			function() {
				if ($('.current img').hasClass('vjs-poster')) {
					if (videoplayer.paused()) {
						videoplayer.play();
						$('.vjs-big-play-button').first().fadeOut();
					} else {
						videoplayer.pause();
						$('.vjs-big-play-button').first().fadeIn();
					}
				} else {
				}
			}
		);
		$('#carousel').hover(
			function() {
				carousel.nav.video_over = true;
				clearTimeout(carousel.nav.video_over_timeout);
				carousel.nav.video_over_timeout = setTimeout('hovervideocontrols("over")', 1000);
			},
			function() {
				carousel.nav.video_over = false;
				clearTimeout(carousel.nav.video_over_timeout);
				carousel.nav.video_over_timeout = setTimeout('hovervideocontrols("out")', 2000);
			}
		);
	}
	if ($('#carousel.large').length > 0) {
		carousel.nav.animduration = 1000;
		setInterval('startslideshow()', 5000);
	}
	var thumbelements = jQuery.merge($('.thumbunit ul li'), $('.listunit ul li'));
	thumbelements.each(function() {
		var borderwidth = $(this).find('.hoverbox').first().css('border-top-width');
		borderwidth = parseInt(borderwidth.replace('px', '')) * 2;
		var elementwidth = $(this).find('.imgbox img').width()-borderwidth;
		if (elementwidth > $(this).find('.imgbox').width()-borderwidth) {
			elementwidth = $(this).find('.imgbox').width()-borderwidth;
		}
		$(this).find('.hoverbox').first().css('width', elementwidth);
	});
	thumbelements.hover(
		function() {
			$(this).children('.imgbox').children('.hoverbox').animate({
					'opacity':1
				}, {
					duration: carousel.nav.animduration,
					queue: false
				}
			);

		},
		function() {
			$(this).children('.imgbox').children('.hoverbox').animate({
					'opacity':0
				}, {
					duration: carousel.nav.animduration,
					queue: false
				}
			);
		}
	);
	thumbelements.click(function() {
		if ($(this).children('a').length > 0) {
			window.location = $(this).children('a').attr('href');
		}
	});
	$('#calls_to_action li').hover(
		function() {
			$(this).children('a').children('.imgbox').children('.hoverbox').animate({
					'opacity':1
				}, {
					duration: carousel.nav.animduration,
					queue: false
				}
			);

		},
		function() {
			$(this).children('a').children('.imgbox').children('.hoverbox').animate({
					'opacity':0
				}, {
					duration: carousel.nav.animduration,
					queue: false
				}
			);
		}
	);
	$('#calls_to_action li').click(function() {
		window.location = $(this).children('a').attr('href');
	});
	
	$('.thumbnails li a').hover(
		function() {
			$(this).animate({
					'opacity':0
				}, {
					duration: carousel.nav.animduration,
					queue: false
				}
			);

		},
		function() {
			$(this).animate({
					'opacity':1
				}, {
					duration: carousel.nav.animduration,
					queue: false
				}
			);
		}
	);

	if ($('#example_video_1').length > 0) {
		videoplayer = VideoJS.setup("example_video_1", {
			'controlsHiding': false
		});
	}
	// Adjusting vertical positioning/height of list elements based on height of image
	$('.hoverbox').each(function(index, element) {
		var borderthickness = parseInt($(element).css('border-top-width').replace('px', '')) * 2;
		var newheight = $(element).next('img').height()-borderthickness + 'px';
		$(element).css('height', newheight);
	});
	var listunitelements = jQuery.merge($('.listunit ul li h3'), $('.listunit ul li p'));
	listunitelements.each(function(index, element) {
		var imageheight  = $(element).siblings('span').first().height();
		var newpadding = Math.round((imageheight - $(element).height()) / 2) + 'px';
		$(element).css('padding-top', newpadding);
	});
});

