// New dbase interface to make life easier.

var downloadPath;
var downloadExt;
var imagePath;
var imageExt;

function animations_preview_size(element, width, stepsize, speed){
	if(element.size_timer) {
		clearInterval(element.size_timer);
	}
	width=width;
	element.state="busy";
	element.size_timer=setInterval(
		function() {
			element.currentWidth= parseInt(element.style.width); 
			
			if(element.currentWidth>(width+stepsize)) {
				element.style.width= element.currentWidth - (2*stepsize);
			} 
			if(element.currentWidth<(width-stepsize)) {
				element.style.width= element.currentWidth + (2*stepsize);
			}
		
			if((element.currentWidth<=width+stepsize && element.currentWidth>=width-stepsize)) {
				clearInterval(element.size_timer);
				element.style.width=width;
				element.state="done";
			}
		}
	, speed);
}

function convertArray(db_array){
	temp_array = new Array();
	var temp= new Array();
	var tempIndex=0;
	
	// Remove all letters from array. Not needed anymore
	for(letter=0; letter<=25; letter++) {	
		for(index=0; index<db_array[letter].length; index++) {
			temp_array[tempIndex] = new Array(5);
			temp_array[tempIndex] = db_array[letter][index];

			// Replace months with numbers
			temp_array[tempIndex][2]=temp_array[tempIndex][2].replace(/january/i,		"01");
			temp_array[tempIndex][2]=temp_array[tempIndex][2].replace(/januari/i,		"01");
			temp_array[tempIndex][2]=temp_array[tempIndex][2].replace(/february/i,		"02");
			temp_array[tempIndex][2]=temp_array[tempIndex][2].replace(/februari/i,		"02");
			temp_array[tempIndex][2]=temp_array[tempIndex][2].replace(/march/i,		"03");
			temp_array[tempIndex][2]=temp_array[tempIndex][2].replace(/maart/i, 		"03");
			temp_array[tempIndex][2]=temp_array[tempIndex][2].replace(/april/i, 		"04");
			temp_array[tempIndex][2]=temp_array[tempIndex][2].replace(/may/i, 			"05");
			temp_array[tempIndex][2]=temp_array[tempIndex][2].replace(/mei/i, 			"05");
			temp_array[tempIndex][2]=temp_array[tempIndex][2].replace(/june/i, 		"06");
			temp_array[tempIndex][2]=temp_array[tempIndex][2].replace(/july/i, 		"07");
			temp_array[tempIndex][2]=temp_array[tempIndex][2].replace(/augustus/i, 	"08");
			temp_array[tempIndex][2]=temp_array[tempIndex][2].replace(/august/i, 		"08");
			temp_array[tempIndex][2]=temp_array[tempIndex][2].replace(/september/i, 	"09");
			temp_array[tempIndex][2]=temp_array[tempIndex][2].replace(/october/i, 		"10");
			temp_array[tempIndex][2]=temp_array[tempIndex][2].replace(/oktober/i, 		"10");
			temp_array[tempIndex][2]=temp_array[tempIndex][2].replace(/november/i, 	"11");
			temp_array[tempIndex][2]=temp_array[tempIndex][2].replace(/december/i, 	"12");
			
			// Remove possible -
			temp_array[tempIndex][2]=temp_array[tempIndex][2].replace(/-/, " ");
			
			// Switch year and month for sorting purposes
			temp = temp_array[tempIndex][2].split(' ');
			temp_array[tempIndex][2] = temp[1]+"-"+temp[0];
			
			tempIndex++;
		}
	}
	return temp_array;
}

function mapArray(db_array, mapping) {
	var returnArray = new Array(5);
	returnArray[0] = db_array[mapping[0]];
	returnArray[1] = db_array[mapping[1]];
	returnArray[2] = db_array[mapping[2]];
	returnArray[3] = db_array[mapping[3]];
	returnArray[4] = db_array[mapping[4]];
	return returnArray;
}

