
addthis_config = { ui_language: "sv" }

$(document).ready(function() {
	initVideos();
	initImageGallery();
	initComments($('.comments'));
	initEPGs();
	initAdminControls();
});

/**************** Admin-kontroller ****************/

function initAdminControls() {
	
	/* Clear cache controls */
	$('.cache-admin-controls-clearcache').click(function(event) {
		event.preventDefault();
		var orgElemId = $(this).attr('id');
		$.post($(this).attr('href'), function(data) {
			$("#" + orgElemId + "-progress").hide();
			$("#" + orgElemId + "-done").show().fadeOut(1000, function() {
				$("#" + orgElemId + "-reloadpage").show();
				$("#" + orgElemId).show();
			});
		});
		$("#" + orgElemId + "-progress").show();
		$(this).hide();
	});

	$('.sitelets-admin-header-edit, .cache-admin-controls-clearcache').hover(
			function(){
				$("#" + $(this).attr('title')).addClass("highlighted")
			},
			function(){
				$("#" + $(this).attr('title')).removeClass("highlighted");
			}
	);

	var hideAdminControlsElement = $('#hide-admin-controls');

	if (hideAdminControlsElement.length) {
		hideAdminControlsElement.cookieBind();

		function hideOrShowAdminControls() {
			var elements = $('.sitelets-admin-header-wrapper, .cache-admin-controls, .article-admin-wrapper');
			if (hideAdminControlsElement.attr('checked')) elements.hide();
			else elements.show();
		}

		hideAdminControlsElement.change(hideOrShowAdminControls);
		hideOrShowAdminControls();
	}
}

/**************** Kommentarer ****************/

function initComments(commentsElement) {
	var textarea = commentsElement.find('textarea');

	textarea.keyup(function() {
		handleMessageLength(commentsElement, $(this).val());
	});

	textarea.bind('paste', function(e) {
		handleMessageLength(commentsElement, e.event);
	});

}

function handleMessageLength(commentsElement, text) {
	if (text) {
		commentsElement.find('.submit-button').attr('disabled', text.length > 300 ? 'disabled' : null);
		commentsElement.find('.message-length').text(text.length);
		commentsElement.find('.message-length-info').css('color', text.length > 300 ? 'red' : 'black');
	}
}

function listErrors(element, errors) {
	element.html('Följande fel uppstod:<ul>');

	for (i in errors) {
		element.append('<li>' + errors[i] + '</li>');
	}

	element.append('</ul>');
	element.show();
}

/**************** Bildgalleri ****************/

function initImageGallery() {
	$('.image-gallery-display').each(function(i) {
		var display = $(this);

		var activeItem = display.find('.item:first');
		activeItem.addClass('active');
		imageGalleryCreateSlide(display, activeItem, 'in');
		imageGalleryCreateSlide(display, activeItem.next(), 'out');

		Cufon.refresh('.image-gallery-display .slide .info div');

		display.find('.slide').css('opacity', '0');
		display.find('.slide.in').animate({opacity: 1}, 500);

		display.append('<div class="nav left"><img src="/public/images/nav-left.png" /></div>');
		display.append('<div class="nav right"><img src="/public/images/nav-right.png" /></div>');
		var navs = display.find('.nav');
		navs.css('opacity', '0');

		display.hover(function(event) {
			navs.css('opacity', '0.5');
		}, function(event) {
			navs.css('opacity', '0');
		});

		navs.hover(function(event) {
			$(this).css('opacity', '1');
		}, function(event) {
			navs.css('opacity', '0.5');
		});

		navs.click(function(event) {
			imageGalleryNav(display, $(this).is('.left'));
		});
	});

	$('.image-gallery-listing div').click(function(event) {
		imageGalleryPick($(this).index());
	});
}

