function init() {
	showNavigation();
	getRecordCount_entries('', 'gibmirinfoueberwievieletracksesgibt', '');
	getRecordCount(firstresult, genre, suchbegriff, filter1, filter2);
	getTableData(firstresult, genre, suchbegriff, filter1, filter2);
	setTimeout(function() {getNextData(firstresult, genre, suchbegriff, filter1, filter2);}, 500);
}

function addCommas(nStr)
{
	nStr += '';
	x = nStr.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + '.' + '$2');
	}
	return x1 + x2;
}

function getRecordCount_entries(firstresult, genre, suchbegriff) {
	var myAjax = new Ajax.Request('flow.php?action=count&suchbegriff=' + suchbegriff + '&firstresult=' + firstresult + '&genre=' + genre.replace(/ /g,'+'),
			{
				method: 'get', 
				parameters: '', 
				onComplete: function(response) {
					infouebertracks = response.responseText;
					Element.hide('loading_1');
					$('daentries').innerHTML += addCommas(infouebertracks);
				}
			});
}

function showNavigation() {
	if ((currentRecord+pagingSize) >= recordCount){maxrecord = recordCount;}
	else {maxrecord = (currentRecord+pagingSize);}

//	$('currentRec').innerHTML = "Displaying Results " + (currentRecord+1) + " to " + (currentRecord+pagingSize)
	$('currentRec').innerHTML = "Displaying Results " + (currentRecord+1) + " to " + maxrecord

	if(recordCount){$('currentRec').innerHTML += " of "+ recordCount;}
	//alert(recordCount);	
	(currentRecord == 0)
		?	$('previousLink1').style.visibility = 'hidden'
		:	$('previousLink1').style.visibility = 'visible';
	(currentRecord == 0)
		?	$('previousLink2').style.visibility = 'hidden'
		:	$('previousLink2').style.visibility = 'visible';

	((currentRecord + pagingSize) >= recordCount)
		?	$('nextLink1').style.visibility = 'hidden'
		:	$('nextLink1').style.visibility = 'visible';
	((currentRecord + pagingSize) >= recordCount)
		?	$('nextLink2').style.visibility = 'hidden'
		:	$('nextLink2').style.visibility = 'visible';
}

function getRecordCount(firstresult, genre, suchbegriff) {
	var myAjax = new Ajax.Request('flow.php?action=count&suchbegriff=' + suchbegriff + '&firstresult=' + firstresult + '&genre=' + genre.replace(/ /g,'+') + '&filter1=' + filter1 + '&filter2=' + filter2.replace(/ /g,'+'),
			{
				method: 'get', 
				parameters: '', 
				onComplete: function(response) {
					recordCount = response.responseText;
					$('currentRec').innerHTML += " of " + recordCount;
					showNavigation();
				}
			});
}

function getTableData(firstresult, genre, suchbegriff, filter1, filter2) {
//	firstresult = '3A';
//	genre = 'Progressive House';

	var myAjax = new Ajax.Request('flow.php?action=page&suchbegriff=' + suchbegriff + '&current='+currentRecord+'&size='+pagingSize+'&firstresult=' + firstresult + '&genre=' + genre.replace(/ /g,'+')+'&filter1=' + filter1 + '&filter2=' + filter2.replace(/ /g,'+'), 								
			{
				method: 'get', 
				parameters: '', 
				onComplete: function(response) {
					currentPage = eval('(' + response.responseText + ')');
					drawTable(currentPage, $('view'));
				}
			});
}

function getNextData(firstresult, genre, suchbegriff, filter1, filter2) {
	$('next').innerHTML = '';
	active = true;
	if(filter1 != '' || filter2 != '') {var myAjax = new Ajax.Request('flow.php?action=page&suchbegriff=' + suchbegriff + '&current='+(currentRecord+pagingSize)+'&size='+pagingSize+'&firstresult=' + firstresult + '&genre=' + genre.replace(/ /g,'+')+'&filter1=' + filter1 + '&filter2=' + filter2.replace(/ /g,'+'), 								
//	var myAjax = new Ajax.Request('flow.php?action=page&current='+(currentRecord+pagingSize)+'&size='+pagingSize, 
			{
				method: 'get', 
				parameters: '', 
				onComplete: function(response) {
					nextPage = eval('(' + response.responseText + ')');
					active = false;
					//drawTable(nextPage, $('next'));
				}
			});
			}
	else { var myAjax = new Ajax.Request('flow.php?action=page&suchbegriff=' + suchbegriff + '&current='+(currentRecord+pagingSize)+'&size='+pagingSize+'&firstresult=' + firstresult + '&genre=' + genre.replace(/ /g,'+'), 								
			{
				method: 'get', 
				parameters: '', 
				onComplete: function(response) {
					nextPage = eval('(' + response.responseText + ')');
					active = false;
					//drawTable(nextPage, $('next'));
				}
			});
			}
	}