function sortArray(db_array, sort_column) {
	temp_array = new Array(26);
	var temp= new Array();
	
	for(index=0; index<db_array.length; index++) {
		temp_array[index] = new Array(5);
	}
	
	switch (sort_column) {
		case 1:
			for(item_index=0; item_index<db_array.length; item_index++){
				temp_array[item_index] = mapArray(db_array[item_index], [0,1,2,3,4]);
			}       	                 
			temp_array.sort();
			for(item_index=0; item_index<temp_array.length; item_index++){
				db_array[item_index] = mapArray(temp_array[item_index], [0,1,2,3,4]);
			}    
			break;
		
		case 3: {
			for(item_index=0; item_index<db_array.length; item_index++){
				temp_array[item_index] = mapArray(db_array[item_index], [2,3,4,0,1]);
			}
			temp_array.sort();
			temp_array.reverse();
			for(item_index=0; item_index<temp_array.length; item_index++){
				db_array[item_index] = mapArray(temp_array[item_index], [3,4,0,1,2]);
			}    
			break;
		}
		
		case 4: 
			for(item_index=0; item_index<db_array.length; item_index++){
				temp_array[item_index] = mapArray(db_array[item_index], [3,4,0,1,2]);
			}       	                 
			temp_array.sort();
			for(item_index=0; item_index<temp_array.length; item_index++){
				db_array[item_index] = mapArray(temp_array[item_index], [2,3,4,0,1]);
			}    
			break;
	}
	return db_array;
}


function setPaths(mode) {		// Switch between movie or pps
	switch(mode) {
		case "pps" :
			downloadPath	= ppsDownloadPath;
			downloadExt 	= ppsDownloadExt;
			imagePath		= ppsImagePath;
			imageExt		= ppsImageExt;	
			break;
		
		case "movie":
			downloadPath	= movieDownloadPath;
			downloadExt 	= movieDownloadExt;
			imagePath		= movieImagePath;
			imageExt		= movieImageExt;	
			break;
	}
}

function setup_page(array, defaultTextColor, overTextColor, overTextPointer, imageWidthSmall, imageWidthMedium, display) {
	document.getElementById("td_sort_image").clicked = "no";
	document.getElementById("td_sort_image").imageWidth=imageWidthMedium;
	document.getElementById("div_main_sort").style.display=display;
	document.getElementById("div_main_table").style.display=display;

	if(display=="inline"){
		var element = document.getElementById("td_sort_name");
		element.onmouseover = function () 	{	this.style.color=overTextColor; this.style.cursor=overTextPointer; };
		element.onmouseout  = function () 	{	if(this.clicked != "yes") this.style.color=defaultTextColor; };
		element.onclick 	 = function () 	{	if(this.clicked != "yes") sortList("name", document.getElementById("dbase_Textbox").value, defaultTextColor, overTextColor); };	
		element = document.getElementById("td_sort_image");
		element.onmouseover = function () 	{	this.style.color=overTextColor; this.style.cursor=overTextPointer; };
		element.onmouseout  = function () 	{	this.style.color=defaultTextColor; };
		element.onclick 	 = function () 	{	if(this.clicked == "yes") {
													this.clicked = "no";
													this.imageWidth=imageWidthMedium;
													for(i=0; i<document.getElementById("main_table").rows.length; i++) {
														if(document.getElementById("img_td1_list_item_"+i)) document.getElementById("img_td1_list_item_"+i).style.width=imageWidthMedium;
													}
												} else {
													this.clicked = "yes";
													this.imageWidth=imageWidthSmall;
													for(i=0; i<document.getElementById("main_table").rows.length; i++) {
														if(document.getElementById("img_td1_list_item_"+i)) document.getElementById("img_td1_list_item_"+i).style.width=imageWidthSmall;
													}
												}
											};	
	
		element = document.getElementById("td_sort_date");
		element.onmouseover = function () 	{	this.style.color=overTextColor; this.style.cursor=overTextPointer;	};
		element.onmouseout  = function () 	{	if(this.clicked != "yes") this.style.color=defaultTextColor; };
		element.onclick 	 = function () {	if(this.clicked != "yes") sortList("date", document.getElementById("dbase_Textbox").value, defaultTextColor, overTextColor); };		
		element = document.getElementById("td_sort_music");
		element.onmouseover = function () 	{	this.style.color=overTextColor; this.style.cursor=overTextPointer; };
		element.onmouseout  = function () 	{	if(this.clicked != "yes") this.style.color=defaultTextColor; };
		element.onclick 	 = function () {	if(this.clicked != "yes") sortList("music", document.getElementById("dbase_Textbox").value, defaultTextColor, overTextColor); };		
	}
}

