var genareas;

var addRoom = {
    geocoder: '',
    map: '',
    marker: '',
    construct: function(){
        this.geocoder = new google.maps.Geocoder();
        addRoom.showMap();
        $('#prop-country').change(function(){
            addRoom.getCounties(this);
        });
        $('#prop-county').change(function(){
            addRoom.getTowns(this);
        });
        $('#prop-town').change(function(){
            addRoom.geocodeMap()
        });
        $('#availableFrom').datepicker({
            dateFormat: 'dd/mm/yy'
        });
        $('#roomForm').validate({
            invalidHandler: function(form, validator){
                $('#errorbox').remove();
                $('#roomForm').before('<div id="errorbox" class="error margin strong">There was problems with your form.</div>');
            }
        });
        $('#postcodefind').click(function(event){
            event.preventDefault();
            addRoom.geocodePostcode();
        })
    },
    
    showMap: function(){
        var latlng = new google.maps.LatLng(52.0, -1.5);
        var myOptions = {
            zoom: 6,
            center: latlng,
            mapTypeId: google.maps.MapTypeId.ROADMAP
        };
        
        addRoom.map = new google.maps.Map(document.getElementById("map-canvas"), myOptions);
        addRoom.marker = new google.maps.Marker({
            map: addRoom.map,
            draggable: true
        });
        google.maps.event.addListener(addRoom.marker, 'mouseup', function(){
            var pos = addRoom.marker.getPosition();
            var lat = pos.lat();
            var lng = pos.lng();
            $('#lat').val(lat);
            $('#lng').val(lng);
        });
    },
    
    getCounties: function(cid){
        var countryid = $(cid).val();
        $.get("/ajax/showareas.php", {
            country: countryid
        }, function(data){
            $('#prop-town').html('<option> - Select A Town - </option>');
            $('#prop-county').html(data);
        });
    },
    
    getTowns: function(cid){
        var countyid = $(cid).val();
        $.get("/ajax/showareas.php", {
            county: countyid
        }, function(data){
            $('#prop-town').html(data);
        });
    },
    
    updateMarker: function(lat, lng){
        addRoom.marker.setPosition(new google.maps.LatLng(lat, lng));
        addRoom.map.setCenter(new google.maps.LatLng(lat, lng));
        addRoom.map.setZoom(14);
        if (lat === 53 && lng === -2) {
            addRoom.map.setZoom(5);
        }
    },
    
    geocodeMap: function(){
        var town = $('#prop-town option:selected').text();
        var county = $('#prop-county option:selected').text();
        var country = $('#prop-country option:selected').text();
        var address = town + ', ' + county + ', ' + country;
        if (this.geocoder) {
            this.geocoder.geocode({
                'address': address
            }, function(results, status){
                if (status == google.maps.GeocoderStatus.OK) {
                    addRoom.map.setCenter(results[0].geometry.location);
                    addRoom.map.setZoom(14);
                    latlng = results[0].geometry.location;
                    $('#lat').val(latlng.lat());
                    $('#lng').val(latlng.lng());
                    if (addRoom.marker instanceof Object) {
                        addRoom.marker.setPosition(results[0].geometry.location);
                    }
                    else {
                        addRoom.marker = new google.maps.Marker({
                            map: addRoom.map,
                            position: results[0].geometry.location,
                            draggable: true
                        });
                    }
                    
                }
                else {
                    alert("Geocode was not successful for the following reason: " + status);
                }
            });
        }
    },
    
    geocodePostcode: function(){
        var postcode = $('#postcode').val();
        if (this.geocoder) {
            this.geocoder.geocode({
                'address': postcode + ',uk',
                'region': 'GB'
            }, function(results, status){
            
                if (status == google.maps.GeocoderStatus.OK) {
                    addRoom.map.setCenter(results[0].geometry.location);
                    addRoom.map.setZoom(15);
                    latlng = results[0].geometry.location;
                    $('#lat').val(latlng.lat());
                    $('#lng').val(latlng.lng());
                    $('#marker-hint').slideDown(500);
                    if (addRoom.marker instanceof Object) {
                        addRoom.marker.setPosition(results[0].geometry.location);
                    }
                    else {
                        addRoom.marker = new google.maps.Marker({
                            map: addRoom.map,
                            position: results[0].geometry.location,
                            draggable: true
                        });
                    }
                }
                else {
                    alert("Geocode was not successful for the following reason: " + status);
                }
            });
        }
    }
}

