﻿function setupFiltering() {
    $("#priceFilter").bind("change", function() { getDealsWithFilter($(this).val()); });
}
function getDealsWithFilter(val) {
    if (val != 9) {
        window.location = requestUrl + "?filterId=" + val;
    } else {
        window.location = requestUrl;    
    }

}

function loadImagesFromGoogle() {
    var noImageDeals = $(".searchImage");
    if (noImageDeals.length > 0) {
        var title = $(noImageDeals[0]).parents(".item").find(".iTitle").text();
        imageSearch.execute(title);
    }
}

function searchComplete(searcher) {
    var noImageDeals = $(".searchImage");
    var currentItem = $(noImageDeals[0]);
    // Check that we got results
    if (searcher.results && searcher.results.length > 0) {       

        // Loop through our results, printing them to the page.
        var results = searcher.results;
        for (var i = 0; i < results.length; i++) {
            var result = results[i];
            var imgContainer = currentItem.find(".iImage")[0];

            var newImg = document.createElement('img');            
            newImg.src = result.tbUrl;

            $(currentItem).append(newImg);
            $(currentItem).removeClass("noImgProd");//remove noProd
            break;
        }
    }
    $(currentItem).removeClass("searchImage");
    loadImagesFromGoogle();
}

function SetupGoogleSearch() {
    // Our ImageSearch instance.
    imageSearch = new google.search.ImageSearch();

    // Restrict to extra large images only
    imageSearch.setRestriction(google.search.ImageSearch.RESTRICT_IMAGESIZE,google.search.ImageSearch.IMAGESIZE_MEDIUM);

    // 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]);

    // Find me a beautiful car.
    //imageSearch.execute("Subaru STI");
}

function fetchMissingImages() {
    SetupGoogleSearch();
    loadImagesFromGoogle();
}
function setActiveTab(selector) {
    $(selector).addClass("active");
}

function getFeaturedProducts(q) {
    $("#blockFeaturedProdContainer").load("/featured/GetFeaturedProducts?q=" + q);
}