var locationMap = {};
var toggleFAQ = {};
var gmarkers = new Array;

$(document).ready(function() {

    $('h1').html('<a href="/" title="CRM London Students &raquo; Student Accommodation Management"><img src="/_img/logo.gif" alt="CRM London Students &raquo; Student Accommodation Management" title="CRM London Students &raquo; Student Accommodation Management" /></a>');
    $('h1').addClass('loaded');

    toggleFAQ = function(id) {
        $('#answer'+id).slideToggle();
    };

    function createMarker(point,g_index,info_name,marker_type) {
        var baseIcon = new GIcon();

        if (marker_type != 1) {
            baseIcon.shadow = "/_img/_markers/shadow.png";
            baseIcon.iconSize = new GSize(22, 22);
            baseIcon.shadowSize = new GSize(30, 23);
            baseIcon.iconAnchor = new GPoint(22, 22);
            baseIcon.infoWindowAnchor = new GPoint(9, 2);
            var icon = new GIcon(baseIcon);
            icon.image = "/_img/_markers/1.png";
        } else {
            baseIcon.shadow = "/_img/shadow.png";
            baseIcon.iconSize = new GSize(28, 36);
            baseIcon.shadowSize = new GSize(46, 36);
            baseIcon.iconAnchor = new GPoint(28, 36);
            baseIcon.infoWindowAnchor = new GPoint(9, 2);
            var icon = new GIcon(baseIcon);
            icon.image = "/_img/marker.png";
        }

        var marker = new GMarker(point,icon);
        gmarkers[g_index] = marker;
        GEvent.addListener(marker, "click", function() {
            marker.openInfoWindowHtml('<p style="font-family:arial;font-size:14px;font-weight:bold;">'+info_name+'</p>');
        });
        //GEvent.addListener(marker,"mouseover", function() {
        //    marker.openInfoWindowHtml('<p style="font-family:arial;font-size:14px;font-weight:bold;">'+info_name+'</p>');
        //});
        return marker;
    }

    /* Small map */
    if ($('#location-map-small').length > 0) {
        if (GBrowserIsCompatible()) {
            map = new GMap2(document.getElementById("location-map-small"));
            map.addControl(new GSmallMapControl());
            map.enableScrollWheelZoom();
            map.setCenter(new GLatLng(51.5163079, -0.0792207), 12);
            map.addOverlay(createMarker(new GLatLng(51.5162291,-0.0764286),0,'East India House<br /><span style="font-size:11px;font-weight:normal;">CULC London Campus</span>',1));
        }
    }

    /* Location Maps */
    if ($('#location-maps').length > 0) {

        $('a.poi').click(function() {
            var lat = $(this).attr('rel').split(',')[0];
            var lng = $(this).attr('rel').split(',')[1];
            var g_index = $(this).attr('rel').split(',')[2];
            map.setCenter(new GLatLng(lat,lng),15)
            GEvent.trigger(gmarkers[g_index], "click");
            return false;
        });

            if (GBrowserIsCompatible()) {
                map = new GMap2(document.getElementById("location-map"));
                map.addControl(new GSmallMapControl());
                map.enableScrollWheelZoom();
                map.setCenter(new GLatLng(51.5163079, -0.0792207), 15);
                map.addOverlay(createMarker(new GLatLng(51.5162291,-0.0764286),0,'East India House<br /><span style="font-size:11px;font-weight:normal;">CULC London Campus</span>',1));
                map.addOverlay(createMarker(new GLatLng(51.5173512,-0.0829657),1,'Liverpool Street Station',0));
                map.addOverlay(createMarker(new GLatLng(51.51357,-0.10013),2,'St Pauls Cathedral',0));
                map.addOverlay(createMarker(new GLatLng(51.51521,-0.08024),3,'The Gherkin',0));
                map.addOverlay(createMarker(new GLatLng(51.51268,-0.08233),4,'Lloyds Building',0));
                map.addOverlay(createMarker(new GLatLng(51.5140969,-0.086305),5,'Bank of England',0));
            }
        }

        /* Directions */

        $('input#postcode').focus(function() {
           if ($(this).val() == '10 Downing Street, London') {
               $(this).attr('value','');
           }
        });

        $('input#postcode').blur(function() {
           if ($(this).val() == '') {
               $(this).attr('value','10 Downing Street, London');
           }
        });

        $('form#directions-form').submit(function() {
            var pcode = $('input#postcode').val();
            if (pcode != '') {
                window.open("/directions.php?&postcode="+pcode,"directions","width=500,height=700,scrollbars=1");
            } else {
                window.alert("Please enter your location");
            }
            return false;
        });

        /* Property Directions */

        $('form#prop-directions-form').submit(function() {
            var lat = $('input#prop_lat').val();
            var lng = $('input#prop_lng').val();
            window.open('/directions-room.php?&lat='+lat+'&lng='+lng,'directions','width=500,height=700,scrollbars=1');
            return false;
        });


});

