//vars
var req;
var Paginator;
var queryStr;

//consts
var txtDownload				= 'T&eacute;l&eacute;charger';
var txtOnline				= 'Jouer en ligne';
var txtBuy					= 'Acheter';

var txtPrevPg				= 'Pr&eacute;c&eacute;dente';
var txtNextPg 				= 'Suivante';
var txtPage					= 'Page&nbsp;:';

var highlightingSettings	= 'color: #FF0000; font-family:arial; font-size: 12px; font-weight: bold;';


//-----------------
 // highlighting
//-----------------
function highLight(text, cssclass, terms)
{
	if (terms==null)
		return text;
		
	var key = "";
	for (keyno=0; keyno<terms.length; keyno++){
		if( key.length > 0 ) key += '|';
			key += terms[keyno];		
	}
	
	key = key.replace(/e/gi, '(e|\u00E8|\u00E9|\u00EA|\u00EB)');
	key = key.replace(/a/gi, '(a|\u00E0|\u00E1|\u00E2|\u00E3|\u00E4|\u00E5)');
	key = key.replace(/i/gi, '(i|\u00EC|\u00ED|\u00EE|\u00EF)');
	key = key.replace(/o/gi, '(o|\u00F2|\u00F3|\u00F4|\u00F5|\u00F6)');
	key = key.replace(/u/gi, '(u|\u00F9|\u00FA|\u00FB|\u00FC)');
	key = key.replace(/y/gi, '(y|\u00FF)');
	key = key.replace(/c/gi, '(c|\u00E7)');

	key = '(' + key + ')';
	
	var s = text.replace(createRegExp(key), '<span style="'+highlightingSettings+ '">$1</span>'); 
	return s;
}
		
function createRegExp(key){	
	key = key.replace("*","\\S*");
	key = key.replace("?","\\S{0,1}");
	return new RegExp(key, 'gi');
}

//-----------------
// drawing functions		
//-----------------

// draw spacer 
function drawSpacer()
{
	var s = '<table width="100%" border="0" cellpadding="0" cellspacing="0">\
				<tr>\
					<td height="11">\
						<img src="' + graphPath + 'spacer.gif" alt="" width="1" height="1" border="0"></td></tr><tr><td class="game1Divider1"><img src="' + graphPath + 'spacer.gif" alt="" width="1" height="1" border="0"></td></tr><tr><td height="3"><img src="' + graphPath+ 'spacer.gif" alt="" width="1" height="1" border="0">\
					</td>\
				</tr>\
			</table>';
	
	return s;
}
// graw game title			
function drawGameItemTitle(obj)
{
	var SupportsHighScore = obj.isEventSupporting ?  'block' : 'none';
	var HasOnlineVersion	= obj.lobbyURL.length>0 && obj.isOnline=='true' ?  'block' : 'none';
	var hasTokens	= obj.isSupportingTokens && obj.isOnline=='true' ?  'block' : 'none';
	var NumberOfPlayers = ' ';
	
	if (obj.HasOnlineVersion && obj.isOnline=='true')
	{
		try {
			NumberOfPlayers = PresenceCatalog.Lobbies.All.BySku[obj.sku].NumberOfPlayers;
		}
		catch(e)
		{}
	 }
	
	var s = '<td class="padding7">\
					<table width="100%" border="0" cellpadding="0" cellspacing="0">\
						<tr>\
							<td class="game1HeaderPlace3">\
								<table width="100%" border="0" cellpadding="0" cellspacing="0">\
									<tr>\
										<td width="100%">\
											<table border="0" cellpadding="0" cellspacing="0">\
												<tr>\
													<td class="game1Padding1"><img src="' + graphPath+ 'general/bullet2.gif" alt="" width="5" height="6" border="0"></td>\
													<td nowrap><a href=' + obj.gamePageURL + ' class="game1Title">' +highLight(obj.name, "searchHighlight", Paginator._terms)+ '</a></td>\
												</tr>\
											</table>\
										</td>\
										<td class="game1Text1" align="right" nowrap>\
											<table border="0" cellpadding="0" cellspacing="0" style="display:' + HasOnlineVersion+ '">\
											<tr>\
												<td style="display:' + SupportsHighScore+ '">\
													<table border="0" cellpadding=0 cellspacing=0>\
													<tr>\
															<td valign=bottom style="display:' + hasTokens+ '" >\
																	<img src="' + graphPath+ 'ppGeneral/koris2.gif" alt="" border="0">\
															</td>\
															<td style="display:' + hasTokens+ '"><img src="' + graphPath+ 'spacer.gif" alt="" width="2" height="1" border="0"></td>\
															<td><img src="' + graphPath+ 'general/medal.gif" alt="" width="16" height="21" border="0"></td>\
															<td><img src="' + graphPath+ 'spacer.gif" alt="" width="2" height="1" border="0"></td>\
															<td nowrap>Jeux avec classement</td>\
															<td><img src="' + graphPath+ 'spacer.gif" alt="" width="2" height="1" border="0"></td>\
														</tr>\
													</table>\
												</td>\
												<td><img src="' + graphPath+ 'spacer.gif" alt="" width="20" height="1" border="0"></td>\
												<td class="game5Text3" nowrap width="15">\
													<img src="' + graphPath+ 'general/person.gif" alt="" border="0">\
												</td>\
												<td class="game5Text3" align="left" width="25" nowrap>'
													+NumberOfPlayers+
												'</td>\
											</tr>\
											</table>\
										</td>\
										<td><img src="' + graphPath+ 'spacer.gif" alt="" width="7" height="1" border="0"></td>\
									</tr>\
								</table>\
							</td>\
						</tr>\
					</table>\
				</td>'
			;
			
	return s;
}