function getPreviousData(firstresult, genre, suchbegriff, filter1, filter2) {
	if((currentRecord - pagingSize) >= 0) {
		$('previous').innerHTML = '';
		active = true;
		var myAjax = new Ajax.Request('flow.php?action=page&suchbegriff=' + suchbegriff + '&current='+(currentRecord-pagingSize)+'&size='+pagingSize+'&firstresult=' + firstresult + '&genre=' + genre.replace(/ /g,'+')+'&filter1=' + filter1 + '&filter2=' + filter2.replace(/ /g,'+'), 
//		var myAjax = new Ajax.Request('flow.php?action=page&current='+(currentRecord-pagingSize)+'&size='+pagingSize, 
				{
					method: 'get', 
					parameters: '', 
					onComplete: function(response) {
						previousPage = eval('(' + response.responseText + ')');
						active = false;
						//drawTable(previousPage,$('previous'));
					}
				});
	}
	else {
		$('previous').innerHTML = '';
	}
}

/*---------------------------------------------------------------*/

function drawTable(page, contain) {
	
	table = '';
	alt = '';
	for(i = 0; i < page['name'].length; i++) {
		
		name = page['name'][i].name;
		product_id = page['name'][i].product_id;
		product_url = page['name'][i].product_url;
		product_image = page['name'][i].product_image;
		product_mp3_file = page['name'][i].product_mp3_file;
		price_currency = page['name'][i].price_currency;
		price = page['name'][i].price;
		store = page['name'][i].store;

		if(store == "Beatport") { product_url = "http://www.beatport.com/en-US/html/content/track/detail/" + product_id + "/"; };

		tracking_pixel = page['name'][i].tracking_pixel;
		genre = page['name'][i].genre;
		artist = page['name'][i].artist;
		album = page['name'][i].album;
		label = page['name'][i].label
		release_date = page['name'][i].release_date;
		firstresult = page['name'][i].firstresult;
		secondresult = page['name'][i].secondresult;
		//secondresult = 'not found';
		bpm = page['name'][i].bpm;

		//if(store == "Beatport") { product_url = "http://www.beatport.com/en-US/html/content/track/detail/" + product_id + "/"; };
		//onclick


		// return overlib('<img src='+String.fromCharCode(34)+'img/logos2/cunda_logo_uebersicht.gif'+String.fromCharCode(34)+' />')
		// onmouseout="return nd();" onmouseover="return overlib(\'<img src=\'' + product_image + '\' />\');"

		// onmouseover="return overlib(\'<img src=\'' + product_image + ' />\');"

		//' onmouseover="return overlib(\'<img src=\''+ String.fromCharCode(34) + product_image + String.fromCharCode(34) + ' />\');" '

		// onmouseover="return overlib('<img src='+String.fromCharCode(34)+'img/logos2/cunda_160x600.gif'+String.fromCharCode(34)+' />')" 
		
		// onmouseout="javascript:return nd();"

		// onmouseover="javascript:return overlib(\'<img src=\'' + product_image + '\' />\'));"

		// prpduct_image_new = 		


		table += '<div id="track_' + i + '" class="list_item"><div class="title"><a target="_blank" onclick="javascript:pageTracker._trackPageview(\'/' + store + '-' + product_id + '\');" title="Details of: ' + name + '" href="http://www.beatport.com/en-US/html/content/track/detail/' + product_id + '/"><img width="30" height="30" alt="Cover of: ' + name + '" src="' + product_image + '"/></a></div>' +
					'<div class="artist"><a title="Search for: ' + name + '" href="search.php?firstresult=&genre=&suchbegriff=' + name + '+' + artist + '+' + label + '&submit=Suche+starten" target="_self">' + name + '</a></div>' +
					'<div class="duration"><a title="Search for: ' + label + '" href="search.php?firstresult=&genre=&suchbegriff=' + label + '&submit=Suche+starten" target="_self">' + label + '</a></div>' +
					'<div class="store"><a target="_blank" class="green" onclick="javascript:pageTracker._trackPageview(\'/' + store + '-' + product_id + '\');" title="Go to store" href="' + product_url + '">' + 'Download from ' + store + '</a><br/>' + '</div>' +
					'<div class="price"><span id="mp3_' + i + '">' +
					'<a href=""><img style="border: 0px none ;" src="loudspeaker.png" onclick="javascript:playMp3(\'mp3_' + i + '\', \'' + product_mp3_file + '\', \'Preview: ' + artist.replace(/'/g,"`").replace("&","and").replace("???","`").replace("???","`") + ' - ' + name.replace(/'/g,"`").replace("&","and").replace("???","`").replace("???","`") + '\'); return false;"/></a></span>' +
					'</div>' +
					'<br class="clear"/>' +
					'<div class="title"><img width="1" height="1" src="' + tracking_pixel + '"/></div>' +
					'<div class="artist"><a href="search.php?firstresult=&genre=&suchbegriff=' + artist + '&submit=Suche+starten" title="Search for: ' + artist + '" target="_self">' + artist + '</a>' +
					'</div>' +
					'<div class="duration">' + release_date + '</div>' +
					'<div class="store"><a title="Find more Tracks from: ' + genre + '" href="search.php?firstresult=' + firstresult + '&genre=' + genre + '&suchbegriff=&submit=Suche+starten" target="_self">' + genre + '</a></div>' +
					'<div class="price"><b>First Camelot Key: <a title="Find more Tracks from: ' + firstresult + '" href="search.php?firstresult=' + firstresult + '&genre=' + genre + '&suchbegriff=&submit=Suche+starten" target="_self">' + firstresult + '</a></b> | Second Cam. Key: <b><a title="Find more Tracks from: ' + secondresult + '" href="search.php?firstresult=' + secondresult + '&genre=' + genre + '&suchbegriff=&submit=Suche+starten" target="_self">' + secondresult + '</a></b><br/>approx. BPM: ' + bpm + '</div>';
		table += '<br class="clear"/></div>';// */
	}
	
	contain.innerHTML = table;
}