function imageGalleryCreateSlide(display, item, inOrOut) {
	display.append(
		'<div class="slide ' + inOrOut + '">' +
		'<img src="' + item.find('.image').html() + '" width="740" height="416" />' +
		'<div class="info">' + item.find('.info').html() + '</div>' +
		'</div>'
	);
}

function imageGalleryNav(display, backwards) {
	var fromItem = display.find('.item.active');
	var toItem = nextGalleryItem(display, fromItem, backwards);
	imageGallerySwitch(display, toItem);
}

function imageGalleryPick(index) {
	var display = $('.image-gallery-display');
	var toItem = display.find('.item:nth-child(' + (index + 1) + ')');
	if (!toItem.is('.active')) imageGallerySwitch(display, toItem);
}

function nextGalleryItem(display, item, backwards) {
	var item = backwards ? item.prev('.item') : item.next('.item');
	if (item.length == 0) item = display.find(backwards ? '.item:last' : '.item:first');
	return item;
}

function imageGallerySwitch(display, toItem) {
	if (!display.data('locked')) {
		display.data('locked', true);

		display.find('.item.active').removeClass('active');
		toItem.addClass('active');

		var fromSlide = display.find('.slide.in');
		var toSlide = display.find('.slide.out');

		fromSlide.removeClass('in');
		fromSlide.addClass('out');
		toSlide.removeClass('out');
		toSlide.addClass('in');

		var toImage = toItem.find('.image').html();
		toSlide.find('img').attr('src', toImage);
		toSlide.find('.info').html(toItem.find('.info').html());
		toSlide.css('opacity', '1');

		Cufon.refresh('.image-gallery-display .slide .info div');

		fromSlide.animate({opacity: 0}, 500, function() {
			var nextItem = nextGalleryItem(display, toItem, false);
			fromSlide.find('img').attr('src', nextItem.find('.image').html());

			display.removeData('locked');
		});

		trac.slideEvent(toImage); // Nielsen event
	}
}

/**************** Tablåer ****************/

function initEPGs() {
	$('.epg .entries li, .mini-epg li').click(function(event)
	{
		var li = $(this);

		li.addClass('open');

		var div = li.find('div');

		if (div.length == 0)
		{
			$.get(li.find('.url').html(), function(entry){
				var html = '<div class="description" style="display: none">';

				var name = li.find('.name');
				if (name.length) {
					html += name.html() + ': ';
				}

				html += entry.description ? entry.description : 'Ingen beskrivning';
				if (entry.url) html += ' <a href="' + entry.url + '"><nobr>Läs mer<' + '/nobr><' + '/a>';
				html += '<' + '/div>';

				li.append(html);
				li.click();
			});
		}
		else
		{
			if (div.css('display') == 'none')
			{
				div.slideDown(250);
			}
			else
			{
				div.slideUp(250, function()
				{
					li.removeClass('open');
				});
			}
		}
	});
}

/**************** Videor ****************/

function initVideos() {
	var staticEmbeds = $('.BrightcoveExperience');
	var dynamicEmbeds = $('.video-embed');

	if (staticEmbeds.length || dynamicEmbeds.length) {
		$.getScript('http://admin.brightcove.com/js/BrightcoveExperiences.js', function() {

			dynamicEmbeds.each(function() {
				var videoId = $(this).find('param[name=id]').attr('value');

				$(this).replaceWith(
					'<div class="video-embedded">' +
					'<object id="myExperience' + videoId + '" class="BrightcoveExperience">' +
					'<param name="bgcolor" value="#FFFFFF" />' +
					'<param name="width" value="450" />' +
					'<param name="height" value="253" />' +
					'<param name="playerID" value="73915700001" />' +
					'<param name="publisherID" value="22710239001" />' +
					'<param name="isSlim" value="true" />' +
					'<param name="optimizedContentLoad" value="true" />' +
					'<param name="@videoPlayer" value="' + videoId + '" />' +
					'</object>' +
					'</div>'
				);
			});

			brightcove.createExperiences();
		});
	}
}

