/**
 * GLOBAL vars for fonts
 * IMPORTANT :: ENCODE THIS FILE IN UTF-8!!!
 */
//old:: http://212.1.36.82 - new :: http://fonts.fontshop.de

// changed read protcol on line
var globalFontRenderingURL = location.protocol + "//fonts.fontshop.de/fonts.php";

var allFontImagesOnList = new Array();
var globalHostVar = '/';

var text2RenderGlobal = "Die heiße Sonne quält Max auf dem Küstenweg";

var hiddenInputRenderText = document.getElementById("renderTextDefault");
if (hiddenInputRenderText!=null) {
	text2RenderGlobal = hiddenInputRenderText.value;
}


//function to update the server vars
function updateFontsServerVars(newText, newSize){
	
	var escapedText = newText;
	
	//since users COULD use # as a render sign, use a specific combination....
	var transmitParams = newSize + "#spec8472estaBL8--" + escapedText;
	
	try {
		try {
			FontshopAjaxUtilities.setFontsRenderSettings(
			  transmitParams, {				//params
			  callback: handleDefaultCallback,	//callback
			  errorHandler: handleErrors,
			  async:false
			});
		} catch (x) {
			(x);
		}
	} catch (exception) {
		(exception);
	}
	
}

//initial page load: load font cuts
function initialLoadImage(size, personalText, sourceDirectory, fontImageUrlFilename, currentImageID, save4DefaultImageBoolean, fontPK, updateIcons, letterAmount) {
	var waitForVar = null;
	waitForVar = addToRefreshablesArray(currentImageID);	
	
	if (personalText == "") { //normally empty - so get it from saved vars
		var persTextInput = document.getElementById("fontListingMyText");
		if (persTextInput!=null) {
			personalText = persTextInput.value; 
		} else {
			personalText = text2RenderGlobal; 
		}
	}
	if (waitForVar!=null) {
		reloadFontImage(currentImageID, size, personalText, sourceDirectory, fontImageUrlFilename, save4DefaultImageBoolean, updateIcons, letterAmount);
	}
}

function setFontImageURL(imgSrcTagID, size, personalText, sourceDirectory, fontImageUrlFilename, renderSmallIcon, letterAmount) 
{
	var src= getFontImageURL(size, personalText, sourceDirectory, fontImageUrlFilename, false);

	document.getElementById(imgSrcTagID).src = src; 

}

//actual reload call
function reloadFontImage(currentImageID, size, personalText, sourceDirectory, fontImageUrlFilename, save4DefaultImageBoolean, updateIcons, letterAmount) {
 var downloadStart = new Date().getTime();
 var ourCurrentImage = document.getElementById(currentImageID);

// if(!$D.hasClass(ourCurrentImage,"thumb")) {
// 	size = 's';
// }
 	var newImageSrc = getFontImageURL(size, personalText, sourceDirectory, fontImageUrlFilename, false) + "&" + downloadStart;
 	
 	 if (ourCurrentImage!=null && newImageSrc != null) {
		try { 	 	
 	 		ourCurrentImage.src = newImageSrc;
			
 	 		if (save4DefaultImageBoolean == true) {
 	 			//call function in ajaxRemoteRequests.js
 	 			//var size4Rendering = "wk";
 	 			var size4Rendering = "s";
 	 			var smallFontImageSrc = getFontImageURL(size4Rendering, personalText, sourceDirectory, fontImageUrlFilename, true, letterAmount) + "&" + downloadStart;
 	 			if (smallFontImageSrc!=null && smallFontImageSrc!="") {
	 	 			//save4DefaultImageBooleanCall(smallFontImageSrc, fontPK);
	 	 			//=> not needed anymore, urls set via rel
	 	 			//if (updateIcons == true) {
	 	 				//updateNotePadIcons(fontPK, smallFontImageSrc);
	 	 			//}
	 	 		}
 	 		}
		} catch (g) {
			//alert(g);
		}
 	 }
 	
}

//function to put all image ids on this page into an array
function addToRefreshablesArray(fontAtomID) {
	//all rendered images have a certain fmdbid of this list!!!
	if (allFontImagesOnList!=null && fontAtomID != null) {
		allFontImagesOnList.push(fontAtomID);
	}
	
	return true;
}

//function to refresh all images on this page
function refreshAllImagesOnList(newSize, isSizeCall) {
	var newText = document.getElementById("fontListingMyText").value;	
	
	var loopCount = allFontImagesOnList.length;
	
	updateFontsServerVars(newText, newSize);
	updateSizeInput(newSize);
	
	if (newText == "") {
		newText = text2RenderGlobal;
		if (isSizeCall == "true") {
			document.getElementById("fontListingMyText").value = newText;
		}
	}
	
	
	for(var i=0; i < loopCount; i++) {
		
		var currentImageID = allFontImagesOnList[i];
		var inputFieldID = currentImageID + "input";
		
		var currSourceDir = document.getElementById(inputFieldID).name;
		var currFileName = document.getElementById(inputFieldID).value;
		
		reloadFontImage(currentImageID, newSize, newText, currSourceDir, currFileName);
	}
}

/**
 * Doppeltes Refresh bei zu langsamen Server ?
 */
