/* Demo created for ECI BV. QuestField copyright 2008 MasterObjects, Inc. */

var eciQuestFieldId = "EciDemoQuestField";
var eciMetaAvailable = false;
var showingEciPopUp = false;
var demoImages = ['http://www.bps4m.nl/images/ean/big/8714253009803.jpg','http://www.bps4m.nl/images/ean/big/5099951986827.jpg','http://www.bps4m.nl/images/ean/big/0802987011723.jpg','http://www.bps4m.nl/images/ean/big/5099923499621.jpg','http://www.bps4m.nl/images/ean/big/0602517804715.jpg'];

function insertEciQuestField() {
	var o = {};
	o.id				= eciQuestFieldId;
	o.license			= "0000001";
	o.defaultDomain		= "questobjects.com";
	o.questletVersion	= 201;
	o.technology		= "auto";
	o.bgColor			= "#f08000";
	o.footerColor		= "#faf7e8";
	o.skinName			= "qo-generic-red";
	o.connectString		= "/questobjects-temporary/server";
	o.contentChannel	= "freedb-demo";
	o.listLayout		= "SimpleScroll";
	o.language			= "nl";
	o.autoSelect		= "match";
	o.showResultArrows	= false;
	o.hoverSelect		= true;
	o.autoComplete		= "off";
	o.questFieldWidth	= 139;
	o.questFieldHeight	= 20;
	o.popUpWidth		= 300;
	o.popUpToLeft		= true;
	o.valueInput		= "fh_search";
	o.popUpHandler 		= "eciPopUpHandler";
	o.metadataHandler	= "eciMusicHandler";
	o.errorHandler		= "demoErrorHandler";
	o.footerHeight		= 74;
	o.footerHTML		= '<div style="padding:4px"><p style="color:#d01">Dutch-language demo for ECI BV by <a href="http://www.masterobjects.com">MasterObjects, Inc.</a>. This demo searches the FreeDB database containing over 20 million songs and over 2 million CD and artist names hosted on the MasterObjects development server in Maarssen.</p></div>';
	o.popUpRows			= 20;
	o.queryLength		= 2;
	o.keepLatin			= true;
	o.placeholder		= "Muziekdemo...";
	if (QO.allowField(o)) {
		document.write('<div style="float:left;width:139px;position:relative;"><input type="hidden" name="fh_search" id="fh_search" />');
		QO.field.insert(o);
		document.write('<\/div>');
		QO.field.addContainer(o.id,"eciDrawer");
	} else {
		document.write('<div class="bgsearchinput"><input type="text" value="Vul je zoekterm in..." name="fh_search" id="fh_search" onfocus="this.value=\'\'" style="color:#949493;border:0px;width:125px;" /><\/div>');								
		alert('DEMO MESSAGE' + MO.N + MO.N + 'This page is not showing the demo QuestField, because the QuestField demo was not tested with your web browser configuration.');
	}
}

function eciHighlight(theInput,theString,color,prefix,suffix) {
	prefix = prefix + '<font color="' + color + '">';
	suffix = '</font>' + suffix;
	var theInputValues = theInput.split(" ");
	for (var i = 0; i<theInputValues.length; i++) {
		var theWord = theInputValues[i];
		var theStopWords = ",for,to,the,at,and,of,in,y,&,en,de,het,een,op,a,li,an";
		if (theWord.length > 2 && (theStopWords.indexOf(',' + theWord.toLowerCase()) == -1)) {
			theString = MO.util.replaceAll(theString,theWord,prefix + theWord + suffix);
			theWord = theWord.substr(0,1).toUpperCase() + theWord.substr(1);
			theString = MO.util.replaceAll(theString,theWord,prefix + theWord + suffix);
			theWord = theWord.toUpperCase();
			theString = MO.util.replaceAll(theString,theWord,prefix + theWord + suffix);
			theWord = theWord.toLowerCase();
			theString = MO.util.replaceAll(theString,theWord,prefix + theWord + suffix);
			theWord = theWord.substr(0,1).toUpperCase() + theWord.substr(1);
			theString = MO.util.replaceAll(theString,theWord,prefix + theWord + suffix);
		}
	}
	return theString;
}

function eciMusicHandler(fieldName, m) {
	m = m || {};
	var theGenre = m["meta0"], theTracks = m["meta1"];
	function splitFreedbTitle(theValue) {
		var theTitle = theValue;
		var thePos = theValue.indexOf(" /");
		if (thePos > -1) {
			theTitle = theValue.substr(0,thePos) + "<br />";
			theValue = theValue.substr(thePos + 3);
			thePos = theValue.lastIndexOf(", ");
			if (thePos > -1) {
				theTitle = theValue.substr(thePos + 1) + " " + theTitle;
				theValue = theValue.substr(0, thePos);
			} else {
				thePos = theTitle.lastIndexOf(", ");
				if (thePos > -1) {
					var temp = theValue;
					theValue = theTitle.substr(thePos + 1);
					theTitle = temp + " " + theTitle.substr(0, thePos) + "<br />";
				}
			}
			theTitle = theTitle + theValue;
		}
		return theTitle.toUpperCase();
	}

	if (theGenre || theTracks) {
		eciMetaAvailable = true;
		var theInput = QO.field.getValue(eciQuestFieldId,true);
		MO.$('metaTitle').innerHTML = 
			eciHighlight(theInput,splitFreedbTitle(QO.field.getValue(eciQuestFieldId)),'#dd0011','','');
		MO.$('metaGenre').innerHTML = theGenre ? 
			(" (" + unescape(theGenre)) + ")" : "";
		MO.$('metaTracks').innerHTML = theTracks ? 
			("<b>Tracks:</b><br><ol>"+ eciHighlight(theInput,MO.util.replaceAll(unescape(theTracks),"&apos;","'"),'#dd0011','<b>','</b>') + "</ol>") : "";
		MO.$('metaImg').src = demoImages[QO.field.getIndex(eciQuestFieldId) % 5];
	} else {
		eciMetaAvailable = false;
	}
	showEciMeta();
}

function eciPopUpHandler(fieldId, willOpen, coordinates, pane) {
	showingEciPopUp = (pane != "close");
	showEciMeta();	
}

function showEciMeta() {
	var toShow = (eciMetaAvailable && showingEciPopUp);
	var theDisplay = toShow ? "block" : "none";
	MO.$("flashvisual").style.visibility = toShow ? "hidden" : "visible";
	MO.$("eciDrawer").style.visibility = toShow ? "visible" : "hidden";
}


function demoErrorHandler(id,number,text,recoverable) {
	var errorText =
		"Oops! It seems that an unexpected error occurred in this demo page." + MO.N + MO.N +
		"Either the DSL connection to the demo server is down, or we are updating the server. In either case, thank you for calling us so that we can rectify the situation!" + MO.N + MO.N + "U.S./Canada: +1-800-518-6001 (toll-free)" + MO.N + "Rest of world: +31-346-285-144";
	if (recoverable) {
		errorText += MO.N + MO.N + '(You can try your query again by clicking on the "triangle" on the right side of the QuestField.)';
	}
	alert(errorText);
}