// draw 'play online'
function drawGameItemFooter_PlayOnline(obj)
{
	var s = '';

	if (obj.isOnline=='true')
	{
		var lobbyUrl = obj.lobbyURL;
		s =	'<td class="game1LinkPlace1">\
				<table  border="0" cellpadding="0" cellspacing="0">\
					<tr>\
						<td class="game1Padding2"><a href onclick = "AutoPlayDIV.onClick(event, ' +obj.sku+ ',\'' +lobbyUrl+ '\');return false" onmouseout="AutoPlayDIV.onMouseOut(event); return false;"><img src="' + graphPath+ 'general/arrow1_up.gif" alt="" width="11" height="11" border="0"></a></td>\
						<td><img src="' + graphPath+ 'spacer.gif" alt="" width="1" height="1" border="0"></td>\
						<td id="'+ obj.sku +'">\
							<a  href onclick = "AutoPlayDIV.onClick(event, ' +obj.sku+ ',\'' +lobbyUrl+ '\');return false" onmouseout="AutoPlayDIV.onMouseOut(event); return false;" class="game1link1">' +txtOnline+ '</a>\
						</td>\
					</tr>\
				</table>\
			<td>';
	}
	else
	{
		s =	'<td class="game1LinkPlace1">\
						<table  width="89px" border="0" cellpadding="0" cellspacing="0">\
						</table>\
					<td>';
	}
	
	return s;
}
// draw 'download'
function drawGameItemFooter_Download(obj)
{
	var s = '';
	
	if (obj.downloadURL.length>0)
	{
		s =	'<td class="game1LinkPlace2" align="right">\
				<table border="0" cellpadding="0" cellspacing="0">\
					<tr>\
						<td class="game1Padding2"><a href="' + downloadDomain+obj.downloadURL + '"  target="_self" onclick="Game.downloadEvents"><img src="' + graphPath+ 'general/arrow3_down.gif" alt="" width="11" height="11" border="0"></a></td>\
						<td><img src="' + graphPath+ 'spacer.gif" alt="" width="1" height="1" border="0"></td>\
						<td><a href="' + downloadDomain+obj.downloadURL + '"  target="_self" onclick="ReportDownload('+ obj.sku + ')"  class="game1link1">' + txtDownload+ '</a></td>\
					</tr>\
				</table>\
			</td>';
	}
	return s;
}
// draw 'checkout'
function drawGameItemFooter_Checkout(obj)
{
	var s = '';
	
	if (obj.buyURL.length>0)
	{
		s =	'<td class="game1LinkPlace3" width="57px">\
				<table border="0" cellpadding="0" cellspacing="0">\
					<tr>\
						<td class="game1Padding2"><a href="javascript:checkOutPageUrl(\'' +obj.buyURL+ '\')"><img src="' + graphPath+ 'general/arrow2_right.gif" alt="" width="11" height="11" border="0"></a></td>\
						<td><img src="' + graphPath+ 'spacer.gif" alt="" width="1" height="1" border="0"></td>\
						<td><a href="javascript:checkOutPageUrl(\'' +obj.buyURL+ '\')" class="game1link1">' +txtBuy+ '</a></td>\
					</tr>\
				</table>\
				<td>';
	}

	return s;
}
//draw the footer ('online'+'download'+checkout')			
function drawGameItemFooter(obj)
{
	var onlineFooter	= drawGameItemFooter_PlayOnline(obj);
	var downloadFooter	= drawGameItemFooter_Download(obj);
	var checkoutFooter	= drawGameItemFooter_Checkout(obj);
	
	
	var s = '<td height="14" align="right" style="padding-left: 97px; padding-right: 7px;">\
				<table border="0" cellpadding="0" cellspacing="0" width="100%">\
					<tr>' + onlineFooter;

					if (onlineFooter.length>0)
						s += '<td class="game1LinkSepPlace" style="width:1px">|</td>';

					s += downloadFooter;
						
					if (downloadFooter.length>0)
						s += '<td class="game1LinkSepPlace" style="width:1px">|</td>';
							
					s += drawGameItemFooter_Checkout(obj) +
					
					'</tr>\
				</table>\
			</td>'
			;
	return s;
}
//draw game item body (description+image)			
function drawGameItemBody(obj)
{
	var txtTitle	= '';
	var txtAlt		= '';
	var url			= '';

	if (obj.isMultiPlayer=='true')
	{
		txtTitle = 'Jouer jeux '  + obj.name;
		txtAlt = 'jeux ' +obj.name+ ' gratuit, jouer jeu multijoueur';
		url = obj.gamePageURL;
	}
	else
	if (obj.isOnline=='true')
	{
		txtTitle = 'Jeux en ligne '  + obj.name;
		txtAlt = 'jouer en ligne &agrave; ' + obj.name + ' gratuitement';
		url = obj.gamePageURL;
	}
	else
	{
		txtTitle = txtDownload + ' ' +obj.name;
		txtAlt = txtDownload.toLowerCase() + ' ' +obj.name + ' gratuit, jouer';
		url	= oberonDomain + obj.downloadURL;
	}
	
	var s =	'<td class="padding7">\
					<table width="100%" border="0" cellpadding="0" cellspacing="0">\
						<tr>\
							<td class="game1Padding3" valign="top" nowrap>\
								<a href="' + url + '">\
								<img src="' + oberonDomain+obj.smallThumbnail+ '" width="81" height="46" border="0" alt="' +txtAlt+ '" Title="' +txtTitle+ '"></a>\
							</td>\
							<td width="100%" valign="top">\
								<table height="46px" width="100%" border="0" cellpadding="0" cellspacing="0">\
									<tr>\
										<td valign="top">\
											<table class="game1Desc" border="0" cellpadding="0" cellspacing="0">\
												<tr>\
													<td class="game1Text1">' +highLight(obj.twoThirdsDesc, "searchHighlight", Paginator._terms)+ '</td>\
												</tr>\
											</table>\
										</td>\
									</tr>\
									<tr>\
										<td height="10"><img src="' +graphPath+ 'spacer.gif" alt="" width="1" height="1" border="0"></td>\
									</tr>\
									<tr height="14px">'
										+ drawGameItemFooter(obj) +
									'</tr>\
								</table>\
							</td>\
						</tr>\
					</table>\
				</td>'
			;
	
	return s;				
}
//draw separator			
function drawGameItemSeparator(obj)
{
	var s =	'<tr>\
				<td class="game1Sep1"><img src="' + graphPath+ 'spacer.gif" alt="" width="1" height="1" border="0"></td>\
			</tr>\
			<tr>\
				<td height="8"><img src="' + graphPath+ 'spacer.gif" alt="" width="1" height="1" border="0"></td>\
			</tr>'
		;
	
	return s;
}
//draw the game item (title+body+footer)
function drawItem(obj)
{
	var s = 
		'<table width="100%" border="0" cellpadding="0" cellspacing="0">'
		+	'<tr>'
		+		drawGameItemTitle(obj)
		+	'</tr>'
		+		drawGameItemSeparator(obj)
		+	'<tr>'
		+		drawGameItemBody(obj)
		+	'</tr>'
		+'</table>'
		;

	return s;
}
//-----------------------------------
 // Search Page Results object
