function OnLoad() {
	app = new MyPresent();
}

var clips = new Array();

/**
* MyPresent()
*
* This is the core application object for this sample app.
* It maintains the various containers, forms, and controls used
* to build the simulated wish list application.
*/
function MyPresent() {

	// get address of the core app sections
	this.searchContainer = document.getElementById("searchContainer");
	this.clipsContainer = document.getElementById("cadou");
	//clips container
	var clips = new Array();

	// bind in a search control with Amazon.com and Web Search
	this.searchControl = new GSearchControl();

	// query null
	this.query = new Array('','');
	var webSearch = new GwebSearch();

	this.searchControl.addSearcher(webSearch);

	// site restricted web search with custom label
	var siteSearch = new GwebSearch();
	siteSearch.setUserDefinedLabel("Okazii.ro");
	siteSearch.setSiteRestriction("okazii.ro");
	this.searchControl.addSearcher(siteSearch);

	var imageSearch = new GimageSearch();
	imageSearch.setUserDefinedLabel("Imagini");
	this.searchControl.addSearcher(imageSearch);


	//KEEP CALLBACK
	this.searchControl.setOnKeepCallback( this, MyPresent.prototype.onKeep, "ADAUGA la cadoul meu" );

	//DRAW A TABBED SEARCH
	var drawOptions = new GdrawOptions();
	drawOptions.setDrawMode(GSearchControl.DRAW_MODE_TABBED);
	this.searchControl.draw(this.searchContainer,drawOptions);
}

/**
* .switchToSearchControl()
*
* Called when a new item is added, or opened for edit.
* Its purpose is to toggle off the add new item form, and enable the
* search control, optionally executing a search in the process.
*/
MyPresent.prototype.switchToSearchControl = function(opt_query,elementId) {
	if ( opt_query ) {
		this.query[elementId] = opt_query;
		this.searchControl.execute(this.query[0]+' '+this.query[1]);
	}
	// cssSetClass(this.newItemForm, "wiw-new-item-form-closed");
	// cssSetClass(this.searchContainer, "wiw-new-item-form-closed");
}

/**
* .onKeep()
*
* Global on keep handler.
*/
MyPresent.prototype.onKeep = function(result) {
	var resultDiv = createDiv(null,"wiw-result");
	var node = result.html.cloneNode(true);
	var deleteMe = createDiv("sterge", "wiw-delete");
	deleteMe.onclick = method_closure(this, this.onDelete, [resultDiv]);

	resultDiv.appendChild(node);
	resultDiv.appendChild(deleteMe);
	this.clipsContainer.appendChild(resultDiv);
	clips.push(result);
}

/**
* .onDeleteResult()
*
* Soft delete the specified search result. Note, that in this function
* result is NOT that same as what we get from the on keep handler. Here,
* result is a dom node.
*/
MyPresent.prototype.onDelete = function(result) {
	this.deleted_ = true;
	app.visibleItemCount--;
	cssSetClass(this.root, "wiw-item wiw-item-deleted");
	if ( app.currentItem.closed || app.currentItem.deleted_ || app.currentItem.visibleItemCount == 0) {
		app.switchToNewItemForm();
	}
}


/**
* .submitForm()
* prepares and stringifyes data before posting
*/
MyPresent.prototype.submitForm = function (){
	// Remove the .html prop from the clips array before stringifying
	for (i=0; i < clips.length; i++){
		clips[i].html = "";
	}

	// Stringify and escape clips array
	var clipsJSONText =  JSON.stringify(clips);
	var escapedClips = escape(clipsJSONText);
	document.getElementById('escapedClips').value=escapedClips;
}

/**
* Various Static DOM Wrappers.
*/
function method_closure(object, method, opt_argArray) {
	return function() {
		return method.apply(object, opt_argArray);
	}
}

function removeChildren(parent) {
	while (parent.firstChild) {
		parent.removeChild(parent.firstChild);
	}
}

function cssSetClass(el, className) {
	el.className = className;
}

function createDiv(opt_text, opt_className) {
	var el = document.createElement("div");
	if (opt_text) {
		el.innerHTML = opt_text;
	}
	if (opt_className) { el.className = opt_className; }
	return el;
}

function prependNode(el, node) {
	if ( el.childNodes.length ) {
		el.insertBefore(node, el.childNodes[0]);
	} else {
		el.appendChild(node);
	}
}

function createForm(opt_className) {
	var el = document.createElement("form");
	if (opt_className) { el.className = opt_className; }
	return el;
}

function createTextInput(opt_className) {
	var el = document.createElement("input");
	el.setAttribute("autoComplete", "off"); // fixes firefox auto-complete bug
	el.type = "text";
	if (opt_className) { el.className = opt_className; }
	return el;
}

function createInput(name,opt_className,type,value) {
	var el = document.createElement("input");
	el.setAttribute("autoComplete", "off"); // fixes firefox auto-complete bug
	el.setAttribute("name",name);
	el.setAttribute("value",value);
	el.type = type;
	if (opt_className) { el.className = opt_className; }
	return el;
}

function createTable(opt_className) {
	var el = document.createElement("table");
	if (opt_className) { el.className = opt_className; }
	return el;
}

function createTableRow(table) {
	var tr = table.insertRow(-1);
	return tr;
}

function createTableCell(tr, opt_className) {
	var td = tr.insertCell(-1);
	if (opt_className) { td.className = opt_className; }
	return td;
}
