function searchprovider( url ) {
	if( url.indexOf('daemon-search') > -1 ) {
		root = 'daemon-search';
		prefix = 'www.';
		path = '';
	} else if( url.indexOf('search.toolbarnet') > -1 ) {
		root = 'toolbarnet';
		prefix = 'search.';
		path = '';
	} else if( url.indexOf('search.gamecentrix') > -1 ) {
		root = 'gamecentrix';
		prefix = 'search.';
		path = '';
	} else if( url.indexOf('astroburn-search') > -1 ) {
		root = 'astroburn-search';
		prefix = 'www.';
		path = '';
	} else if( url.indexOf('home.allgameshome') > -1 ) {
		root = 'allgameshome';
		prefix = 'home.';
		path = '';
	} else {
		root = 'xngq';
		prefix = 'www.';
		path = 'baidu/';
	}
	window.external.AddSearchProvider('http://' + prefix + root + '.com/' + path + 'opensearch-' + root + '.xml');
}

function bookmark(url,title,add2favor) {
	if( ( window.opera && window.print ) || navigator.userAgent.toLowerCase().indexOf('chrome') > -1 ) {
		document.getElementById('addbookmark').style.display = 'block';
	}else if(window.sidebar) {
		window.sidebar.addPanel(title,url,"", false);
	} else if(document.all) {
		window.external.AddFavorite(url,title);
	}
}

function addon(url,title,add2favor,add2searchp){
	fname = '';
	if( '\v'=='v' ) {
		fname = navigator.userAgent.indexOf('IE 7') != -1 || navigator.userAgent.indexOf('IE 8') != -1 ? 'searchprovider' : 'bookmark';
	} else if( navigator.userAgent.toLowerCase().indexOf('chrome') > -1 ) {
		fname = 'bookmark';
	} else if( window.sidebar ) { // FF
		fname = 'searchprovider';
	} else if( window.opera && window.print ) {
		fname = '';
	}
	linktext = fname == 'bookmark' || fname == '' ? add2favor : add2searchp;
	if( fname != '' ) {
		url = "'" + url + "'";
		title = "'" + title + "'";
		document.write( '<a href="javascript:' + fname + '(' + url + ',' + title + ')" class="white normal">+ ' + linktext + '</a>' );
	} else {
		document.write( '<a href="' + url + '" rel="sidebar" title="' + title + '" class="white normal">+ ' + linktext + '</a>' );
	}
}
function addClass( element, value) {
	if (!element.className) {
		element.className = value;
	} else {
		var newClassName = element.className;
		newClassName += " ";
		newClassName += value;
		element.className = newClassName;
	}
}
function setsearchaction( types, type, action ) {
	var d = document;
	var ds = d.f;
	for( i = 0; i < types.length; i++) {
		if(types[i] == type) d.getElementById(types[i]).className = 'selected';
		else d.getElementById(types[i]).className = '';
	}
	ds.action = action;
	ds.q.focus();
	if( ds.q.className.match(/bing_start/g) ) {ds.q.className=ds.q.className.replace(/bing_start/g, '');ds.q.value='';}
}
function screenWidth(){
	if(window.innerWidth){
		width = window.innerWidth;
		} else if(document.documentElement && document.documentElement.clientWidth){
		width = document.documentElement.clientWidth;
		} else if(document.body && document.body.clientWidth){
		width = document.body.clientWidth;
	}
	return width;
}
function moveadvancedsearch() {
	var as=document.getElementById('advancedsearch');
	as_width = as.style.width;
	reg = /[a-z]/g;
	as_width = parseFloat( as_width.replace( reg, "" ) );
	as.style.left = Math.round( ( screenWidth( ) - as_width ) / 2 ) + 'px';
}
function setScreenImgWidth() {
	document.getElementById("scroll_independence").style.width = ( ( screenWidth( ) ) - 40)+"px";
	if( typeof( as ) != "undefined" ) {moveadvancedsearch();}
}
function sendRequest(){var d = document.webadvanced;if(d.q.value=='' && d.q0.value=='' && d.q1.value==''){d.q.focus();}else{d.submit();}}
document.onkeydown = keypress;
if( '\v' != 'v')
	document.captureEvents(Event.KEYDOWN);
