//This is used for the new search box
//it hooks up the clicks of go button
//and pressing enter in the text box
jQuery(document).ready(function() {
    jQuery('.SearchBarGoBtn').bind('click', function(event) {
        //alert(jQuery(this).attr('searchURL')); //jQuery('.hs_search_bar_mode_menu').val());
        //alert(jQuery('.hs_search_bar_input_field').attr('id'));
        Search(jQuery(this).attr('searchURL'), jQuery('.rd_nav_input').attr('id'),
                 'SS'); //jQuery('.hs_search_bar_mode_menu').val());

        return false;
    });

    jQuery('.SearchBarGoBtnPopup').bind('click', function(event) {

        var searchType = 'AllWords';

        if (jQuery('input[id*=SearchBoxAnyWordsRb]:checked').val())
            searchType = 'AnyWords';

        else if (jQuery('input[id*=SearchBoxExactPhraseRb]:checked').val())
            searchType = 'Phrase';

        Search(jQuery('.SearchBarGoBtn').attr('searchURL'), jQuery('.rd_nav_input').attr('id'),
                 searchType);

        return false;
    });


    jQuery('.SearchBoxDropDownBtn').bind('click', function(event) {
        jQuery('.SearchBarAdvancedSearchPnl').css({
            'z-index': 5000,
            'position': 'absolute',
            'top': jQuery('.rd_nav_input').position().top + 50,
            'left': jQuery('.rd_nav_input').position().left - 3
        }).slideDown('normal');
        jQuery('.SearchBarGoBtn').hide();
        return false;
    });

    jQuery('.hs_search_bar_close_button').bind('click', function(event) {
        jQuery('.SearchBarAdvancedSearchPnl').slideUp('normal');
        jQuery('.SearchBarGoBtn').show();
        return false;
    });

    jQuery('.rd_nav_input').bind('keypress', function(event) {
        if (event.which && event.which == 13) {
            Search(jQuery('.SearchBarGoBtn').attr('searchURL'), jQuery('.rd_nav_input').attr('id')
                , 'SS');
            return false;
        }
    });


    jQuery('.SearchBoxCatList').bind('change', function() {

        if (jQuery(this).val() == "")
            return false;
        window.location = jQuery(this).val();
    });

    jQuery('.SearchBoxAdvListDDL, .SearchBoxAdvList').bind('change', function() {

        if (jQuery(this).val() == "")
            return false;
        window.location = jQuery(this).val();
    });


    //Shopper
    jQuery('.hs_search_bar_go_button').bind('click', function(event) {

        Search(jQuery('.hs_search_bar_go_button').attr('searchurl'), jQuery('.hs_search_bar_input_field').attr('id')
                , jQuery('.hs_search_bar_mode_menu').val());

        return false;
    });

    jQuery('.hs_search_bar_input_field').bind('keypress', function(event) {
        if (event.which && event.which == 13) {
            Search(jQuery('.hs_search_bar_go_button').attr('searchURL'), jQuery('.hs_search_bar_input_field').attr('id')
                , jQuery('.hs_search_bar_mode_menu').val());
            return false;
        }
    });

    jQuery('.ItemListZipSearchBtn').bind('click', function() {
        return DistanceSearch(false);
    });

    jQuery('.ItemListAdvDistSearchBtn').bind('click', function() {
        return DistanceSearch(true);
    });

    jQuery('.ItemListZipCodeText').bind('keypress', function(event) {
        if (event.which && event.which == 13) {
            jQuery('.SearchBoxDropDownBtn').unbind();
            return DistanceSearch(false);
        }
    });

    jQuery('.ItemListStreetAddressTxt').bind('keypress', function(event) {
        if (event.which && event.which == 13) {
            jQuery('.SearchBoxDropDownBtn').unbind();
            return DistanceSearch(true);
        }
    });

    jQuery('.ItemListDisplayAdvBtn').bind('click', function() {
        if (jQuery('.ItemListZipSearchBtn').css("display") == 'inline') {
            jQuery('.ItemListAdvDistanceSearchPnl').slideDown('normal');
            jQuery('.ItemListZipSearchBtn').css("display", "none");
            jQuery(this).attr('src', '/image/searchbar/nav_Lite_slideup.jpg');
            jQuery(this).unbind('mouseenter').unbind('mouseleave');
            jQuery(this).parent().find('#texthover').remove().end().css('cursor', 'default');
        }
        else {
            jQuery('.ItemListAdvDistanceSearchPnl').slideUp('normal');
            jQuery('.ItemListZipSearchBtn').css("display", "inline");
            jQuery(this).attr('src', '/image/searchbar/nav_Lite_dd.jpg');

            jQuery(this).hover(function(event) {
                DisplayPopup(this, event, "Click here to add your street address");
            }, function() {
                jQuery(this).parent().find('#texthover').remove().end().css('cursor', 'default');
            });
        }
        return false;
    });

    jQuery('.ItemListDisplayAdvBtn').hover(function(event) {
        DisplayPopup(this, event, "Click here to add your street address");
    }, function() {
        jQuery(this).parent().find('#texthover').remove().end().css('cursor', 'default');
    });
});


