// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults

var BZ = (function() {
    var LOCATION_ERROR = "<p>Bookzee was unable to determine your location automatically.<br />Please enter your location above.<p>";
	
	var bz = {
		Location: (function() {
            return {
                found: function(data) {
                    $('input#user-location').val('location found')
                    
                },
                notFound: function() {
                    BZ.Location.showError(LOCATION_ERROR);
                },
                
                showError: function(msg) {
                    $('#book-search-errors').html(msg).slideDown().animate({
                        padding: '5px 8px'
                    });
                },
                
                detect: function() {
                    /*
                     * determine if the handset has client side geo location capabilities
                     */
                    if(geo_position_js.init()) {
                       //$.blockUI({ message: '<strong>Bookzee is trying to find your location, please wait...</strong>'});
                       
                       geo_position_js.getCurrentPosition(function(pos) {
                           //console.log('getCurrentPosition succeeded');
                           try {
                               $.ajax({
                                   url: '/user_addresses/create.json',
                                   data: {
                                       'q': (pos.coords.longitude + ',' + pos.coords.latitude)
                                   },
                                   contentType: 'application/json',
                                   method: 'GET',
                                   dataType: 'json',
                                   success: function(data){
                                       //console.log('ajax succeeded');
                                       $.unblockUI();
                                       
                                       if (data.success === true) {
                                           BZ.Location.found(data);
                                       } else {
                                           BZ.Location.notFound(data);
                                       }
                                       
                                       
                                   },
                                   error: function(xhr, stat, err){
                                       //console.log('ajax error');
                                       //$.unblockUI();
                                       BZ.Location.notFound();
                                       $.unblockUI();
                                   }
                               });
                           } 
                           catch (e) {
                            console.log('getCurrentPosition catch');
                           }
                       }, 
                       function(err) {
                           //console.log('no geolocation');
                           $.unblockUI();                           
                           BZ.Location.notFound();
                           
                       });
                    }
                    else{
                       
                    }
                }
            };
        })(),
        
        Util: (function() {
            var useragent = navigator.userAgent;
            
            var util = {
                isMobile: function() {
                    return (useragent.indexOf('iPhone') != -1 || useragent.indexOf('Android') != -1);
                }
            };
            
            return util;
        })()
	};
	
	return bz;
})();



(function($) {
    
    
    $.fn.defaultText = function() {
        function blur(el){
            if (el.val() === "" || el.val() === el.attr('title')) {
                el.addClass('default-text').val(el.attr('title'));
            }
        }
        function focus(el) {
            el.removeClass('default-text');
            
            if (el.val() === el.attr('title')) {
                el.val('');
            }
        }
        
        return $(this).each(function() {
            var $this = $(this);
            
            $this.blur(function() {
                blur($this)
            })
            .focus(function() {
                focus($this);
            });
            
            // Initial load
            blur($this);
        });
    };
    
    /**
     * Wrap element's contents with a <span>
     */
    $.fn.insertSpan = function() {
        return $(this).each(function() {
            var el = $(this),
                h  = el.html();
                
                el.html('').append('<span>' + h + '</span>');
            
        });
    };
    
    $(function() {
        /*
         * Default tezt for input fields
         */
        $('.book_search input[type=text]').defaultText();
        $('h1:first:not(.book-title)').insertSpan();
        
        //BZ.Location.detect();        
    });


})(jQuery);
