google.load( "search", "1" );

function OnLoad() {
	if ( statusGoogleSearchGWT ) {
		new RawSearchControl();
	} else {
		window.setTimeout( "OnLoad()", 3 );
	}
}

function RawSearchControl() {
	// Create a web serach
	var webSearch = new google.search.WebSearch();
	
	webSearch.setSiteRestriction( "www.comibook.com" );
	
	webSearch.setSearchCompleteCallback( this, searchComplete, [webSearch]);
	
	//for 10 results
/*	webSearch.setResultSetSize( google.search.Search.FILTERED_CSE_RESULTSET );*/
	
	//for 4 results
/*	webSearch.setResultSetSize( google.search.Search.SMALL_RESULTSET );*/
	
	//for 8 results
	webSearch.setResultSetSize( google.search.Search.LARGE_RESULTSET );
	
	//used without setting setSiteRestriction();
/*	webSearch.execute( "site:www.comibook.com intitle:" + document.getElementById( "search_keyword" ).value );*/

	webSearch.execute( document.getElementById( "search_keyword" ).value );
}

function searchComplete ( searcher ) {
	initAddCount();
	
	clearResults();
	
	if ( searcher.results.length > 0 && searcher.cursor ) {
		var switch_button = document.getElementById( "glistpageswitch" );
		
		setPageNumber( searcher.cursor.currentPageIndex );
		
		for ( var k = 0; k < searcher.results.length; k++ ) {
			var artURL = searcher.results[k].unescapedUrl;
			var artID = artURL.substring( artURL.lastIndexOf( "/" ) + 3 );
			var artTitle = searcher.results[k].titleNoFormatting;
			
			artTitle = artTitle.substring( artTitle.indexOf( "-" ) + 1 );
			
/*			alert( "[" + k + "] - Title : " + artTitle );
			alert( "[" + k + "] - URL : " + artURL );
*/			
			if ( !isNaN( artID ) && artID != "" ) {
				addNewRow( artID, artTitle, artURL );
			}
		}
		
		var img_url = "";
		
		removeChildren( switch_button );
		
		for ( var i = 0; i < searcher.cursor.pages.length; i++ ) {
			img_url = "";
			
			if ( i == ( searcher.cursor.currentPageIndex - 1 ) ) {
				img_url = WebsiteURL + "/images/LastPage.gif";
			} else if ( i == ( searcher.cursor.currentPageIndex + 1 ) ) {
				if ( i < searcher.cursor.pages.length ) {
					img_url = WebsiteURL + "/images/NextPage.gif";
				} else {
					img_url = WebsiteURL + "/images/NoPage.gif";
				}
			} else {
				img_url = "";
			}
			
			if ( img_url != "" ) {
				var pageNode = createIMG( img_url );
				
				if ( img_url != WebsiteURL + "/images/NoPage.gif" ) {
					pageNode.onclick = methodClosure( this, gotoPage, [searcher, i] );
				}
				
				switch_button.appendChild( pageNode );
			}
		}
	}
	
	clearMessage();
}

function gotoPage ( searcher, page ) {
	searcher.gotoPage( page );
}

function methodClosure ( object, method, opt_argArray ) {
	return function () {
		return method.apply( object, opt_argArray );
	}
}

function createIMG ( src_url ) {
	var el = document.createElement( "img" );
	el.src = src_url;
	return el;
}

function removeChildren ( parent ) {
	while ( parent.firstChild ) {
		parent.removeChild( parent.firstChild );
	}
}

google.setOnLoadCallback( OnLoad );
