//Details for an entry in the chart
CHARTENTRY = new Array();

function loadChartEntry(liid, sart) {

	//Save current content to history and save current action
	saveHistory();
	saveCurrentAction({'action':'chartentry', 'liid':liid, 'sart':sart});

	if (!isDefined(CHARTENTRY[liid]))
		CHARTENTRY[liid] = new Array();

	if (!isDefined(CHARTENTRY[liid][sart])) {
		//Start session and load defaults
		$.getJSON(
			SCRIPTURL + 'chartentry.json?xpdato=' + $.cookie("xpdato") + '&xpwlnr=' + $.cookie("xpwlnr") + '&liid=' + liid + '&sart=' + sart + '&callback=?',
			function (data) {
				if (isDefined(data.chartentry) && isDefined(data.chartentry.itemdata) && isDefined(data.chartentry.itemdata.arso)) {
					//save info on cache
					CHARTENTRY[liid][sart] = data.chartentry.itemdata;

					//build the table and update
					showContent(buildChartEntryContent(data.chartentry.itemdata));
				}
				else {
					console.log('Error retrieving chart entry. Data undefined.');
					showContent(
						'<p class="msg ui-state-highlight ui-corner-all">\
							<span class="ui-icon ui-icon-alert"></span>\
							Sorry, there was a problem while retrieving chart entry info.\
						</p>'
					);
				}
			}
		);
	}
	else {
		//If it is already loaded, build the table and update
		showContent(buildChartEntryContent(CHARTENTRY[liid][sart]));
	}
}


function buildChartEntryContent(itemdata) {
	//insert processed data in itemdata var
	itemdata.topTable		= buildChartEntryTopTable(itemdata);
	itemdata.backButton		= buildBackButton();
	itemdata.facebookButton	= buildFacebookButton();
	itemdata.posInfo		= buildChartEntryPositionInfo(itemdata);
	itemdata.certInfo		= buildChartEntryCertificatesInfo(itemdata);
	itemdata.additemsInfo	= buildChartEntryAdditionalItemsInfo(itemdata);
	itemdata.historyInfo	= buildChartEntryHistoryInfo(itemdata);
	itemdata.searchResult	= buildSearchResultRows(itemdata.result);
	itemdata.buttonBox		= buildButtonBox(itemdata, true);

	//then build the content and return
	return '\
		' + itemdata.buttonBox + '\
		<div id="chartentry" class="box2">\
			<h2>Artikelinformation</h2>\
			' + itemdata.topTable + '\
			' + itemdata.posInfo + '\
			' + itemdata.certInfo + '\
			' + itemdata.additemsInfo + '\
			' + itemdata.historyInfo + '\
			<div class="info" id="searchinfo" >\
				<h2>Artist Search</h2>\
				<div class="info" >' + itemdata.searchcount + ' other entries found for artist: <em>' + itemdata.arso + '</em></div>\
				' + itemdata.searchResult + '\
			</div>\
		</div>\
		' + itemdata.buttonBox;
}


function buildChartEntryTopTable(itemdata) {
	//insert processed data into itemdata var
	itemdata.classes	= getPosRowClasses(itemdata.lng);
	itemdata.newKva		= buildCoverImage(itemdata.kva ? BASEURL + itemdata.kva : NOIMAGE_KVA, itemdata.arso, itemdata.tit, 'kva');
	itemdata.newLng		= buildTopLngImage(itemdata.lng);
	itemdata.catName	= buildCatName(itemdata.bol, itemdata.boltxt);
	itemdata.buttons	= buildBuyLink(itemdata.buyExt);

	//then build the content and return
	return '\
		<table class="toppos" >\
			<tbody>\
				<tr class="' + itemdata.classes + '">\
					<td class="image kva">' + itemdata.newKva + '</td>\
					<td class="text info">\
						<span class="artist">' + itemdata.arso + '</span><br />\
						<span class="title">' + itemdata.tit + '</span><br />\
						' + itemdata.newLng + '<br />\
						<h3 class="lbtxt">' + itemdata.lbtxt + '</h3>\
						<span class="label">' + itemdata.labl + '</span><br />\
						' + itemdata.catName + ' <span class="catnum">' + itemdata.katn + '</span><br />\
						<div class="entrybuttons">' + itemdata.buttons + '</div>\
					</td>\
				</tr>\
			</tbody>\
		</table>';
}

