﻿// Check that the page has loaded
$(document).ready(function () {


    // Show refine results popup
    $('.refineResults UL LI A:not(UL LI UL LI A)').click(function () {

        // Hide All
        $('.refineResultsPopup').hide();

        var offsetHeight = -(($(this).parent().find('.refineResultsPopup').outerHeight()) / 2) + 8;

        // Show correct one
        $(this).parent().find('.refineResultsPopup').css('top', offsetHeight).show('fast');

        return false;

    });

    // Close refine results popup
    $('.refineResultsPopup .btnClose').click(function () {

        $('.refineResultsPopup').hide();

        return false;

    });

    $('.refineResultsPopup .checkBox').live('click', function () {
        $(this).parents('li').find('.selectedOptions').append('<li>' + $(this).html() + ' (<a href="#" class="remove">remove</a>)</li>');

        if ($(this).siblings().length < 1) {
            $(this).parents('.refineResultsPopup').hide();
        }

        $(this).remove();



        getFilterResults();


        return false;
    });

    $('.refineResults .remove').live('click', function () {
        $(this).parents('li').parents('li').find('.refineResultsPopupMiddle').append('<span class="checkBox clearfix">' + $(this).parent().text().replace('(remove)', '') + '</span>');
        $(this).parent().remove();
        getFilterResults();

        return false;
    });

    $('.btnRefine').click(function () {

        var searchURL = "";
        var qs = new Querystring()

        if (qs.get('CatID') != null) {
            searchURL += 'CatID=' + qs.get('CatID') + '&';
        }

        searchURL += "refine=true";

        $('.refineResults UL .field').each(function () {
            searchURL += '&';
            searchURL += $(this).attr('id') + '=';
            var optionIndex = 0;
            $(this).find('.selectedOptions li').each(function () {
                if (optionIndex > 0) {
                    searchURL += ',';
                }
                searchURL += $(this).text().replace(' (remove)', '');
                optionIndex++;
            });
        });

        window.location.search = searchURL;
        return false;
    });


});

function getFilterResults() {
        
    // Create Data Object
    var myData = {};


    // Build the filter options
    $('.refineResults UL .field').each(function () {

        var optionIndex = 0;
        var fieldValue = "";
        $(this).find('.selectedOptions li').each(function () {
            if (optionIndex > 0) {
                fieldValue += ',';
            }
            fieldValue += $(this).text().replace(' (remove)', '');
            optionIndex++;
        });

        myData[$(this).attr('id')] = fieldValue;

    });

    var qs = new Querystring()

    if (qs.get('CatID') != null) {
        myData.CatID = qs.get('CatID');
    }
    else {
        myData.CatID = 224;
    }

    if (qs.get('page') != null) {
        myData.PageNo = qs.get('page');
    }
    else {
        myData.PageNo = 1;
    }

    // Do Ajax Call
    TVI.ajax({
        data: myData,
        url: '/handlers/B4P.aspx/getFilteredPlants',
        success: function (d) {
            $('.BrowseResults').html(d.productHtml + '<div style="clear:both; margin-top:10px;"></div>');
            $('.PagingContainer').html(d.paging);
        }

    });

}

/* Client-side access to querystring name=value pairs
Version 1.3
28 May 2008
	
License (Simplified BSD):
http://adamv.com/dev/javascript/qslicense.txt
*/
function Querystring(qs) { // optionally pass a querystring to parse
    this.params = {};

    if (qs == null) qs = location.search.substring(1, location.search.length);
    if (qs.length == 0) return;

    // Turn <plus> back to <space>
    // See: http://www.w3.org/TR/REC-html40/interact/forms.html#h-17.13.4.1
    qs = qs.replace(/\+/g, ' ');
    var args = qs.split('&'); // parse out name/value pairs separated via &

    // split out each name=value pair
    for (var i = 0; i < args.length; i++) {
        var pair = args[i].split('=');
        var name = decodeURIComponent(pair[0]);

        var value = (pair.length == 2)
			? decodeURIComponent(pair[1])
			: name;

        this.params[name] = value;
    }
}

Querystring.prototype.get = function(key, default_) {
    var value = this.params[key];
    return (value != null) ? value : default_;
}

Querystring.prototype.contains = function(key) {
    var value = this.params[key];
    return (value != null);
}
