/* 
Copyright © 2010 to Present, Local Matters, Inc.
All rights reserved.
 */

/*
 * $Id$
 * $URL$
 * Functions for city guide search form
 */

/*jslint browser: true, undef: true, evil: false
    onevar: true, debug: false, on: false, eqeqeq: true */
/*global LMI, YAHOO, window*/

(function() {
    var $ = LMI.Element.getOne,
        Y = YAHOO.util,
        $E = Y.Event,
        form = document.getElementById( 'searchForm' ),
        baseUrl = LMI.Data.baseUrl.replace(/;.*/, '').replace(/\/$/, '');

    /**
     * Create a pretty URL from the given what and where, and redirect to it.
     * @param {String} what The value of the what field
     * @param {String} where The value of the where field
     */
    function prettifySearchUrl( what, where, distance ) {
        var urlKey, dims, ll, ur, zoom;
        // Encode the what and where; if either is empty substitute '-':
        what = what.length > 0 ? LMI.PrettyUrlEncoder.encode( what ) : '-';
        where = where.length > 0 ? LMI.PrettyUrlEncoder.encode( where ) : '-';

        if( document.body.id === 'findOnMap' ) {
            urlKey = "js.url." + LMI.Data.guideType + ".map.search.results";
            if( where === '-' ) {
                ll = LMI.MapSearch.getMap().getLLPoint();
                ur = LMI.MapSearch.getMap().getURPoint();
                zoom = LMI.MapSearch.getMap().tileManager.getScaledZoomLevel();

                //send bounding box if no where
                window.location = LMI.Urls.get( LMI.Strings.getString( urlKey, what, where ) ) +
                    '?bottomLeftLatitude='+ll.lat+'&bottomLeftLongitude='+ll.lng+'&topRightLatitude='+ur.lat+'&topRightLongitude='+ur.lng+'&mapLevel='+zoom;
            } else {
                dims = LMI.MapSearchSize.getDimensions();
                window.location = LMI.Urls.get( LMI.Strings.getString( urlKey, what, where ) ) + '?mapWidth='+dims.x+'&mapHeight='+dims.y+'&mapLevel=0';
            }
        } else if( document.body.id === 'mapResults' ) {
            urlKey = "js.url." + LMI.Data.guideType + ".map";
            window.location = LMI.Urls.get( LMI.Strings.getString( urlKey, where, what ) );
        } else {
            //TODO: add 3rd param
            if ( distance && $(".searchForm").className.indexOf('radiusSearch') !== -1) {
                urlKey = "js.url." + LMI.Data.guideType + ".search.radius";
                window.location = LMI.Urls.get( LMI.Strings.getString( urlKey, where, what, distance ) );
            }
            else if ( document.getElementById( 'allCategorySearch' ).value === "true" ) {
                urlKey = "js.url." + LMI.Data.guideType + ".category.search";
                window.location = LMI.Urls.get( LMI.Strings.getString( urlKey, where, what ) );
            }
            else {
                urlKey = "js.url." + LMI.Data.guideType + ".search";
                window.location = LMI.Urls.get( LMI.Strings.getString( urlKey, where, what ) );
            }
        }
    }

    /**
     * Handle the form submit
     */
    function submitHandler( e ) {
        var form = this;
        //time out is needed in IE so that removing the drop down hints takes place before prettifying url.
        window.setTimeout( function() {
            prettifySearchUrl( form.what.value, form.where.value, form.distance.options[form.distance.selectedIndex].value );
        }, 0 );
        $E.stopEvent( e );
    }

    function init() {
        // Capture form submit event
        $E.on( form, 'submit', submitHandler );
    }

    LMI.Init.addFunction( init, 70 );

    function quickSearchHandler( e ) {
        $E.stopEvent( e );

        var url = new LMI.Url( this.href ),
            map = LMI.MapSearch.getMap(),
            ll = map.getLLPoint(),
            ur = map.getURPoint();

        url.addQueryValue( 'bottomLeftLatitude', ll.lat );
        url.addQueryValue( 'bottomLeftLongitude', ll.lng );
        url.addQueryValue( 'topRightLatitude', ur.lat );
        url.addQueryValue( 'topRightLongitude', ur.lng );

        url.addQueryValue( 'mapLevel', map.tileManager.getScaledZoomLevel() );

        window.location = url.getUrl();
    }
    LMI.LinkBehavior.add( 'quickSearch', quickSearchHandler );
    if( $('#featuredRestaurant') ) {
        LMI.WidgetStyling.init( $('#featuredRestaurant'), baseUrl + '/img/background-featured_restaurant.png' );
    }
})();