var tenant = {
    geocoder: '',
    map: '',
    marker: '',
    towns: [],
    
    Contruct: function(){
        this.geocoder = new google.maps.Geocoder();
        $('#prop-country').change(function(){
            tenant.getCounties(this);
        });
        $('#prop-county').change(function(){
            tenant.getTowns(this);
        });
        $('#prop-towns').autocomplete({
            source: tenant.towns
        });
        $('#showMap').hide();
        if($('#prop-towns').val() !== '') {
            $('#showMap').show();
        }
        $('#prop-towns').bind('blur change', function(){
            if($('#prop-towns').val() !== '') {
                $('#showMap').show();
            }
            
        });
        $('#showMap').click(function(){
            var county = $('#prop-county option:selected').text();
            var town = $('#prop-towns').val();
            if(town && county !== '- Please Select A County -'){
                $('#mapstatus').removeClass('success').addClass('notice')
                .html('Drag the marker to the center of your search location. Then Click \'Set Mark Here\', to update your position.');
               tenant.geocodeMap(town + ',' + county);
               $('#tenantMapWrap').css({
                position: 'relative',
                left: 0
            });
            $('#tenantMapWrap').dialog({
                width: 600,
                height: 400,
                draggable: false,
                resizable: false,
                title: 'Place Marker',
                modal: true,
                buttons: {
                    'Set Marker Here': function(){
                        tenant.map.setCenter(tenant.marker.position);
                        tenant.map.setZoom(15);
                        $('#lat').val(tenant.marker.position.lat());
                        $('#lng').val(tenant.marker.position.lng());
                        $('#mapstatus').removeClass('notice').addClass('success thicker')
                        .html('Search Location Has Been Updated.');
                    },
                    Close: function(){
                        $(this).dialog('close');
                    }
                },
                hide: 'clip'
            });
            } else {
                alert('Please Select A County');
            }
            
            
            
        });
        var latlng = new google.maps.LatLng(52.0, -1.5);
        var myOptions = {
            zoom: 6,
            center: latlng,
            mapTypeId: google.maps.MapTypeId.ROADMAP
        };
        
        tenant.map = new google.maps.Map(document.getElementById("tenantMap"), myOptions);
        tenant.marker = new google.maps.Marker({
            map: tenant.map,
            draggable: true
        });
    },
    
    getCounties: function(cid){
        var countryid = $(cid).val();
        $.get("/ajax/showareas.php", {
            country: countryid
        }, function(data){
            $('#prop-county').html(data);
        });
    },
    
    getTowns: function(cid){
        var countyid = $(cid).val();
        $.getJSON("/ajax/showareas.php", {
            county: countyid,
            json: 1
        }, function(data){
            tenant.towns = data;
            $("#prop-towns").autocomplete('destroy');
            $("#prop-towns").autocomplete({
                source: data,
                minLength: 3,
                select: function(event, ui){
                    var county = $('#prop-county option:selected').text();
                    var town = ui.item.value;
                    tenant.geocodeMap(town + ',' + county);
                }
            });
            $('#prop-towns').attr('disabled', '');
        });
    },
    
    geocodeMap: function(area){
        tenant.geocoder.geocode({
            'address': area + ', UK'
        }, function(results, status){
            if (results) {
                var latlng = results[0].geometry.location;
                tenant.map.setCenter(latlng);
                tenant.map.setZoom(15);
                tenant.marker.setPosition(latlng);
                $('#lat').val(latlng.lat());
                $('#lng').val(latlng.lng());
            } else {
                alert('yest look');
                var latlng = new google.maps.LatLng(52.0, -1.5);
                tenant.map.setCenter(latlng);
                tenant.map.setZoom(15);
                tenant.marker.setPosition(latlng);
                $('#lat').val(latlng.lat());
                $('#lng').val(latlng.lng());
            }
        });
    },
    
    loadMarker: function(lat, lng){
        var latlng = new google.maps.LatLng(lat, lng);
        tenant.marker.setPosition(latlng);
        tenant.map.setCenter(latlng);
        tenant.map.setZoom(14);
    },
    
    loadTowns: function(countyid){
        $.getJSON("/ajax/showareas.php", {
            county: countyid,
            json: 1
        }, function(data){
            tenant.towns = data;
            $("#prop-towns").autocomplete('destroy');
            $("#prop-towns").autocomplete({
                source: data,
                minLength: 3,
                select: function(event, ui){
                    var county = $('#prop-county option:selected').text();
                    var town = ui.item.value;
                    tenant.geocodeMap(town + ',' + county);
                }
            });
            $('#prop-towns').attr('disabled', '');
        });
    }
}

var property = {
    map: '',
    marker: '',
    latlng: '',
    Construct: function(){
        $('#propertyTabs').tabs();
        $('#register').click(function(){
            property.register(event);
        });
        $('#propertyTabs').bind('tabsshow', function(event, ui){
            if (ui.panel.id == "map") {
                google.maps.event.trigger(property.map, 'resize');
                property.map.setCenter(property.latlng);
                property.map.setZoom(14);
                //resizeMap();
            }
        });
        $(".gallery:first a[rel^='prettyPhoto']").prettyPhoto({
            animationSpeed: 'slow',
            theme: 'light_square',
            slideshow: 4000,
            autoplay_slideshow: false
        });
        
    },
    
    register: function(event){
    
    },
    
    addMap: function(lat, lng){
        property.latlng = new google.maps.LatLng(lat, lng);
        var myOptions = {
            zoom: 13,
            center: property.latlng,
            mapTypeId: google.maps.MapTypeId.ROADMAP,
            center: property.latlng
        };
        
        property.map = new google.maps.Map(document.getElementById("map"), myOptions);
        property.marker = new google.maps.Marker({
            map: property.map,
            draggable: false,
            position: property.latlng
        });
        
        property.marker.setPosition(property.latlng);
        property.map.setCenter(property.latlng);
        property.map.setZoom(14);
    }
    
    
}