function reset_scrollbar(element){
	document.getElementById(element).scrollTop = 0;
}

function clearTable(element){
	// Delete existing rows
	for(var i = document.getElementById(element).rows.length; i > 0;i--)	{
		document.getElementById(element).deleteRow(i -1);
	}
}

// Make list with scrollbar
function generateList(db_array, filter, downloadPath, downloadExt, imagePath, imageExt, overTextPointer, onMouseOverlay, imageWidthSmall, imageWidthFull) {
	var list="";
	var tableIndex=0;
	var addRow=0;
	var listItems=0;

	// Delete existing rows
	clearTable("main_table");

	if(filter) {
		if(filter==default_textbox_text) filter=null;
	}

	// generate rows
	for(index=0; index<db_array.length+3; index++){
		
		addRow=0;
		
		if(!filter) {  // No filter active
			addRow=1;		// To generate some space at the bottom for last picture
			if(index<db_array.length) listItems++;
		} else {
			var filterRegexp = new RegExp(filter,"i");
			
			if(db_array[index]) { // Valid database, search
				if( db_array[index][0].search(filterRegexp) >= 0 ||
					db_array[index][1].search(filterRegexp) >= 0 ||
					db_array[index][2].search(filterRegexp) >= 0 ||
					db_array[index][3].search(filterRegexp) >= 0 ||
					db_array[index][4].search(filterRegexp) >= 0 ) {
					addRow=1;
					listItems++;
				} 
			} else {	// Filter active, add 3 extra rows.
				addRow=1;
			}
		}
		
		if( addRow == 1 ){
			var mainTable 	= document.getElementById("main_table");
			var newRow 		= mainTable.insertRow(tableIndex);
			var newCell0 	= newRow.insertCell(0);
			var newCell1 	= newRow.insertCell(1);
			var newCell2 	= newRow.insertCell(2);
			var newCell3 	= newRow.insertCell(3);
			var newCell4 	= newRow.insertCell(4);
			var newCell5 	= newRow.insertCell(5);
			
			if( index<db_array.length ) {
				newCell1.innerHTML=db_array[index][0];
				newCell2.innerHTML="<CENTER><img id='img_td1_list_item_"+tableIndex+"' src='"+imagePath+db_array[index][4]+imageExt+"' style='width:"+imageWidthSmall+"; border:white 1px solid;'></img></CENTER>";
				newCell3.innerHTML="<CENTER>"+db_array[index][1]+"</CENTER>";
				newCell4.innerHTML="<CENTER>"+db_array[index][2]+"</CENTER>";
				newCell5.innerHTML=db_array[index][3];
				newCell1.fileName=db_array[index][4];
			} else {
				newRow.style.height="30px";
			}
			
			newRow.className="list_row";
			
			newCell1.className="list_item";
			newCell2.className="list_item";
			newCell3.className="list_item";
			newCell4.className="list_item";
			newCell5.className="list_item";
			
			newCell0.style.width="1%";
			newCell1.style.width="34%";
			newCell2.style.width="19%";
			newCell3.style.width="8%";
			newCell4.style.width="8%";
			newCell5.style.width="30%";
			
			newRow.setAttribute("id","list_item_"+tableIndex);
			newCell1.setAttribute("id","td0_list_item_"+tableIndex);

			if(document.getElementById("td_sort_image").imageWidth &&  index<db_array.length ) {
			
				var imageId = document.getElementById("img_td1_list_item_"+tableIndex);
				
				imageId.style.width= document.getElementById("td_sort_image").imageWidth;
				
				imageId.onmouseover		= function ()	{	animations_preview_size(this, imageWidthFull, 2, 10); };
				imageId.onmouseout		= function ()	{	animations_preview_size(this, document.getElementById("td_sort_image").imageWidth, 2, 10); };
				newRow.onmouseover 		= function ()	{	if(this.clicked != "yes") this.style.background="url("+onmouseOverlay+")";
															this.style.cursor=overTextPointer;
														};
				newRow.onmouseout  		= function () 	{	if(this.clicked != "yes") this.style.background="none"; };
				newRow.onclick 	 		= function () 	{	var element = document.getElementById("td0_"+this.id);
															download_item(element.innerHTML,downloadPath+element.fileName+downloadExt);
														};		
			}

			if(index==db_array.length+1 && filter){		// If filtering active, add row which can be clicked to show all.
				newCell1.innerHTML="SHOW ALL";
				newCell1.className="list_item_showall";
				newRow.onmouseover 		= function ()	{	this.style.background="url("+onmouseOverlay+")"; this.style.cursor=overTextPointer; };
				newRow.onmouseout  		= function () 	{	this.style.background="none"; };
				newRow.onclick 	 		= function () 	{	sortList("name", null, defaultTextColor, overTextColor);
															reset_textbox(); reset_scrollbar("div_main_table");
														};	
			}
			tableIndex++;
		}
	}
	
	// Set number of items in list
	document.getElementById("div_list_items").innerHTML="Items in list: "+(listItems);
	
	if(listItems == 1) {
		document.getElementById("img_td1_list_item_0").style.width=imageWidthFull;
		document.getElementById("img_td1_list_item_0").onmouseover=null;
		document.getElementById("img_td1_list_item_0").onmouseout=null;
	}
}	

