var ExtRender = {
	lastFmResponsed : true
};
jQuery.extend(ExtRender, {		
	getDataShowRecord : function() {
		// getting item info: title, author, type, faustNum
		var author = $('.record .recordInfo .author').text();
		var title = $('.record .recordInfo .title').text();
		var isbn = $('.record .recordInfo .isbn').text();
		var recordId = $('.record .recordInfo .recordId').text();
		var type = $('#recordData .recordImageContainer .recordImageLarge img:first').attr('src').split('/').pop().split('.')[0];
		var recordTypeText = $('#recordData .recordTypeText').html();		
		
		var showTeasers = function (teasers) {
			var first = $('#externalResourcesBookTeasers .similarContent .similarItem:first');

			first.find('span.simTiAuthor').text(teasers[0].author);
			link = first.find('span.simTiLink a').attr('href');
			first.find('span.simTiLink a').attr('href', link+'?recordId='+teasers[0].id).text(teasers[0].title);

			var img = new Image();
			$(img).load(function() {
				ratio = img.width / 100;
				img.width = 100;
				img.height = Math.round(img.height / ratio);
				first.find('.similarPicture').append(img);
			});
			img.src = teasers[0].data.thumbnail;

			$(img).click(function() {
				window.location = link+'?recordId='+teasers[0].id;
			});
			
			first.find('.readersOpinion').empty();
			var rating0 = 0;
			if (teasers[0].rating)
				rating0 = teasers[0].rating;
			for ($i=1; $i<=5; $i++) {
				if ($i <= Math.round(rating0))
					first.find('.readersOpinion').append('<img src="images/opacStarActive.png" alt="" />');
				else
					first.find('.readersOpinion').append('<img src="images/opacStarInactive.png" alt="" />');
			}

			if(teasers[1]) {
				var second = $('#externalResourcesBookTeasers .similarContent .similarItem:last');

				second.find('span.simTiAuthor').text(teasers[1].author);
				link = second.find('span.simTiLink a').attr('href');
				second.find('span.simTiLink a').attr('href', link+'?recordId='+teasers[1].id).text(teasers[1].title);

				var img2 = new Image();
				$(img2).load(function() {
					ratio = img2.width / 100;
					img2.width = 100;
					img2.height = Math.round(img2.height / ratio);
					second.find('.similarPicture').append(img2);
				});
				img2.src = teasers[1].data.thumbnail;

				$(img2).click(function() {
					window.location = link+'?recordId='+teasers[1].id;
				});
				
				second.find('.readersOpinion').empty();
				var rating1 = 0;
				if (teasers[1].rating)
					rating1 = teasers[1].rating;
				for ($i=1; $i<=5; $i++) {
					if ($i <= Math.round(rating1))
						second.find('.readersOpinion').append('<img src="images/opacStarActive.png" alt="" />');
					else
						second.find('.readersOpinion').append('<img src="images/opacStarInactive.png" alt="" />');
				}

				second.show();
			}
			$('.similarTeaserHider').slideDown();
		};
	
		var showAuthorPortrayal =  function(fw, ls) {
			$( function() {
				$('div.tabs ul.tabNavigation a').click(
					function() {
						EasyOpac.toggleExtResources(this);
						return false;
					});	
				$('div.tabs ul.tabNavigation:first-child a').click();
			});
			var anchorHash = EasyOpac.getAnchorFromUrl();
			if (null != ls) {
				$('#portrayalLitteratursiden').find('.quote span:first').html('Der blev fundet et portr&aelig;t p&aring; Litteratursiden<br>');
				$('#portrayalLitteratursiden').find('.quote a').attr('href', ls['dc.identifier']);
				$('#linkPortrayalLitteratursiden').fadeIn();
				$('#portrayalLitteratursiden').show();
				if (anchorHash == "litteraturSiden") {
					$("#linkPortrayalLitteratursiden a").click();
				}
				if ((anchorHash == null || anchorHash == "")&& $('#extPortret .tabNavigation li:visible a.selected:visble').length == 0) {
					$("#linkPortrayalLitteratursiden a").click();
				}
			}
			if (null != fw) {
				$('#portrayalForfatterweb').find('.quote span:first').text(fw.abstract);
				$('#portrayalForfatterweb').find('.quote a').attr('href', fw.url);
				$('#linkPortrayalForfatterweb').fadeIn();
				$('#portrayalForfatterweb').show();
				if (anchorHash == "forfatterweb") 
					$("#linkPortrayalForfatterweb a").click();
				if ((anchorHash == null || anchorHash == "")&& $('#extPortret .tabNavigation li:visible a.selected:visble').length == 0) {
					$("#linkPortrayalForfatterweb a").click();
				}
			}
		};
		
		var showBogpriserPrice = function (bogpriser) {	
			if (true == bogpriser.priceAvailable) { 
				$('.extResBogpriserText').attr('href', bogpriser.url);
				$('.extResBogpriserText').append(bogpriser.priceDKK); 
				$('.bookPriceData').show().next().show();
				$('.extResBogpriserContainer').fadeIn();
			} else {
				// hide the whole data group
				$('.bookPriceData').hide().next().hide();
			}
		};
		
		var showLastFm = function (lastfmArtist) {
			if (lastfmArtist != false) {
				var lfm = $('#extLastfm');

				// Description
				lfm.find('.extLastfmDescription').html(lastfmArtist.bio);

				// Url
				lfm.find('.extLastfmShowMore').attr('href', lastfmArtist.url);

				// Listeners
				lfm.find('.listenersRightNow').text(lastfmArtist.listeners);

				// Playcount
				lfm.find('.timesListened').text(lastfmArtist.playcount);

				// Artist Image
				lfm.find('.artistImage').attr('src', lastfmArtist.image);

				// Similar artist
				var sims = Array();
				
				if (lastfmArtist.similar.length > 0) {
					$.each(lastfmArtist.similar, function(index, elm) {
						sims.push('<a href="'+EasyOpac.searchResultBaseUrl+'?query='+elm+'" class="opacLink">'+elm+'</a>');
					});
				}
				lfm.find('.similarArtist').html(sims.join('&nbsp;,&nbsp;'));

				lfm.show();
				
				$('#linkPortrayalLastFm').show();
			} else {
				$('#linkLastFm').remove();
			}
		};
		
		var a = {type : type, author: author, title: title, faust: isbn, recordId: recordId, recordTypeText: recordTypeText};
		$.inleadAjaxSync('externalsctl', {type : type, author: author, title: title, faust: isbn, recordId: recordId, recordTypeText: recordTypeText}, function(resp) {
			if (resp.status == 1) {
				if (resp.data.lastfmArtist) {
					showLastFm(resp.data.lastfmArtist);
				}
				if (resp.data.bogpriser) {
					showBogpriserPrice(resp.data.bogpriser);
				}
				if (resp.data.fw || resp.data.ls) {
					showAuthorPortrayal(resp.data.fw, resp.data.ls);
				} else {
					$('#linkPortrayalForfatterweb').hide();
					$('#linkPortrayalLitteratursiden').hide();
				}
				if (resp.data.teasers && resp.data.teasers.length > 0) {
					showTeasers(resp.data.teasers);
				}
				// If there is no extRes available, the div will be shown.
				if (resp.data.lastfmArtist || resp.data.bogpriser || resp.data.fw || resp.data.ls) {
					$('#externalResources').show();
				}
			}
		});		
	},
	
	getDataSearchResult : function(recordType, author, searchRecord) {
		var showLastfmAlbumart = function (lastfmAlbumart, searchRecord) {
			if(lastfmAlbumart != null) {
				$(searchRecord).find('.lastfmIcon').show();
			} else {
				$('#linkLastFm').remove();
			}
		};
		
		var showAuthorPortrayal = function(fw, searchRecord) {
			if (fw && fw.url != null) {
				$(searchRecord).find('.forfatterWebIcon').fadeIn();
			}
		};
		
		var showLiteratursiden = function(ls, searchRecord) {
			if (ls) {
				$(searchRecord).find('.litteraturSidenIcon').fadeIn();
			} else {
				$(searchRecord).find('.litteraturSidenIcon a').hide();
				$(searchRecord).find('.litteraturSidenIcon').fadeOut();
			}
		}
		
		var albumTitle = $(searchRecord).getRecordInfo('title');
		$.inleadAjax('externalsctl', {searchResult : 1, type : recordType, author : author, album : albumTitle}, function(resp) {
			showLastfmAlbumart(resp.data.lastfmAlbumart, searchRecord);
			showAuthorPortrayal(resp.data.fw, searchRecord);
			showLiteratursiden(resp.data.ls, searchRecord);
		});
	}
});
