



function show_ViewerAdminTools(){
	$('#adminTools').show(200);
}
function hide_ViewerAdminTools(){
	$('#adminTools').hide(200);
}

function registerAdminTooltips(){


	$('#toggleTooltip').toggle(function(){
		$(this).addClass('tooltipOn');
		window.tooltips = true;
		$(this).text('Turn Demo Tool Tips off')
	}, function(){
		$(this).removeClass('tooltipOn');
		$(this).text('Turn Demo Tool Tips on')
		$('.tooltip').remove();
		window.tooltips = false;
	});
	
	
	$('#gotoAdminPage').click(function(){
		location.href = 'admin.html'
	})
	
	tooltip('#color1, #color2', "Change pic background")
	tooltip('.epName, .subtitle, .title', "Content editable in config.js file")
	tooltip('#logoutLink', "You are logged on as admin. Click to log off")
	tooltip('#toggleTooltip', "Show/Hide tooltop. Tooltips for demo purposes only. Hopefully later iterations of app will be self-explanatory")
	tooltip('#gotosite', "Go to live site")
	tooltip('#gotoAdminPage', "Go to admin page for more editing control")
	tooltip('.lineItem', "When logged in as administrator you can click to edit contents.")
	tooltip('.smlInput', "Hit RETURN or click away from this area to save new value.")
	tooltip('.thumb', "Drag these thumbnails to reorder.")
	tooltip('.image img, .zoom', "Click to see originally uploaded file.")
	tooltip('#paintings, #paintingActive', "This entire area is a dropzone for upload (firefox only). Drag JPEG files from your computer or desktop or folder onto the page and wait for page to refresh. You should see the new files. This is currently buggy. Sometimes files don't upload, so try dragging small files, or a few at a time.")
	
	tooltip('#backup', "Save a copy of current JSON data to backup file")
	tooltip('#restoreNow', "Restore saved data with backup data (in case of corruption).")
	tooltip('#export', "Exports to XLS")
	tooltip('#export2Json', "Exports to json format.")
	tooltip('#generateIndexImage', "Generates the preview image on the homepage (For SEO purposes, this image should not be ajax based, but should appear on page for search bots) from the first image in the sort window.")
	tooltip('#normalize', "If some files have more metadata fields than others, this will add to those missing fields. This function also sorts fields to the same order.")
	tooltip('#deleteAll', "Delete all files from server.")
	tooltip('#expand', "Expand/unexpand sorting window.")
	
	tooltip('.sortthis img', "DRAG to sort.<br />DOUBLE CLICK to see original file.<br />CLICK to select, and edit metadata to the right. <br />Use ARROWS to change selection (after selecting).<br /> Hit RETURN to save data.<br /> <br /> Note. If blue highlight is on, then sorting is disabled. Click outside sorting area to exit editing mode and enable sorting again. Or hit RETURN<br /> <br /> Note: This entire area is a dropzone for upload (firefox only). Drag JPEG files from your computer or desktop or folder onto the page and wait for page to refresh. You should see the new files. This is currently buggy. Sometimes files don't upload, so try dragging small files, or a few at a time.")
	
	tooltip('.imgDelete', "Delete this image from server.")
	
	tooltip('.imgZoom', "CLICK to see original file.")
	tooltip('.lineitemSortable', "Click value to the right to enter edit mode. Then you can DRAG line item to change the order, then confirm below (Apply ordering to... etc).")
	tooltip('.data', "Enter new data, hit return to save")
	tooltip('.removeProp', "Remove this metadata field. ALT-CLICK to remove field from all records. <BR />NOTE: This is buggy. You have to try twice. The 2nd click it will work")
	
	tooltip('.nfLabel, .nfVal, #newFieldNameInput, #newFieldValueInput', "Create new field. Hit RETURN to add or use buttons to right. Note: fieldname should not use spaces or weird characters for now.")
	tooltip('#addFieldButtonAll', "Add Field to all records")
	tooltip('#addFieldButton', "Add Field to single record")
	
	
	tooltip('#applyOrder_Single, #applyOrder_Single_label', "After reordering, click button to apply order to just this record.")
	
	tooltip('#applyOrder_All, #applyOrder_All_label', "After reordering, click button to apply order to all records.")
}