function sortList(mode, filter, defaultTextColor, overTextColor){

	clearTable("main_table");
	setup_text_new(document.getElementById("div_textbox_right"), 17, default_text_color, "inline", "- Loading database -", 470, 315, 138, 30);
	
	switch(mode) {
		case "name":
			document.getElementById("td_sort_name").style.color=overTextColor;
			document.getElementById("td_sort_date").style.color=defaultTextColor;
			document.getElementById("td_sort_music").style.color=defaultTextColor;
			document.getElementById("td_sort_name").clicked="yes";
			document.getElementById("td_sort_date").clicked="no";
			document.getElementById("td_sort_music").clicked="no";
			mode=1;
			break;
		case "date":
			document.getElementById("td_sort_name").style.color=defaultTextColor;
			document.getElementById("td_sort_date").style.color=overTextColor;
			document.getElementById("td_sort_music").style.color=defaultTextColor;
			document.getElementById("td_sort_name").clicked="no";
			document.getElementById("td_sort_date").clicked="yes";
			document.getElementById("td_sort_music").clicked="no";
			mode=3;
			break;
		case "music":
			document.getElementById("td_sort_name").style.color=defaultTextColor;
			document.getElementById("td_sort_date").style.color=defaultTextColor;
			document.getElementById("td_sort_music").style.color=overTextColor;
			document.getElementById("td_sort_name").clicked="no";
			document.getElementById("td_sort_date").clicked="no";
			document.getElementById("td_sort_music").clicked="yes";
			mode=4;
			break;
	}
	
	internalArray=sortArray(internalArray, mode);
	generateList(internalArray, filter, downloadPath, downloadExt, imagePath, imageExt, overTextPointer, onmouseOverlay,  imageWidthSmall, imageWidthFull);
	setup_text_new(document.getElementById("div_textbox_right"), 17, default_text_color, "none", null, null, null, null, null);
}

// Perform dbase conversion on startup (actual conversion is done on original arrays because of inernalArray is not an define array on its own. only pointer here.
internalArrayMovie=convertArray(moviename);
internalArrayPps=convertArray(ppsname);
