/* ON DOM LOAD
-----------------------------------------------------------------------*/
var dataUrl;
var GoToPageText; // = 'G&aring; till sidan';
var MMTitle = '';
var loadFirst = true;
var itemInfo;
var imageHolder;
var title;
var ShowLink;

var SYS = {}

$(document).ready(function() {

	SYS.Init();

	var slide = $('.mediamodule-list');
	itemInfo = $('.iteminfo');
	imageHolder = $('.image-holder');
	title = $('.mediamodule-title');

	setupDesign();
	title.append(MMTitle);

	$.ajax({

		type: 'GET',
		url: dataUrl,
		datatype: 'xml',
		contentType: "text/xml; charset=utf-8",
		success: function(xml) {
			//slide.css('display: hidden');
			$(xml).find('object').each(function() {
				var alt = $(this).find('alt').text();
				var thumbnail = $(this).find('thumbnail').text();
				var thumbHeight = $(this).find('thumbnailheight').text();
				if (thumbHeight == "-") { thumbHeight = ""; }
				var thumbWidth = $(this).find('thumbnailwidth').text();
				if (thumbWidth == "-") { thumbWidth = ""; }
				var islandscape = $(this).find('islandscape').text();
				var image = $(this).find('image').text();
				var heading = $(this).find('heading').text();
				var info = $(this).find('info').text();
				var link = $(this).find('link').text();
				var linktext = $(this).find('linktext').text();
				var content = $.trim($("content", this).text());
				SYS.item = $('<li class="mediamodule-item"></li>').html('<img src="' + thumbnail + '" width="' + thumbWidth + '" height="' + thumbHeight + '" alt="' + alt + '" />').appendTo(slide);


				if (loadFirst) {
					imageHolder.empty();
					itemInfo.empty();
					imageHolder.append(content); // add the content to the main player window
					loadContent(heading, info, link, linktext); // Add Heading and Metadata
					loadFirst = false;
				}


				$(SYS.item).click(function(e) {
					SYS.autoItemCount = $(this).prevAll().length;
					imageHolder.empty();
					itemInfo.empty();
					imageHolder.append(content); // add the content to the main player window
					loadContent(heading, info, link, linktext); // Add Heading and Metadata
					autoSlide();
				});
			});
			slide.show();
		},
		complete: function(xhr, status) {
			if (status == 'parsererror') { alert("Kunde inte tolka XMLen, antagligen fel format"); }
			initSlideNavigation();
		}
	});
});

SYS.Init = function() {
	this.item;
	this.autoItemCount = 1;
	this.itemCount = 0;
	this.currentPosition = 0;
	this.slideNavRight = ('a[name = navigation]');
	this.timeout = 0;
}


/* Setup structure and design
-----------------------------------------------------------------------*/
var setupDesign = function() {
	if (MMTitle == '') {
		imageHolder.css('top', '18px');
		itemInfo.css('bottom', '112px');
		title.css('display', 'none');
	}
	else {
		imageHolder.css('top', '0px');
		itemInfo.css('bottom', '98px');
		title.css('display', 'block');
	}
}

/* SERVICE ITEM MODAL POPUP
-----------------------------------------------------------------------*/
var loadContent = function(heading, infoText, link, linktext) {
	if (heading == '') { itemInfo.css('visibility', 'hidden'); return; }
	itemInfo.css('height', '29px');
	itemInfo.css('visibility', 'visible');
	itemInfo.fadeTo('slow', 0.8);

	$('<h3 class=\"item-info-heading\"></h3>').html(heading).appendTo(itemInfo);
	$('<p></p>').html(infoText).appendTo(itemInfo);
	if (link != '' && ShowLink == 'True') {
		if (linktext != '')
			$('<p></p>').html('<a href="' + link + '">' + linktext + '</a>').appendTo(itemInfo);
		else
			$('<p></p>').html('<a href="' + link + '">' + GoToPageText + '</a>').appendTo(itemInfo);
	}
	$('.item-info-heading').hover(
		function(over) {
			$('.item-info-heading').css('color', '#15A9AB');
		},
		function(out) {
			$('.item-info-heading').css('color', '#000000');
		});
	$('.item-info-heading').click(function(e) {
		autoSlide();
		if (itemInfo.css('height') == '89px') {
			itemInfo.animate({
				height: '29px'
			}, 500);
		}
		else {
			itemInfo.animate({
				height: '89px'
			}, 500);
		}
	});
}

/* SLIDE NAVIGATION FOR SERVICE ITEMS
-----------------------------------------------------------------------*/
var initSlideNavigation = function() {

	autoSlideNavigation(5000);

	//select all the a tag with name equal to slide
	var id = $('.mediamodule-clip');
	var slide = $('.mediamodule-list');
	var serviceItems = $('.mediamodule-list .mediamodule-item');
	if (!serviceItems || serviceItems.length == 0) { return; }

	//Number of service items with 12px margin
	$(slide).css({ 'width': serviceItems[0].offsetWidth * serviceItems.length + serviceItems.length * 12 });
	$('#mediamodule .mediamodule-wrap .mediamodule-next-horizontal').addClass('mediamodule-next-disabled');
	$('#mediamodule .mediamodule-wrap .mediamodule-prev-horizontal').addClass('mediamodule-prev-disabled');
	//Check if slide contains hissen items. If so, display navigation arrow
	if (slide.width() > $(id).width()) {
		$('#mediamodule .mediamodule-wrap .mediamodule-next-horizontal').removeClass('mediamodule-next-disabled');
	}

	$('a[name=navigation]').click(function(e) {

		//Cancel the link behavior
		e.preventDefault();

		if (this.className == 'mediamodule-next-horizontal') {
			$(slide).animate({ left: SYS.currentPosition - serviceItems[0].offsetWidth - 12 }, 'fast', function() {
				SYS.currentPosition = SYS.currentPosition - serviceItems[0].offsetWidth - 12;
				if (SYS.currentPosition < 0) $('#mediamodule .mediamodule-wrap .mediamodule-prev-horizontal').removeClass('mediamodule-prev-disabled');
				SYS.itemCount++;
				if (SYS.itemCount + 4 == serviceItems.length) $('#mediamodule .mediamodule-wrap .mediamodule-next-horizontal').addClass('mediamodule-next-disabled');
			});
		}
		else if (this.className == 'mediamodule-prev-horizontal') {
			$(slide).animate({ left: SYS.currentPosition + serviceItems[0].offsetWidth + 12 }, 'fast', function() {
				SYS.currentPosition = SYS.currentPosition + serviceItems[0].offsetWidth + 12;
				if (SYS.currentPosition >= 0) $('#mediamodule .mediamodule-wrap .mediamodule-prev-horizontal').addClass('mediamodule-prev-disabled');
				SYS.itemCount--;
				if (SYS.itemCount + 4 < serviceItems.length) $('#mediamodule .mediamodule-wrap .mediamodule-next-horizontal').removeClass('mediamodule-next-disabled');
			});
		}
	});
}

var autoSlideNavigation = function(time) {
	SYS.timeout = setTimeout(function() {
		$('#serviceslide').children().eq(SYS.autoItemCount).trigger('click');
		SYS.autoItemCount++;
		if (SYS.autoItemCount > $('#serviceslide').children().length) {
			SYS.autoItemCount = 0;
		}
		if (SYS.timeout > 0) clearTimeout(SYS.timeout);
		autoSlideNavigation(5000);
	}, time);
};


var autoSlide = function() {
	if (SYS.timeout > 0) clearTimeout(SYS.timeout);
	autoSlideNavigation(0);
};