function tooltip (target, message) {
	$(target).live("mouseenter", function(e){
		if (window.tooltips == true) {
			$('.tooltip').remove();
			var tooltipID = $(this).attr('id') + "_tooltip";
			var jqttid = "#"+ tooltipID;
			var tooltip = '<div class="tooltip" id="'+tooltipID+'"></div>';
			$(this).data('tooltipID', tooltipID);
			$('body').append(tooltip)
			$(jqttid).html(message)
			.css({
				"opacity":".85",
				"position":"absolute"
			});
			var thiswidth = $(jqttid).width();
			if (thiswidth > 250) {
				$(jqttid).css("width","250px")
			}	
			
			
			
			
			
			var offsetx = $(jqttid).width();
			var offsety = 28 + $(jqttid).outerHeight(true);
			mousex =  e.pageX;
			mousey = e.pageY
			if ((mousey-offsety) < 20) {
				offsety = -20
			}
			if (mousex < offsetx) {
				mousex += mousex
			}
			$(jqttid).css({
				"top":mousey-offsety,
				"left":mousex-(offsetx/2)
			});
		}
	});
	
	
	
	
	$(target).live("mousemove", function(e){
		if (window.tooltips == true) {
			var tooltipID = $(this).data('tooltipID');
			var jqttid = "#"+ tooltipID;
			var offsetx = $(jqttid).width();
			var offsety = 28 + $(jqttid).outerHeight(true);
			mousex =  e.pageX;
			mousey = e.pageY
			if ((mousey-offsety) < 20) {
				offsety = -20
			}
			if (mousex < offsetx) {
				mousex += offsetx/2
			}
			$(jqttid).css({
				"top":mousey-offsety,
				"left":mousex-(offsetx/2)
			});
		}	
	});
	
	$(target).live("mouseleave", function(){
		if (window.tooltips == true) {
			var tooltipID = $(this).data('tooltipID');
			var jqttid = "#"+ tooltipID;
			$(jqttid).remove();
		}
	});
}


// ---------------------------------------------------------
// LOAD PIC DATA (MOVE THIS TO FILE COMMON)
// ---------------------------------------------------------

function getFilelist(dir) {
	var path = "../"+dir;
	if (window.usermode != "admin") {  // don't check the file system. Go straight to the file
		$.ajax({
		type: "GET",
		url: 'php/readIndex.php',
		data: {"directory":"../"+dir},
		dataType: "json",
		success: function(data){
			window.fileList = data.filelist;
			if (! $.isPlainObject(data) || typeof data.filelist == "undefined" || !data.filelist.length || ! $.isArray(data.filelist) ) {
				alert("Problem with indexfile. Please try again later.");
				return;
			}
			fileListComplete();
			}});
	
	} else {
		$.getJSON('php/json_ImageList.php',
			{"directory":path},function(data){
				
				window.fileList =[];
				var metadata = data;
				
				$.each(metadata, function(i, val){
					val.dir = dir;
					var fileObject =  new PicData(val);
					
					//$.each(window.config.defaultFields, function(fi, fval){
					//		fileObject.addProp(fi, fval);
					//});
						
					fileObject.addProp("dir", dir);
					fileObject.addProp("id", stripIllegal(fileObject.filename));
					window.fileList.push(fileObject);
					if (i == metadata.length-1) {
						getIndexFile (window.fileList);
					}
				});
		});
	}
}

function fileListComplete() { // called from a timeout. Don't want the code on the metadata page.
	if (!window.usermode || window.usermode != "admin") {
		
		if (window.hasuploaded == 1) {
			debug('nothing!')
			//location.href = 'paintings.html'
		} else {
			window.hasuploaded = 0;
			setupPages(14)

		}
		
	} else {
		admin_generateAll('#container', window.fileList, 150, 100)
		
	}
}
// ---------------------------------------------------------
// CONSOLE LOG
// ---------------------------------------------------------


debug = function (log_txt) {
    if (window.console != undefined) {
        console.log(log_txt);
    }
}







// ---------------------------------------------------------
// PHP GD GENERATE PIC URL
// ---------------------------------------------------------


function toSrcString(dir, filename, title, maxwidth, maxheight, quality) {
	var script = 'php/getThumb.php?';
	var path = 'imagePath=../'+escape(dir)+'/'+escape(filename);
	var srcTitle = "&title=" + escape(title);
	var maxwidth = "&maxwidth=" + maxwidth;
	var maxheight = "&maxheight=" + maxheight;
	var quality = "&quality=" + quality;
	var src = script+path+srcTitle+maxwidth+maxheight+quality;
	return src;
}