function DistanceSearch(isStreetAddress) {
    var inError = false;
    var zipCodeFld = jQuery('.ItemListZipCodeText');
    var zipCode = zipCodeFld.val();
    if (zipCode == '' || zipCode == null) {
        if (jQuery('span#ItemListZipStar').length == 0)
            zipCodeFld.after("<span id='ItemListZipStar' style='color:red;cursor:default' title='Please enter zip code'>&nbsp;*</span>").focus();
        inError = true;
    }
    else
        jQuery('span#ItemListZipStar').remove();



    if (!IsNumeric(zipCode) || zipCode.indexOf('.') > -1) {
        if (jQuery('span#ItemListZipStar').length == 0)
            zipCodeFld.after("<span id='ItemListZipStar' style='color:red;cursor:default' title='Zip code must be numeric'>&nbsp;*</span>").focus();
        inError = true;
    }

    //Google call
    var address = null;
    if (isStreetAddress) {
        var streetAddressFld = jQuery('.ItemListStreetAddressTxt');
        var streetAddress = streetAddressFld.val();
        if (jQuery.trim(streetAddress).length == 0) {
            if (jQuery('span#ItemListStrAddrStar').length == 0)
                streetAddressFld.after("<span id='ItemListStrAddrStar' style='color:red;cursor:default' title='Please enter street address'>&nbsp;*</span>").focus();
            inError = true;
        }
        else
            jQuery('span#ItemListStrAddrStar').remove();
        address = streetAddress + ' ' + zipCode;
    }
    else
        address = zipCode;

    if (inError) return false;

    var geocoder = new GClientGeocoder();
    geocoder.getLatLng(address, function(point) {
        if (point == null) {
            if (jQuery('div#ItemListErrorPnl').length == 0)
                jQuery('.ItemListZipCodeSearchPnl').append("<br/><div id='ItemListErrorPnl' style='color:red'>Error processing Zip Code.</div>");
            return false;
        }
        if (jQuery('div#ItemListErrorPnl').length > 0)
            jQuery('div#ItemListErrorPnl').remove();

        var url = jQuery('.ItemListZipSearchBtn').attr('searchurl');
        if (url.indexOf('?') > -1 || url.indexOf('&') > -1)
            url += "&lat=" + point.y;
        else
            url += "?lat=" + point.y;
        url += "&lng=" + point.x;
        url += "&zip=" + zipCode;
        url += "&miles=" + jQuery('.ItemListMilesDDList').val();
        if (isStreetAddress)
            url += "&straddr=" + streetAddress;
        url += "&forcerewrite=true";

        document.forms['aspnetForm'].action = url;
        document.forms['aspnetForm'].submit();
    });

    return false;
}

function IsNumeric(data) {
    return parseFloat(data) == data;
}

function Search(searchURL, searchId, searchType) {

    //    alert(searchType);
    //    alert(searchId);
    switch (searchType) {
        case "SS":
            StandardSearch(searchURL, searchId, '', '', '', '', '');
            break;

        case "AllWords":
            AdvancedSearch(searchURL, searchId, '', '', '', 'AllWords', '', '', '');
            break;

        case "AnyWords":
            AdvancedSearch(searchURL, searchId, '', '', '', 'AnyWords', '', '', '');
            break;

        case "Phrase":
            AdvancedSearch(searchURL, searchId, '', '', '', 'Phrase', '', '', '');
            break;
    }
}

var xOffset = 15;
var yOffset = 10;
function DisplayPopup(element, event, text) {
    jQuery(element).parent().append(jQuery('<div id="texthover"></div>').css({
        position: 'absolute',
        left: event.pageX + 15,
        top: event.pageY + 10,
        cursor: 'hand',
        display: 'block',
        'text-align': 'left'
    }).html('<table><tr><td valign="top"><img src="/image/information.png"/></td><td style="padding-left:3px;padding-top:5px">' + text + '</td></tr></table>'));
}