function buildChartEntryPositionInfo(itemdata) {
	//insert processed data into itemdata var
	itemdata.splitdate = splitDate(itemdata.entdate);
	
	//build content and return
	return '\
		<div class="info" id="entranceinfo" >\
			<h2>Chart Entrance Information</h2>\
			<span class="entrancepos"><strong>Första placering:</strong> ' + itemdata.entpos + '</span><br />\
			<span class="entranceperiod"><strong>Period:</strong> ' + itemdata.entbtxt + '</span><br />\
			<span class="entrancedate"><strong>Datum:</strong> ' + itemdata.splitdate + '</span><br />\
			<span class="peekposition"><strong>Högsta placering:</strong> ' + itemdata.peekpos + '</span><br />\
			<span class="periodsinchart"><strong>Listas:</strong> ' + itemdata.avli + '</span>\
		</div>';
}

function buildChartEntryAdditionalItemsInfo(itemdata) {
	var additems_html = '';

	if(isDefined(itemdata.additems)) {

		var countShownItems = 0;

		$.each(itemdata.additems, function(key, value) {
			if (value.show == 'true') {
				countShownItems++;
				//insert processed data into value var
				value.catName = buildCatName(value.bol, value.boltxt);
				value.buttons = buildBuyLink(value.buyExt);
				//then build and contatenate content
				additems_html += '\
					<div class="additem" >\
						<div class="additemimg" >\
							<img src="images/additems/' + value.lb + '.png" alt="' + value.lbtxt + ' Icon" />\
						</div>\
						<div class="additemdesc" >\
							<h3>' + value.lbtxt + '</h3>\
							<em>' + value.tit + '</em><br />\
							' + value.catName + ' ' + value.katn + '\
							<div class="entrybuttons">' + value.buttons + '</div>\
						</div>\
					</div>';
			}
		});
		if (countShownItems == 0)
			additems_html = "<em>Inga kopplade artiklar</em>";
	}
	else {
		additems_html = "<em>Inga kopplade artiklar</em>";
	}
	return '\
		<div class="info" id="additemsinfo" >\
			<h2>Kopplade artiklar</h2>\
			' + additems_html + '\
		</div>';
}

function buildChartEntryCertificatesInfo(itemdata) {
	if (itemdata.certificates instanceof Array) {
		var certs_html = '';
		$.each(itemdata.certificates, function(key, value){
			//insert processed data into value var
			value.certImg		= getCertImage(value.certtxt);
			value.newCertDate	= splitDate(value.certdate);
			//then prepends the content
			certs_html = '<strong>' + value.certImg + ' ' + value.certtxt + '</strong> ' + value.newCertDate + '<br />' + certs_html;
		});
	}
	var certsnumber	= (itemdata.certificates instanceof Array) ? itemdata.certificates.length : 0;
	var certslist	= (itemdata.certificates instanceof Array) ? certs_html : '';
	return '\
		<div class="info" id="certsinfo" >\
			<h2>Certifikat</h2>\
			<span class="certsnumber"><strong>Antal certifikat:</strong> ' + certsnumber + '</span><br />\
			<div class="certslist">' + certslist + '</div>\
		</div>';
}

function buildChartEntryHistoryInfo(itemdata) {
	var history_html = '';
	$.each(itemdata.chartpos.pos, function(key, value) {
		//insert processed data into value var
		/*value.newPbdat	= splitDate(value.pbdat);*/
		value.liid		= itemdata.chartpos.liid;
		//build and contatenate content
		history_html += '\
			<tr>\
				<td class="center row" >' + value.rowid + '</td>\
				<td class="number pos" >' + value.pbpl + '</td>\
				<td class="text summary">\
					<a href="javascript:void(0);" onclick="loadChart(' + value.liid + ', ' + value.dspy + ', ' + value.dspp + ')" title="Visa lista" >\
						<img class="noprint" src="images/loadchart.png" alt="Visa lista" style="vertical-align: middle;" /> <span>' + value.btxt + '</span>\
					</a>\
				</td>\
			</tr>';
	});
	return '\
		<div class="info" id="historyinfo" >\
			<h2>Placeringar på lista</h2>\
			<table id="historyinfotable" >\
				<thead>\
					<tr>\
						<th>Perioder</th>\
						<th>Placering</th>\
						<th>Listas</th>\
					</tr>\
				</thead>\
				<tbody>\
					' + history_html + '\
				</tbody>\
			</table>\
		</div>';
}


function splitDate(fulldate) {
	if(isDefined(fulldate) && fulldate.length == 8)
		return fulldate.substring(0,4) + '-' + fulldate.substring(4,6) + '-' + fulldate.substring(6); //yyyy-mm-dd
	return fulldate;
}

function getCertImage(certtxt) {
	return '<img src="images/certsmall_' + (certtxt == 'Guld' ? 'guld' : 'plat') + '.png" alt="' + certtxt + ' icon" />';
}