function drawTable_save(page, contain) {
	
	table = '';
	alt = '';
	for(i = 0; i < page['name'].length; i++) {
		
		name = page['name'][i].name;
		product_id = page['name'][i].product_id;
		product_url = page['name'][i].product_url;
		product_image = page['name'][i].product_image;
		product_mp3_file = page['name'][i].product_mp3_file;
		price_currency = page['name'][i].price_currency;
		price = page['name'][i].price;
		store = page['name'][i].store;
		tracking_pixel = page['name'][i].tracking_pixel;
		genre = page['name'][i].genre;
		artist = page['name'][i].artist;
		album = page['name'][i].album;
		label = page['name'][i].label
		release_date = page['name'][i].release_date;
		firstresult = page['name'][i].firstresult;
		secondresult = page['name'][i].secondresult;
		//secondresult = 'not found';
		bpm = page['name'][i].bpm;
		
		table += '<div id="track_' + i + '" class="list_item"><div class="title"><a target="_blank" title="Details of: ' + name + '" href="' + product_url + '"><img width="30" height="30" alt="Cover of: ' + name + '" src="' + product_image + '"/></a></div>' +
					'<div class="artist"><a title="Search for: ' + name + '" href="search.php?firstresult=&genre=&suchbegriff=' + name + '&submit=Suche+starten" target="_self">' + name + '</a></div>' +
					'<div class="duration"><a title="Search for: ' + label + '" href="search.php?firstresult=&genre=&suchbegriff=' + label + '&submit=Suche+starten" target="_self">' + label + '</a></div>' +
					'<div class="store"><a target="_blank" onclick="javascript:urchinTracker(/outgoing/' + store + ' Download);" title="Go to store" href="' + product_url + '">' + store + '</a><br/><a target="_blank" onclick="javascript:urchinTracker(/outgoing/Beatsdigital Download);" title="Go to store" href="' + product_url + '">' + price + ' ' + price_currency + '</a></div>' +
					'<div class="price"><span id="mp3_' + i + '">' +
					'<a href=""><img style="border: 0px none ;" src="loudspeaker.png" onclick="javascript:playMp3(\'mp3_' + i + '\', \'' + product_mp3_file + '\', \'Preview: ' + artist.replace("'","`").replace("&","and").replace("???","`").replace("???","`") + ' - ' + name.replace("'","`").replace("&","and").replace("???","`").replace("???","`") + '\'); return false;"/></a></span>' +
					'</div>' +
					'<br class="clear"/>' +
					'<div class="title"><img width="1" height="1"/></div>' +
					'<div class="artist"><a href="search.php?firstresult=&genre=&suchbegriff=' + artist + '&submit=Suche+starten" title="Search for: ' + artist + '" target="_self">' + artist + '</a>' +
					'</div>' +
					'<div class="duration">' + release_date + '</div>' +
					'<div class="store"><a title="Find more Tracks from: ' + genre + '" href="search.php?firstresult=' + firstresult + '&genre=' + genre + '&suchbegriff=&submit=Suche+starten" target="_self">' + genre + '</a></div>' +
					'<div class="price"><b>First: <a title="Find more Tracks from: ' + firstresult + '" href="search.php?firstresult=' + firstresult + '&genre=' + genre + '&suchbegriff=&submit=Suche+starten" target="_self">' + firstresult + '</a></b> | Second: <b><a title="Find more Tracks from: ' + firstresult + '" href="search.php?firstresult=' + secondresult + '&genre=' + genre + '&suchbegriff=&submit=Suche+starten" target="_self">' + secondresult + '</a></b> | BPM: ' + bpm + '</div>';
		table += '<br class="clear"/></div>';// */
	}
	
	contain.innerHTML = table;
}