function reloadEmptySrcDirImages(newSize, isSizeCall) {
	if(document.getElementById("fontListingMyText")){
		var newText = document.getElementById("fontListingMyText").value;	
		var loopCount = allFontImagesOnList.length;
		for(var i=0; i < loopCount; i++) {
			try {
				//surround with try/catch because fontlibraries do not have these...
				var currentImageID = allFontImagesOnList[i];
				var inputFieldID = currentImageID + "input";
				
				var currSourceDir = document.getElementById(inputFieldID).name;
				var currSrc = document.getElementById(inputFieldID).src;
				var currFileName = document.getElementById(inputFieldID).value;
				
				if (null == currSrc || currSrc == "") {
					reloadFontImage(currentImageID, newSize, newText, currSourceDir, currFileName);
				}
			} catch (u) {
				//
			}
		}
	}
}

function reloadTwice4SlowBrowsers(newSize, isSizeCall) {	setTimeout(function () {reloadEmptySrcDirImages(newSize, isSizeCall);}, 2000);
}



//function to update the render size input
//and change the icons class
function updateSizeInput(newSize) {
	if (newSize != null) {
		document.getElementById("fontsRenderSize").value = newSize;
	}
	updateIconBorders(newSize);
}

function updateIconBorders(newSize) {
	
	
	var iconS = document.getElementById("fontsRenderIcon_s");
	var iconM = document.getElementById("fontsRenderIcon_m");
	var iconL = document.getElementById("fontsRenderIcon_l");
	var addClassFinal = "fontsResizeInactive";
	var addClassInactive = "fontsResizeInactive";
	var addClassActive = "fontsResizeAktiv";
	
	if(iconS!=null) {
		if (newSize == "s") {
			addClassFinal = addClassActive;
		} else {
			addClassFinal = addClassInactive;
		}
		iconS.setAttribute("class", "fontviewbutton " + addClassFinal);
	}
	if(iconM!=null) {
		if (newSize == "m") {
			addClassFinal = addClassActive;
		} else {
			addClassFinal = addClassInactive;
		}
		iconM.setAttribute("class", "fontviewbutton " + addClassFinal);
	}
	
	if(iconL!=null) {
		if (newSize == "l") {
			addClassFinal = addClassActive;
		} else {
			addClassFinal = addClassInactive;
		}
		iconL.setAttribute("class", "fontviewbutton " + addClassFinal);
	}
	
	
	
}

/**
 * Function to retrieve retrieve (and thereby render) the font image url
 */
function getFontImageURL(size, personalText, sourceDirectory, fontImageUrlFilename, renderSmallIcon, letterAmount) {
	var textToRender = retrieveAndEscapeText(personalText)
	
	var newFontImageUrl = globalFontRenderingURL;
	var checkLetterAmount = letterAmount;
	
	if (size==null || size =="") {
		size = "s";
	}
	if (textToRender==null || textToRender =="") {
		textToRender = text2RenderGlobal;
	}
	if (renderSmallIcon==true) {
		textToRender = "Ag";
		if (document.getElementById("renderTextNotePad")!=null) {
			textToRender = document.getElementById("renderTextNotePad").value;
		}
	}
	if (textToRender.length > checkLetterAmount && size == "l") {
		textToRender = textToRender.substring(0, checkLetterAmount) + "...";
	}
	var appendToUrl = "?size="+size+"&path="+sourceDirectory+"&font="+fontImageUrlFilename+"&text="+textToRender;
	
	if ((sourceDirectory != null && sourceDirectory!= "") && (fontImageUrlFilename != null && fontImageUrlFilename != ""))  {
		newFontImageUrl = newFontImageUrl + appendToUrl;
	} else {
		newFontImageUrl = "/img/btbg/empty.gif";
	}
	return replaceCertainChars(newFontImageUrl);
}

//function for font rendering via special folder names... in order to find the thing, replace chars
function replaceCertainChars(fontUrl) {
	if (fontUrl.contains("+")) {
		fontUrl = fontUrl.replace("+", "%2B");
	}
	return fontUrl;
}



var updateFontProcess = null;

//function to submit a field on a event (fired i.e. by keyup)
function submitenterfontrendering(forminputfield, e, fontsRenderSize, searchTerm, isSizeCall) {
	//http://www.htmlcodetutorial.com/forms/index_famsupp_157.html
	
	var selRenderSize = document.getElementById("fontsRenderSize").value;

	var keycode;
	if (window.event) keycode = window.event.keyCode;
		else if (e) keycode = e.which;
		else return true;

	if (keycode != null || isSizeCall!=null){ //just check for any keypress, because we want to submit on each key
	   if (fontsRenderSize) {
	   		// update images delayed, clear recent update
	   		refreshAllImagesOnList(selRenderSize, isSizeCall);
	   }
	  return false;
	}
	else
	   return true;
}


function retrieveAndEscapeText(personalText) {
	var text = personalText;
	if (text == null) {
		text = document.getElementById("fontListingMyText").value;
	}
	
	var textToRender = encodeURIComponent(text);
	if (textToRender.contains("%u20AC")){
		textToRender = textToRender.replace("%u20AC", "€");
	}
	if (textToRender.contains("\\")) {
		textToRender = textToRender.replace("\\", escape("&#92;"));
	}
	//IMPORTANT :: no save possible so just replace
	if (textToRender.contains("\'")) {
		textToRender = textToRender.replace("\'", "");
	}
	if (textToRender.contains("\"")) {
		textToRender = textToRender.replace("\"", "");
	}
	textToRender = textToRender.replace("%20", " "); //let the browser do that - will be interpreted correctly and WE do NOT want to save this...
	
	return textToRender;
}



