function OnLoad() {
	new RawSearchControl();
}

function RawSearchControl() {

	// latch on to key portions of the document
	this.searcherform = $('searcher');
	this.results = $('results');
	this.searchform = $('searchform');
	this.noresultsfound = 0;
	this.searcherscount = 0;
	// create map of searchers as well as note the active searcher
	this.activeSearcher = "web";
	this.searchers = new Array();
	GSearch.getBranding($('branding'));
	var s = document.location.search;
	var match, query = '', type = '';
	match = s.match(/query=([^&]*)/);
	if(match)
		query = match[1];
	query = query.replace('+', ' ');
	match = s.match(/type=([^&]*)/);
	if(match)
		type = match[1];
	query = unescape(query);
	var searchboxquery = $('txtsearch');
	searchboxquery.value = query;
	var cseId = '005823141800884017761:acq_9vpjcko';

	// Articles
	if (type == '' || type == 'article') {
		var searcher = new GwebSearch();
		searcher.setNoHtmlGeneration();
		searcher.setSiteRestriction("telecoms.premiercercle.com");
		searcher.setSearchCompleteCallback(this, RawSearchControl.prototype.searchComplete,[searcher]);
		if (type == '')
			searcher.setResultSetSize(GSearch.SMALL_RESULTSET);
		else
			searcher.setResultSetSize(GSearch.LARGE_RESULTSET);
		this.searchers["article"] = searcher;
		searcher.execute(query + " site:telecoms.premiercercle.com/article");
	}

	// Videos
	if (type == '' || type == 'video') {
		var searcher = new GwebSearch();
		searcher.setNoHtmlGeneration();
		searcher.setSiteRestriction("telecoms.premiercercle.com");
		searcher.setSearchCompleteCallback(this, RawSearchControl.prototype.searchComplete,[searcher]);
		if (type == '')
			searcher.setResultSetSize(GSearch.SMALL_RESULTSET);
		else
			searcher.setResultSetSize(GSearch.LARGE_RESULTSET);
		this.searchers["video"] = searcher;
		this.searcherscount++;
		searcher.execute(query + " site:telecoms.premiercercle.com/video");
	}

}

// Search callback
RawSearchControl.prototype.searchComplete = function(searcher) {

	// always clear old from the page
	//this.clearResults();
	// if the searcher has results then process them
	if (searcher.results && searcher.results.length > 0) {
		// print the result titles
		var div, section, moretype, pretype;
		$("resultsloading").hide();
		if (this.searchers["article"] == searcher) {
			section = "Articles";
			moretype = "article";
			pretype = "d'";
		}
		else {
			section = "Videos";
			moretype = "video";
			pretype = "de ";
		}
		div = createDiv(section, 'searchheader');
		this.results.appendChild(div);
		var exactMatch, query = '', type = '';
		var s = document.location.search;
		var match = s.match(/query=([^&]*)/);
		if(match)
			query = match[1];
		var match = s.match(/type=([^&]*)/);
		if(match)
			type = match[1];
		for (var i=0; i<searcher.results.length; i++) {
			var result = searcher.results[i];
			var titleLine = result.title;
			if (!exactMatch)
				exactMatch = titleLine.match(/^<b>.*<\/b>$/);
			div = createDiv('<p class="title"><a href="' + result.unescapedUrl + '">' + titleLine + '</a></p><p class="content">' + result.content.replace('<br>','') + '<span class="readmore"><a href="' + result.unescapedUrl + '">Lire la suite</a></span></p>');
			this.results.appendChild(div);
		}
		if (type == '') {
			div = createDiv('<p class="more"><a href="?item=search&query=' + query + '&type=' + moretype + '">Plus ' + pretype + section + '</a></p>');
			this.results.appendChild(div);
		} else {
			div = createDiv('<p class="more"><a href="?item=search&query=' + query + '&type=">Revenir aux résultats</a></p>');
			this.results.appendChild(div);
		}
	}
	else {
		this.noresultsfound++;
		// None of the searchers found results
		if (this.noresultsfound > this.searcherscount) {
			$("resultsloading").hide();
			div = createDiv('<p style="clear:both;text-align:center;margin-top:40px;text-transform:uppercase;">Aucun résultat trouvé</p>');
			this.results.appendChild(div);
		}
	}

}

function createDiv(opt_text, opt_classname) {
	var el = document.createElement("div");
	if (opt_text) {
		el.update(opt_text);
	}
	if (opt_classname) {
		el.addClassName(opt_classname);
	}
	return el;
}

// register to be called at OnLoad when the page loads
GSearch.setOnLoadCallback(OnLoad);
