﻿var mosaicIndex = [1, 1, 1];
var mosaicLimit = 3;
var mosaicCycleDelay = 6000;
var mosaicFadeSpeed = 3000;

var geocoder;
var map = [];
var mapBounds = [];

$(document).ready(function () {

    setTimeout('mosaicCycle(0, 0)', 3000);
    setTimeout('mosaicCycle(1, 3)', 6000);
    setTimeout('mosaicCycle(2, 6)', 9000);
  
    $('#navigation ul.level1 li.NavigationSelected').parent().parent().addClass('NavigationSelected');

    $('#navigation ul.level0 > li > a').mouseenter(function () {

        $(this).parent().find('ul.level1').stop().fadeTo('fast', 1);
    });

    $('#navigation ul.level0 > li').mouseleave(function () {

        $('ul.level1', this).stop().fadeTo('fast', 0);
    });

    // Map navigation

    $('#map-navigation li').click(function () {

        $('#map-navigation li').removeClass('selected');

        $(this).addClass('selected');

        $('#map').removeClass('selected');

        $('.map').addClass('hidden');

        $('#map-' + $('#map-navigation li').index(this)).removeClass('hidden');
    });

    // Google Maps

    if ($('#maps').length > 0) {

        geocoder = new google.maps.Geocoder();

        var myLatlng = new google.maps.LatLng(51.752068, -0.337374);

        var myOptions = {
            zoom: 12,
            center: myLatlng,
            mapTypeId: google.maps.MapTypeId.ROADMAP,
            mapTypeControl: false,
            streetViewControl: false
        }

        map[0] = new google.maps.Map(document.getElementById("map-0"), myOptions);
        mapBounds[0] = new google.maps.LatLngBounds();

        mapAddress(map[0], mapBounds[0], 'Rayden Solicitors', '21 Victoria Street, St Albans, Herts, AL1 3JJ', true, false);

        map[1] = new google.maps.Map(document.getElementById("map-1"), myOptions);
        mapBounds[1] = new google.maps.LatLngBounds();

        mapAddress(map[1], mapBounds[1], 'Regus London Holborn Gate', '1st Floor Holborn Gate, 330 High Holborn, London, WC1V 7QT', false, true);
        mapAddress(map[1], mapBounds[1], 'Regus London Victoria', 'Portland House Bressenden Place, London, SW1E 5RS', false, true);
        mapAddress(map[1], mapBounds[1], 'Regus London Westminster St James', '50 Broadway, London, SW1H 0RG', false, true);
        mapAddress(map[1], mapBounds[1], 'Regus London Hanover Square', '16 Hanover Square, Mayfair, London, W1S 1HT', false, true);
        mapAddress(map[1], mapBounds[1], 'Regus London Trafalgar Square', '1 Northumberland Avenue, Trafalgar Square, London, WC2N 5BW', false, true);
        mapAddress(map[1], mapBounds[1], 'Regus London City Point', '1 Ropemaker Street, London, EC2Y 9HT', false, true);

        //mapAddress(map[1], mapBounds[1], "SM1 2JB, UK", false, false);
    }

    $('.BlogAuthorDate').each(function () {

        $(this).html($(this).html().replace('&nbsp;', '').replace('- ', ' - '));
    });
});

function mosaicCycle(index, offset) {

    $('#banner .location-' + (mosaicIndex[index] + offset)).fadeOut(mosaicFadeSpeed, function () {

        if (mosaicIndex[index] == mosaicLimit)
            mosaicIndex[index] = 1;
        else
            mosaicIndex[index]++;

        $('#banner .location-' + (mosaicIndex[index] + offset)).fadeIn(mosaicFadeSpeed);

        setTimeout('mosaicCycle(' + index + ', ' + offset + ')', mosaicCycleDelay);
    });
}

function mapAddress(map, mapBounds, title, address, centre, fit) {

    geocoder.geocode({ 'address': address }, function (results, status) {

        if (status == google.maps.GeocoderStatus.OK) {

            mapBounds.extend(results[0].geometry.location);

            if (centre)
                map.setCenter(results[0].geometry.location);

            if (fit)
                map.fitBounds(mapBounds);

            var marker = new google.maps.Marker({
                map: map,
                position: results[0].geometry.location,
                title: title + ' - Click for larger map',
                clickable: true,
                icon: '/Frontend/Images/pin.png'
            });

            google.maps.event.addListener(marker, 'click', function (marker, i) {
                window.open('http://local.google.co.uk/maps?q=' + encodeURIComponent(address));
            });
        }
        else {

            alert("Geocode was not successful for the following reason: " + status);
        }
    });
}