function drawTable_old(page, contain) {
	table = 	'<table>';
	alt = '';
	for(i = 0; i < page['name'].length; i++) {
		table +=		'<tr class="'+alt+'">' +
						'<td>' + page['name'][i].name 	+ ',</td>' +
						'<td>' + page['name'][i].product_id 	+ '</td>' +
						'<td>' + page['name'][i].product_url	+ '</td>' +
						'<td>' + page['name'][i].product_image	+ '</td>' +
						'<td>' + page['name'][i].product_mp3_file	+ '</td>' +
						'<td>' + page['name'][i].price_currency	+ '</td>' +
						'<td>' + page['name'][i].price	+ '</td>' +
						'<td>' + page['name'][i].store	+ '</td>' +
						'<td>' + page['name'][i].tracking_pixel	+ '</td>' +
						'<td>' + page['name'][i].genre	+ '</td>' +
						'<td>' + page['name'][i].artist	+ '</td>' +
						'<td>' + page['name'][i].album	+ '</td>' +
						'<td>' + page['name'][i].label	+ '</td>' +
						'<td>' + page['name'][i].release_date	+ '</td>' +
						'<td>' + page['name'][i].firstresult	+ '</td>' +
						'<td>' + page['name'][i].secondresult	+ '</td>' +
						'<td>' + page['name'][i].bpm	+ '</td>' +
						'</tr>';
		(alt == '')
			?	alt = 'alt'
			:	alt = '';
	}
	
	table += '</table>';
	contain.innerHTML = table;
}

function getNextPage(firstresult, genre, suchbegriff, filter1, filter2) {
	if(!active) {
		currentRecord += pagingSize;
		showNavigation();
		previousPage = currentPage;
		currentPage = nextPage;
		drawTable(currentPage, $('view'));
		//drawTable(previousPage, $('previous'));
		getNextData(firstresult, genre, suchbegriff, filter1, filter2);
		lastplay[0] = "notset";
		lastplay[1] = "notset";
		lastplay[2] = "notset";
	}
}

function getPreviousPage(firstresult, genre, suchbegriff, filter1, filter2) {
	if(!active) {
		currentRecord -= pagingSize;
		showNavigation();
		nextPage = currentPage;
		currentPage = previousPage;
		drawTable(currentPage, $('view'));
		//drawTable(nextPage, $('next'));
		getPreviousData(firstresult, genre, suchbegriff, filter1, filter2);
		lastplay[0] = "notset";
		lastplay[1] = "notset";
		lastplay[2] = "notset";
	}
}