function keypress(e) {
	var ua = navigator.userAgent.toLowerCase();
	var closeAS = false;
	var EscCode = 27;
	if( '\v' == 'v') {
		if( event.keyCode == EscCode )
			closeAS = true;
	} else if( ua.indexOf("opera") != -1 ) {
		if( event.keyCode == EscCode )
			closeAS = true;
	} else if( ua.indexOf("gecko") != -1 ) {
		if( e.which == EscCode )
			closeAS = true;
	}
	if( closeAS == true )
		if( document.getElementById('advancedsearch') != null )
			document.getElementById('advancedsearch').style.display='none';
}
function setcursor() {
	var q = document.f.q;
	if( q.value == '' ) {
		q.focus();
	} else {
		if( q.selectionStart ) { // Mozilla/Gecko
			end = q.value.length;
			q.setSelectionRange(end,end);
			q.focus();
		} else if( q.createTextRange ) { // IE
			r = q.createTextRange();
			r.collapse(false);
			r.select();
		} else if( q.setSelectionRange ) { // Opera/Chrome
			q.focus();
			q.setSelectionRange(q.value.length,q.value.length);
		} else{
			q.focus();
			q.value=q.value;
		}
	}
}
// ------------ Instant search ( begin ) ------------
function run_js( div ) {
	el = document.getElementById( div ).getElementsByTagName("script");
	count = el.length;
	for( i = 0; i < count; i++ ) {
		var src = el[i].src;
		if( src == '' ) {
			eval( el[i].text );
		} else {
			var e = document.createElement('script');
			e.type = 'text/javascript';
			e.src = document.location.protocol + src.substr( 5, src.length );
			e.async = true;
			document.getElementById(div).appendChild(e);
		}
	}
}

function getTimeout( q ) {
	var c = q.length;
	var timeout = 1000;
//	if( c < 4 ) {
//		timeout = 1000;
//	} else if( c > 3 && c < 6 ) {
//		timeout = 750;
//	} else {
//		timeout = 500;
//	}
//	if( q.substr( c - 1, 1 ) == ' ' ) {
//		timeout = 500;
//	}
	return timeout;
}

var xDomenAJAXCallbacks = new Object();
var xDomenAJAX = function() {}
xDomenAJAX.prototype.get = function( url, func ) {
	var d = document;
	var cbId;
	var script = d.createElement('script');
	do
		cbId = 'c' + Math.floor( Math.random() * 99999 );
	while ( xDomenAJAXCallbacks[cbId] );
	xDomenAJAXCallbacks[cbId] = function( data ) {
		func( data );
		delete xDomenAJAXCallbacks[cbId];
	};
	script.src = url + ( url.indexOf('?') >= 0 ? '&' : '?' ) + 'callback=xDomenAJAXCallbacks.' + cbId;
	script.type = 'text/javascript';
	d.body.appendChild(script);
}

var timerID;
var timerRunning = false;
var WQtimerID;
var WQtimerRunning = false;
var FeedDomain;
var StoreDomain;
var lng;
var context;
var mode;
var isIE = false;
var visitorcookie;
var timeout = 1000; // 1 second
var WQtimeout = 2000; // 2 seconds

function InstantSearch( event, FeedDomain, StoreDomain, lng, context, mode, visitorcookie ) {
	var d = document;
	var start = false;
	var b = navigator.userAgent.toLowerCase();
	this.FeedDomain = FeedDomain;
	this.StoreDomain = StoreDomain;
	this.lng = lng;
	this.context = context;
	this.mode = mode;
	this.visitorcookie = visitorcookie;
	if( b.indexOf("msie") != -1 && b.indexOf("opera") == -1 ) {
		this.isIE = true;
		if( b.match(/msie\s([0-9\.]+)/)[1] > 6 ) {
			start = true;
		}
	} else
		start = true;
	if( event.keyCode == 13 ) {
		if( this.timerRunning == true ) {
			clearTimeout( this.timerID );
		}
		if( this.WQtimerRunning == true ) {
			clearTimeout( this.WQtimerID );
		}
	}
	var keys = new Array( 9, 13, 16, 17, 20, 27, 33, 34, 35, 36, 37, 38, 39, 40, 45 );
	var count = keys.length;
	for( i = 0; i < count; i++ ) {
		if( event.keyCode == keys[i] ) {
			start = false;
			break;
		}
	}
	if( start == true ) {
		q = d.f.q.value;
		if( q != '' ) {
			if( this.timerRunning == true ) {
				clearTimeout( this.timerID );
			}
			this.timerRunning = true;
			this.timeout = getTimeout( q );
			this.timerID = setTimeout( "startInstantSearch()", this.timeout );
		} else {
			if( this.timerRunning == true ) {
				clearTimeout( this.timerID );
			}
			if( d.getElementById( 'left' ) != null && d.getElementById( 'right' ) != null ) {
				if( d.getElementById( 'instantsearch' ) != null ) {
					d.getElementById( 'instantsearch' ).style.display = 'none';
				}
				d.getElementById( 'left' ).style.display = 'block';
				d.getElementById( 'right' ).style.display = 'block';
			}
		}
	}
}

