/*re
**
** Copyright (C) 2008-2009 PublicDomainReprints.org. All rights reserved.
** May not be used without permission.
**
** Contact code@shaftek.org for more information.
*/

function searchAjaxOnLoad() {
    // Set options for archives
    // Internet Archive
    searcherIA = new google.search.WebSearch();
    searcherIA.setUserDefinedLabel('Internet Archive');
    searcherIA.setSiteRestriction('archive.org/details/');
    searcherIA.setQueryAddition('"Mediatype*texts"');
    //searcherIA.setQueryAddition('"NOT_IN_COPYRIGHT"');

    // Google Books
    searcherGB = new google.search.BookSearch();
    searcherGB.setUserDefinedLabel('Google Books');
    searcherGB.setRestriction(google.search.Search.RESTRICT_TYPE, google.search.BookSearch.TYPE_FULL_VIEW_BOOKS);
    searcherGB.setQueryAddition('date:0-1923');

    // OpenCRS
    searcherCRS = new google.search.WebSearch();
    searcherCRS.setUserDefinedLabel('OpenCRS');
    searcherCRS.setSiteRestriction('opencrs.com/document/');
    var optionsCRS = new google.search.SearcherOptions();    
    optionsCRS.setExpandMode(google.search.SearchControl.EXPAND_MODE_OPEN);

    // Add to search control
    var searcherOptions = new GsearcherOptions();
    searcherOptions.setExpandMode(GSearchControl.EXPAND_MODE_OPEN);
    var searchControl = new google.search.SearchControl();
    searchControl.setResultSetSize(google.search.Search.LARGE_RESULTSET);
    searchControl.addSearcher(searcherIA, searcherOptions);
    searchControl.addSearcher(searcherGB, searcherOptions);
    searchControl.addSearcher(searcherCRS, optionsCRS);
    searchControl.setOnKeepCallback(this, searchAjaxMyKeepHandler, 'Request a Reprint');
    
    // Draw and execute
    var drawOptions = new GdrawOptions();
    //drawOptions.setDrawMode(GSearchControl.DRAW_MODE_TABBED);
    drawOptions.setDrawMode(GSearchControl.DRAW_MODE_LINEAR);
    searchControl.draw($("searchcontrol"), drawOptions);
    
    // Check for query
    if(queryUnescaped.length > 0) {
        searchControl.execute(queryUnescaped);
	
	// Match URL
        if(queryUnescaped.match(/http\:\/\/.*/)) {
	    if(confirm('It looks like you are trying to request a reprint. Do you want to go to the request page?')) {
		location.href = 'http://www.publicdomainreprints.org/code/review.pl?url=' + escape(queryUnescaped) + '&from=search_redirect';
	    }	    
	}
    }
}

function searchAjaxMyKeepHandler(result) {
    if(result.GsearchResultClass == google.search.WebSearch.RESULT_CLASS) {
	if(confirm('Preview your reprint request for "' + result.titleNoFormatting.sub('Internet Archive: Details: ', '') + '". Do you want to continue?')) {
	    window.open('http://www.publicdomainreprints.org/code/review.pl?url=' + result.url + '&from=search');
	}
    } else if(result.GsearchResultClass == google.search.BookSearch.RESULT_CLASS) {
	// Check pages
	if(result.pageCount > 730) {
	    alert('This service cannot process books larger than 730 pages!');
	}

	// Calculate Price
	if(result.pageCount > 0) {
    	    price = 4.53 + result.pageCount*0.02;
	    price = price.ceil() - 0.01;
	    msg = ' (estimated price for softcover is ' + price + ')';	    
	} else {
	    msg = '';
	}
	
	if(confirm('Preview your reprint request for "' + result.titleNoFormatting + '" from Google Books' + msg + '. Do you want to continue?')) {
	    window.open('http://www.publicdomainreprints.org/code/review.pl?url=' + result.url + '&from=search');
	}
    } else {
	alert('Unknown result, please contact the site administrator!');
    }
}

function searchAjaxInit() {
    google.load("search", "1");
    google.setOnLoadCallback(searchAjaxOnLoad);
}