//-----------------------------------
function SearchResultPage(result, gameCatalogSkus, itemsPerPage, visiblePages) 
{
	this._totalSkus	= 0;
	this._terms		= null;

	if (	result==null
		||	request.result.skus.length==0)
	{
		return;
	}
		
	this._objs			= new Array();
	this._terms			= new Array();
	this._firstVisPage	= 0;	
	this._currPage		= 0;
	this._prevPage		= -1;	

	this._visiblePages	= visiblePages;
	this._itemsPerPage	= itemsPerPage;

	this._isSuggested = result.isSuggested;
	if (this._isSuggested==true)
		this._similarWord = result.similarWord;
	
	for (var i=0; i<result.skus.length; i++)
	{
		var sku = result.skus[i];
		var obj = gameCatalogSkus[sku];
		if (obj!=undefined)
		{
			this._objs[this._totalSkus++] = obj;
		}
	}
	
	for (var i=0; i<result.terms.length; i++)
	{
		var term = result.terms[i];
		if(term != "+"){
			if(term.indexOf("*") == (term.length-1)){			
				term = term.substring(0,term.length-1);
			}
			this._terms.push(term);
		}
	}

	this._totalPages = Math.floor(this._totalSkus / this._itemsPerPage);
	if (this._totalSkus % this._itemsPerPage > 0)
		++this._totalPages;
}
	
