﻿/*
*  How to search for images and restrict them by size.
*  This demo will also show how to use Raw Searchers, aka a searcher that is
*  not attached to a SearchControl.  Thus, we will handle and draw the results
*  manually.
*/

/*
* Please call the following google API Key method before including this javascript
* <script src="http://www.google.com/jsapi?key=ABQIAAAA1XbMiDxx_BTCY2_FkPh06RRaGTYH6UMl8mADNa0YKuWNNa8VNxQEerTAUcfkyrr6OwBovxn7TDAH5Q"></script> 
*/
google.load('search', '1');

function searchComplete(searcher, imgTag) {
    // Check that we got results
    if (searcher.results && searcher.results.length > 0) {
        imgTag.src = searcher.results[0].tbUrl;
        imgTag.alt = searcher.results[0].url;
    }
}

function AssignImage(imgTag) {
    var itemName = imgTag.attributes.getNamedItem("alt").value;
    // Our ImageSearch instance.
    var imageSearch = new google.search.ImageSearch();

    // Restrict to extra large images only
    imageSearch.setRestriction(google.search.ImageSearch.RESTRICT_IMAGESIZE,
                             google.search.ImageSearch.IMAGESIZE_MEDIUM);
    imageSearch.setRestriction(google.search.ImageSearch.RESTRICT_COLORIZATION,
                             google.search.ImageSearch.COLORIZATION_COLOR);


    // Here we set a callback so that anytime a search is executed, it will call
    // the searchComplete function and pass it our ImageSearch searcher.
    // When a search completes, our ImageSearch object is automatically
    // populated with the results.
    imageSearch.setSearchCompleteCallback(this, searchComplete, [imageSearch, imgTag]);

    // Search for the images on the web
    //If category has - in it than return recipe. This is for Chutney-N-Pickle
    var query = getqueryParameter("Category");
    if ((query.indexOf('-') > 0) || (query == "Entree"))
    { query = "recipe"; }
    imageSearch.execute(GetImageSearchString(itemName + " " + query));
}

function GetImageSearchString(itemName) {
    var index = itemName.indexOf('(');
    if (index > 0)
        itemName = itemName.substring(0, index);
    return itemName.trim();

}