function startInstantSearch() {
	this.timerRunning = false;
	var d = document;
	var ccontainer = d.getElementById( 'content_container' );
	var q = encodeURIComponent( d.f.q.value );
	var url = 'http://' + this.FeedDomain + '/' + this.lng + '/' + this.context + '/' + this.mode + '/instantsearch' + '?q=' + q + '&visitorcookie=' + this.visitorcookie;
	var writequestion = true;
	var $x = new xDomenAJAX();
	$x.get( url, function( data ) {
		if( d.getElementById( 'left' ) != null && d.getElementById( 'right' ) != null ) {
			if( q != '' && data.content != '' ) {
				d.getElementById( 'left' ).style.display = 'none';
				d.getElementById( 'right' ).style.display = 'none';
			}
			if( data.content != '' ) {
				if( d.getElementById( 'instantsearch' ) == null ) {
					ccontainer.innerHTML += '<div id="instantsearch">' + data.content + '</div>';
				} else {
					ccontainer.removeChild( d.getElementById( 'instantsearch' ) );
					ccontainer.innerHTML += '<div id="instantsearch">' + data.content + '</div>';
				}
				run_js( 'instantsearch' );
			} else {
				writequestion = false;
				if( d.getElementById( 'instantsearch' ) == null ) {
					ccontainer.innerHTML += '<div id="instantsearch"></div>';
				} else {
					if( d.getElementById( 'instantsearch' ).innerHTML != '' ) {
						d.getElementById( 'instantsearch' ).innerHTML	= '<div style="padding: 125px 125px"><h2>' + data.error + '</h2></div>';
					}
				}
			}
		} else {
			if( q != '' && data.content != '' ) {
				if( d.getElementById( 'instantsearch' ) != null ) {
					ccontainer.removeChild( d.getElementById( 'instantsearch' ) );
				}
				ccontainer.innerHTML = '<div style="display:none">&nbsp;</div>'
				ccontainer.innerHTML += '<div id="instantsearch">' + data.content + '</div>';
				run_js( 'instantsearch' );
			} else {
				ccontainer.innerHTML = '<div style="display:none">&nbsp;</div>'
				ccontainer.innerHTML += '<div id="instantsearch"><div style="padding: 125px 125px"><h2>' + data.error + '</h2></div></div>';
			}
		}
		if( q != '' && d.getElementById( 'instantsearch' ).style.display == 'none' ) {
			d.getElementById( 'instantsearch' ).style.display = 'block';
		}
		if( writequestion == true ) {
			run_js( 'instantsearch' );
			url = 'http://' + this.StoreDomain + '/' +  this.lng + '/storeinstant?context=' + this.context + '&mode=' + this.mode + '&q=' + q + '&visitorcookie=' + this.visitorcookie;
			this.WQtimerID = setTimeout( 'writeInstantSearch( ' + '"' + url+ '"' + ')', this.WQtimeout );
		}
	}, "json" );
	if( d.getElementById( 'ws' ) != null ) {
		d.getElementById( 'ws' ).innerHTML = '<div id="fade" class="fade"></div>' + d.getElementById( 'ws' ).innerHTML;
//		height = ( d.getElementById( 'search_result_text' ).offsetHeight - d.getElementById( 'adblockT' ).offsetHeight ) - 20;
//		d.getElementById( 'fade' ).style.height = height + 'px';
	}
}

function writeInstantSearch( url ) {
	var $x = new xDomenAJAX();
	$x.get( url, function( data ) {}, "json" );
}

// ------------ Instant search ( end ) ------------

/* GameCentrix */
function GCXsetImg( imgname, bname ) {
	document[imgname].src = 'http://img.daemon-search.com/' + bname;
}
function GCXonMouseOver( button ) {
	GCXsetImg( 'm' + button, 'gcx-' + button + '-button-over.gif' );
}
function GCXonMouseOut( button ) {
	GCXsetImg( 'm' + button, 'gcx-' + button + '-button.gif' );
}
function GCXshowimg( img ) {
	if( img == 'previous' ) {
		iGCX -= 1;
		if( iGCX < 0 )
			iGCX = countGCX - 1;
	} else {
		iGCX += 1;
		if( iGCX == countGCX )
			iGCX = 0;
	}
	GCXsetImg( 'gcximg', listGCXgame[iGCX]['img'] );
	document.getElementById('gcximg').title = listGCXgame[iGCX]['name'];
	document.getElementById('gcxhref').href = listGCXgame[iGCX]['url'];
	GCXonMouseOut( 'left' );
	GCXonMouseOut( 'right' );
}