// draw page
SearchResultPage.prototype.DrawPage = function()
{
	var i = this._currPage * this._itemsPerPage;
	var j = i + this._itemsPerPage;
	if (j>=this._totalSkus)
		j = this._totalSkus;
	
	var s = '';
	
	s += drawSpacer();
	for (i; i<j; i++)
	{
 		s += drawItem(this._objs[i]);
		s += drawSpacer();
	}
	
	return s;
}

// process on prev button
SearchResultPage.prototype.onPrev = function()
{
	if (this._currPage > 0)
	{
		--this._currPage;
		
		if (this._currPage < this._firstVisPage)
		{
			--this._firstVisPage;
		}
		
		this.Refresh();

		window.scrollTo(0, 0);
	}
}
// process on next button
SearchResultPage.prototype.onNext = function()
{
	if (this._currPage < this._totalPages-1)
	{
		++this._currPage;
		
		if (this._currPage >= this._firstVisPage+this._visiblePages)
		{
			++this._firstVisPage;
		}
		
		this.Refresh();

		window.scrollTo(0, 0);
	}	
}

// // process on (page no) button
SearchResultPage.prototype.onChangePage = function(pageNo)
{
	if (this._currPage != pageNo)
	{
		this._currPage = pageNo;
		this.Refresh();
		
		window.scrollTo(0, 0);
	}
}

// draw Search Results Page Title		
SearchResultPage.prototype.DrawTitle = function()
{
	var s = '<table>\
				<tr>\
					<td><img src="' + graphPath+ 'sideMenu1/searchResults.gif" alt="" width="305" height="18" border="0"></td>\
				</tr>';
				
		if (this._totalSkus>0 && this._isSuggested==true)
		{
			// 0 found for word <xxx>
			s += '<tr>';
			s += '<td style="font-size:16px">\
				0 jeu trouv&eacute;&nbsp;<span style="color:#FF5500;font-size:16px;">' +queryStr+ '</span>\
				</td>';
			s += '</tr>';

			queryStr = this._similarWord;
		}

		var term = this._terms==null? queryStr : this._terms.join(' ');
		
		// x found for word <yyy>
		s += '<tr>';
		s += '<td>';
		
		if (this._totalSkus>0)
			s += '<span style="font-size:16px">' +this._totalSkus+ '&nbsp;jeu(x) trouv&eacute;(s) pour&nbsp;</span>';
		else
			s += '<span style="font-size:16px">' +this._totalSkus+ '&nbsp;jeu trouv&eacute;&nbsp;</span>';
			
		s += '<span style="color:#FF5500;font-size:16px;">' +term+ '</span></td>';
		s += '</tr>\
			</table>';

	return s;
}
// draw Search Results Page footer
SearchResultPage.prototype.DrawFooter = function()
{
	if (this._totalSkus<=0)
		return;

	var s =
		'<table>\
			<tr><div style="height:15"></div></tr>\
			<tr>';
			
	{
			s +=  '<td>\
						<div style="font-size:12px; width:35">'+txtPage+'</div>\
					</td>';
		
		if (this._currPage>0)
		{
			s +=  '<td>\
						<div onclick="Paginator.onPrev()" style="CURSOR: pointer; font-size:12px"><u>'+txtPrevPg+'</u></div>\
					</td>';
		}

		var i = this._firstVisPage;
		var j = i+this._visiblePages;
		if (j>=this._totalPages)
			j=this._totalPages;

		for (i; i < j; i++)
		{
			if (i==this._currPage)
			{
				s += '<td>'
						+ '<div onclick="Paginator.onChangePage(' +i+ ')" style="width:14; font-size:12px"><b>' + (i+1) + '</b></div>'
					+ '</td>';
			}
			else
			{
				s += '<td>'
						+ '<div onclick="Paginator.onChangePage(' +i+ ')" style="CURSOR: pointer; width:14; font-size:12px"><u>' + (i+1) + '</u></div>'
					+ '</td>';
			}
		}
		
		if (this._currPage!=this._totalPages-1)
		{
			s +=  '<td>\
						<div onclick="Paginator.onNext()"; style="CURSOR: pointer; font-size:12px"><u>'+txtNextPg+'</u></div>\
					</td>';
		}
		
	}
	
	s +=	'</tr>\
		</table>';

	return s;
};

// refresh Search Results Page
SearchResultPage.prototype.Refresh = function()
{
	var new_content = '';

	PreOmnitureReport(this._totalSkus>0);
	
	new_content += Paginator.DrawTitle();
	
	if (this._totalSkus>0)
	{
		new_content += this.DrawPage();
		new_content += this.DrawFooter();
	}
	else
	{
		new_content += this.notFound();
	}

	new_content = '<div>'+new_content+'<div/>';
	
	document.getElementById("PageContent").innerHTML = new_content;
	

	OmnitureReport(this._totalSkus>0);
}

// draw not found
SearchResultPage.prototype.notFound = function()
{
	var style = 'color: #000000; font-family:arial; font-size: 12px;';
	var queryStyle = 'color: #FF0000; font-family:arial; font-size: 18px;';

	var s = 
	
	'<table>\
		<tr>\
			<td></td>\
		</tr>\
		<tr>\
			<td>\
				Les termes de recherche sp&eacute;cifi&eacute;s - <span style="queryStyle">' + queryStr + '</span> - ne correspondent &agrave; aucun jeu.\
			</td>\
		</tr>\
		<tr>\
			<td>\
				Suggestions :\
			</td>\
		</tr>\
		<tr>\
			<td>\
				<menu>\
				<li>V&eacute;rifiez &#318;orthographe des termes de recherche.</li>\
				<li>Essayez d&#39;autres mots.</li>\
				<li>Utilisez des mots plus g&eacute;n&eacute;raux.</li>\
				<li>Tapez une requ&ecirc;te plus courte.</li>\
				</menu>\
			</td>\
		</tr>\
		<tr>\
			<td>\
				Si vous ne parvenez pas &agrave; trouver le jeu recherch&eacute;, notre &eacute;quipe\
				&eacute;ditoriale vous recommande d&#39;essayer les jeux suivants :\
			</td>\
		</tr>\
		<tr>\
			<td height="30px">\
				&nbsp;\
			</td>\
		</tr>\
	</table>'
	;

	if (SearchData!=undefined)
	{
		for (var i=0; i<SearchData.topDownLoadGameSkus.length; i++)
		{
			var sku = SearchData.topDownLoadGameSkus[i];
			var obj = TC.SKUs[sku];
			if (obj)
				s += drawItem(obj);
		}
	}	

	return s;	
}

// ---------------------------------------
// handlers
function processError(request)
{
	request.result = null;
	onAnswer(request);
}

function onTimeout(request)
{ 
	processError(request);
}

function onAnswer(request)
{
	if (Paginator!='undefined')
		Paginator = null;

	try
	{
		Paginator = new SearchResultPage(request.result, TC.SKUs, 10, 10);
		Paginator.Refresh();
	}
	catch(e)
	{
		processError(request);
	}
}

function submit_search()
{
	queryStr = document.getElementById('searchTextBoxID').value;
	
	// remove '#' symbol from url 
	queryStr = queryStr.replace(/#/g, '');

	if (queryStr.length > 0)
	{
		req = encodeURI(reqStr + queryStr);

		try
		{
			// request id is always 0, to prevent problems with caching on Akamai
			Jast.nextRequestId	= 0; 
			Jast.timeout		= 60000;
			
			var jastReq = new Jast.Request(req, {onSuccess:onAnswer,onFailure:onAnswer,onTimeout:onTimeout});
			showSearchAnim();
		}
		catch(e)
		{}
	}
}

// prevent submit when enter is pressed in input box
function noenter(event) 
{
	var keyPressed = window.event ? window.event.keyCode : event.which;

	if (keyPressed == 13)
	{
		submit_search();
		return false;
	}
	return true;
}

// ---------------------------------------
// draw search animation
function showSearchAnim()
{
	document.getElementById("PageContent").innerHTML = 
		'<table border="0" width="100%" height="200px">\
			<tr>\
				<td align="center">\
					Recherche...<br>\
					<img src="' + graphPath+ 'sideMenu1/searchAnim.gif" alt="" width="80" height="46" border="0">\
				</td>\
			</tr>\
		</table>';
}
// ---------------------------------------
