var monuments = new Array();

$(document).ready(function() {
	$('.jqmWindow').jqm({
	    modal: true,
	    overlay: 70,
	    onHide: function(hash){
	        hash.o.remove();
	        hash.w.fadeOut('2000');
	    },
	    onShow: function(hash){
	        hash.w.fadeIn('2000');
	    }
	});

    var lahti = new google.maps.LatLng(60.983, 25.651);
    var map = new google.maps.Map($("#map_canvas").get(0), {
        backgroundColor: "transparent",
        zoom: 14,
        center: lahti,
        mapTypeId: google.maps.MapTypeId.ROADMAP,
        disableDefaultUI: true,
        scaleControl: true,
        navigationControl: true,
        navigationControlOptions: {
            position: google.maps.ControlPosition.RIGHT
        }
    });

    var icon = new google.maps.MarkerImage(
        '/images/icon.png',
        new google.maps.Size(31, 32),
        new google.maps.Point(0,0), //origin
        new google.maps.Point(12, 12) //anchor
    );
    var icon2 = new google.maps.MarkerImage(
        '/images/icon2.png',
        new google.maps.Size(31, 32),
        new google.maps.Point(0,0),
        new google.maps.Point(12, 12)
    );

    //Kartan muotoilu
    var styles = [{
      featureType: "all",
      elementType: "labels",
      stylers: [{visibility: "off"}]
    },
    {
      featureType: "road.local",
      elementType: "geometry",
      stylers: [{visibility: "off"}]
    },
    {
      featureType: "transit",
      elementType: "geometry",
      stylers: [{visibility: "off"}]
    },
    
    {
      featureType: "road",
      elementType: "geometry",
      stylers: [{hue: "#888"},{saturation: -100},{lightness: -15}]
    },
    {
      featureType: "landscape",
      elementType: "geometry",
      stylers: [{hue: "#dedbd5"}]
    },
    {
      featureType: "landscape.natural",
      elementType: "geometry",
      stylers: [{visibility: "off"}]
    },
    {
      featureType: "poi",
      elementType: "geometry",
      stylers: [{visibility: "off"}]
    },
    {
      featureType: "water",
      elementType: "geometry",
      stylers: [{hue: "#79a3ac"}]
    }];

    var styledMapOptions = {
      map: map,
      name: "Colored"
    }

    var mapType = new google.maps.StyledMapType(styles, styledMapOptions);
    map.mapTypes.set('colored', mapType);
    map.setMapTypeId('colored');


    addMonuments(map);


    //Aluerajoitus
    var lahdenAlue = new google.maps.LatLngBounds(new google.maps.LatLng(60.930247866274, 25.501053787231463), new google.maps.LatLng(61.04214816675415, 25.788414932250994));
    var lastPos = lahti;
    var minZoom = 12;
    var maxZoom = 16;
    google.maps.event.addListener(map, 'bounds_changed', function() {
        if(!lahdenAlue.contains(map.getCenter())) {
            map.panTo(lastPos);
        } else {
            lastPos = map.getCenter();
        }
    });
    google.maps.event.addListener(map, 'zoom_changed', function() {
         if(map.getZoom() < minZoom) {
            map.setZoom(minZoom);
        } else if(map.getZoom() > maxZoom) {
            map.setZoom(maxZoom);
        }
    });
    
    $(window).resize(function() {
        map.setCenter(lahti);
    });

    //Spottien lataaminen
    $.post("/index/xml", {}, function(xml){
        var selected = null;
        $(xml).find("spot").each(function() {
            var spot = $(this);
            
            var id = parseInt(spot.find('id').text());
            var title = spot.find('title').text();
            var location = spot.find('locat').text();
            var info = spot.find('info').text();
            var position = new google.maps.LatLng(
                parseFloat(spot.find("lat").text()),
                parseFloat(spot.find("lng").text())
            );
            var images = new Array();
            spot.find('image').each(function() {
                images.push($(this).text());
            });
            var rotate = spot.find('rotate').text();

            var imagebuttons = "";
            if(images.length > 0) {
                imagebuttons = '<div class="btn btnNumber" onclick="loadPano(\'' + images[0] + '\', ' + rotate + ', this)">1</div>';
                for(var i = 1; i < images.length; i++) {
                    imagebuttons += ' <div class="btn btnNumber" onclick="loadPano(\'' + images[i] + '\', 0, this)">' + (i+1) + '</div>';
                }
            }

            var tooltip = '<div class="tooltip"><h4>' + title + '</h4>' + location + '</div>';
            
            var marker = new google.maps.Marker({
                map: map,
                position: position,
                icon: icon
            });

            marker.attachTipTool(tooltip);
            
            google.maps.event.addListener(marker, 'mouseover', function() {
                marker.setIcon(icon2);
            });
            google.maps.event.addListener(marker, 'mouseout', function() {
                marker.setIcon(icon);
            });

            google.maps.event.addListener(marker, 'click', function() {
                $('#dialog').jqmShow();
                $('#spotTitle').html(title);
                $('#spotInfo').html(info);
                
                Cufon.replace('h2');
                
                if(images.length > 1) {
                    $('#xtraPanos').html(imagebuttons);
                } else {
                	$('#xtraPanos').empty();
                }
                if(images.length > 0) {
                    showPano(true);
                    loadPano(images[0], rotate);
                } else {
                    showPano(false);
                }
                
                window.location = window.location.pathname + '#' + id;
            });


            spotCell = spotTable.fnAddData([
                title,
                location
            ]);
            var nTr = spotTable.fnSettings().aoData[spotCell[0]].nTr;
            $(nTr).click(function(){
                google.maps.event.trigger(marker, 'click');
            });
            
            if(window.location.hash == '#' + id) {
                selected = marker;
            }
        });
        
        if(selected != null) {
            google.maps.event.trigger(selected, 'click');
        }
    });    
});

function addMonuments(map)
{
    //Kerrostalot
    monuments.push(new Monument(
        map,
        '/images/icons/kerrostalo4.png', //image
        new google.maps.LatLng(60.987162, 25.659892), //position
        new google.maps.Point(27, 47), //image anchor point (optional)
        new google.maps.Size(55, 67), //image size (optional)
        0.33 //image scale (optional)
    ));
	monuments.push(new Monument( map, '/images/icons/kerrostalo4.png', new google.maps.LatLng(60.988495, 25.664032), new google.maps.Point(27, 47), new google.maps.Size(55, 67), 0.3));
	monuments.push(new Monument( map, '/images/icons/kerrostalo5.png', new google.maps.LatLng(60.987746, 25.665405), new google.maps.Point(33, 55), new google.maps.Size(69, 84), 0.3));
	monuments.push(new Monument( map, '/images/icons/kerrostalo4.png', new google.maps.LatLng(60.991992, 25.661214), new google.maps.Point(27, 47), new google.maps.Size(55, 67), 0.33));
	monuments.push(new Monument( map, '/images/icons/kerrostalo4.png', new google.maps.LatLng(60.991242, 25.659154), new google.maps.Point(27, 47), new google.maps.Size(55, 67), 0.33));
	monuments.push(new Monument( map, '/images/icons/kerrostalo4.png', new google.maps.LatLng(60.981168, 25.664132), new google.maps.Point(27, 47), new google.maps.Size(55, 67), 0.3));
	monuments.push(new Monument(map, '/images/icons/kerrostalo5.png', new google.maps.LatLng(60.980335, 25.667050), new google.maps.Point(33, 55), new google.maps.Size(69, 84), 0.3));
	monuments.push(new Monument(map, '/images/icons/kerrostalo4.png', new google.maps.LatLng(60.979851, 25.669797), new google.maps.Point(27, 47), new google.maps.Size(55, 67), 0.3));
	monuments.push(new Monument(map, '/images/icons/kerrostalo5.png', new google.maps.LatLng(60.970924, 25.628083), new google.maps.Point(33, 55), new google.maps.Size(69, 84), 0.33));
	monuments.push(new Monument(map, '/images/icons/kerrostalo5.png', new google.maps.LatLng(60.969799, 25.625766), new google.maps.Point(33, 55), new google.maps.Size(69, 84), 0.33));
	monuments.push(new Monument(map, '/images/icons/kerrostalo5.png', new google.maps.LatLng(60.970466, 25.634006), new google.maps.Point(33, 55), new google.maps.Size(69, 84), 0.33));
	monuments.push(new Monument(map, '/images/icons/kerrostalo5.png', new google.maps.LatLng(60.969549, 25.636409), new google.maps.Point(33, 55), new google.maps.Size(69, 84), 0.33));
	monuments.push(new Monument(map, '/images/icons/kerrostalo1.png', new google.maps.LatLng(61.004767, 25.641902), new google.maps.Point(35, 41), new google.maps.Size(72, 69), 0.33));
	monuments.push(new Monument(map, '/images/icons/kerrostalo1.png', new google.maps.LatLng(61.003852, 25.643876), new google.maps.Point(35, 41), new google.maps.Size(72, 69), 0.33));
	monuments.push(new Monument(map, '/images/icons/kerrostalo1.png', new google.maps.LatLng(61.00302, 25.645936), new google.maps.Point(35, 41), new google.maps.Size(72, 69), 0.33));
	monuments.push(new Monument(map, '/images/icons/kerrostalo3.png', new google.maps.LatLng(60.98470, 25.665849), new google.maps.Point(44, 52), new google.maps.Size(90, 87), 0.3));
	monuments.push(new Monument(map, '/images/icons/kerrostalo2.png', new google.maps.LatLng(60.98328, 25.6649), new google.maps.Point(59, 66), new google.maps.Size(116, 111), 0.3));
	monuments.push(new Monument(map, '/images/icons/kulmatalo.png', new google.maps.LatLng(60.978462, 25.663274), new google.maps.Point(59, 53), new google.maps.Size(131, 97), 0.33));
	
	//Omakotitalot
	monuments.push(new Monument(map, '/images/icons/omakotitalo1.png', new google.maps.LatLng(60.971382, 25.619672), new google.maps.Point(35, 34), new google.maps.Size(70, 62), 0.33));
	monuments.push(new Monument(map, '/images/icons/omakotitalo3.png', new google.maps.LatLng(60.971632, 25.616410), new google.maps.Point(35, 34), new google.maps.Size(70, 62), 0.33));
	monuments.push(new Monument(map, '/images/icons/omakotitalo1.png', new google.maps.LatLng(60.970382, 25.675977), new google.maps.Point(35, 34), new google.maps.Size(70, 62), 0.33));
	monuments.push(new Monument(map, '/images/icons/omakotitalo1.png', new google.maps.LatLng(60.970049, 25.677178), new google.maps.Point(35, 34), new google.maps.Size(70, 62), 0.33));
	monuments.push(new Monument(map, '/images/icons/omakotitalo1.png', new google.maps.LatLng(60.975631, 25.670484), new google.maps.Point(35, 34), new google.maps.Size(70, 62), 0.33));
	monuments.push(new Monument(map, '/images/icons/omakotitalo2.png', new google.maps.LatLng(60.975214, 25.671857), new google.maps.Point(35, 34), new google.maps.Size(70, 62), 0.33));
	monuments.push(new Monument(map, '/images/icons/omakotitalo1.png', new google.maps.LatLng(60.974797, 25.673235), new google.maps.Point(35, 34), new google.maps.Size(70, 62), 0.33));
	monuments.push(new Monument(map, '/images/icons/omakotitalo3.png', new google.maps.LatLng(60.975463, 25.667222), new google.maps.Point(35, 34), new google.maps.Size(70, 62), 0.33));
	monuments.push(new Monument(map, '/images/icons/omakotitalo3.png', new google.maps.LatLng(61.001022, 25.666535), new google.maps.Point(35, 34), new google.maps.Size(70, 62), 0.25));
	monuments.push(new Monument(map, '/images/icons/omakotitalo1.png', new google.maps.LatLng(60.997111, 25.67426), new google.maps.Point(35, 34), new google.maps.Size(70, 62), 0.25));
	monuments.push(new Monument(map, '/images/icons/omakotitalo1.png', new google.maps.LatLng(60.99686, 25.675118), new google.maps.Point(35, 34), new google.maps.Size(70, 62), 0.25));
	
	//Puut
	monuments.push(new Monument(map, '/images/icons/puita2.png', new google.maps.LatLng(60.986455, 25.621045), new google.maps.Point(91, 52), new google.maps.Size(183, 104), 0.33));
	monuments.push(new Monument(map, '/images/icons/puita3.png', new google.maps.LatLng(60.984457, 25.611089), new google.maps.Point(91, 52), new google.maps.Size(183, 104), 0.33));
	monuments.push(new Monument(map, '/images/icons/puita.png', new google.maps.LatLng(60.983708, 25.611089), new google.maps.Point(73, 61), new google.maps.Size(158, 131), 0.33));
	monuments.push(new Monument(map, '/images/icons/puita4.png', new google.maps.LatLng(60.985789, 25.683873), new google.maps.Point(45, 35), new google.maps.Size(84, 70), 0.33));
	monuments.push(new Monument(map, '/images/icons/puita5.png', new google.maps.LatLng(60.983375, 25.673058), new google.maps.Point(45, 35), new google.maps.Size(84, 70), 0.33));
	
	//Autot
	monuments.push(new Monument(map, '/images/icons/car1.png', new google.maps.LatLng(60.982001, 25.666836), new google.maps.Point(17, 13), new google.maps.Size(34, 25), 0.33));
	monuments.push(new Monument(map, '/images/icons/car2.png', new google.maps.LatLng(60.973339, 25.675033), new google.maps.Point(17, 13), new google.maps.Size(34, 25), 0.33));
	monuments.push(new Monument(map, '/images/icons/car3.png', new google.maps.LatLng(60.983812, 25.649863), new google.maps.Point(17, 13), new google.maps.Size(34, 25), 0.33));
	
	//Botskit
	monuments.push(new Monument(map, '/images/icons/vene1.png', new google.maps.LatLng(60.991534, 25.636323), new google.maps.Point(23, 16), new google.maps.Size(41, 33), 0.33));
	monuments.push(new Monument(map, '/images/icons/vene2.png', new google.maps.LatLng(60.993781, 25.6102304), new google.maps.Point(23, 16), new google.maps.Size(41, 33), 0.33));
	monuments.push(new Monument(map, '/images/icons/vene3.png', new google.maps.LatLng(60.993282, 25.643275), new google.maps.Point(31, 20), new google.maps.Size(56, 45), 0.33));
	monuments.push(new Monument(map, '/images/icons/vene4.png', new google.maps.LatLng(60.994114, 25.644391), new google.maps.Point(31, 20), new google.maps.Size(56, 45), 0.33));
	monuments.push(new Monument(map, '/images/icons/vene5.png', new google.maps.LatLng(60.994863, 25.645249), new google.maps.Point(31, 20), new google.maps.Size(56, 45), 0.33));
	monuments.push(new Monument(map, '/images/icons/vene6.png', new google.maps.LatLng(60.992616, 25.620015), new google.maps.Point(25, 20), new google.maps.Size(56, 45), 0.33));

  	//Kohteet
  	monuments.push(new Monument(map, '/images/icons/mastot.png', new google.maps.LatLng(60.9798, 25.64665), new google.maps.Point(84, 200), new google.maps.Size(160, 257), 0.33));
  	monuments.push(new Monument(map, '/images/icons/oppilastalot.png', new google.maps.LatLng(60.995303, 25.656549), new google.maps.Point(65, 91), new google.maps.Size(131, 136), 0.33));
  	monuments.push(new Monument(map, '/images/icons/sibeliushall.png', new google.maps.LatLng(60.99355, 25.65259), new google.maps.Point(75, 90), new google.maps.Size(150, 150), 0.33));
  	monuments.push(new Monument(map, '/images/icons/puisto.png', new google.maps.LatLng(60.972777, 25.643146), new google.maps.Point(143, 94), new google.maps.Size(298, 217), 0.33));
  	monuments.push(new Monument(map, '/images/icons/swing.png', new google.maps.LatLng(60.97113, 25.642640), new google.maps.Point(71, 75), new google.maps.Size(138, 112), 0.33));
  	monuments.push(new Monument(map, '/images/icons/hyppytornit.png', new google.maps.LatLng(60.984539, 25.635516), new google.maps.Point(138, 145), new google.maps.Size(288, 257), 0.33));
  	monuments.push(new Monument(map, '/images/icons/historiallinenmuseo.png', new google.maps.LatLng(60.986454, 25.650623), new google.maps.Point(54, 81), new google.maps.Size(116, 127), 0.33));
  	monuments.push(new Monument(map, '/images/icons/vesitorni.png', new google.maps.LatLng(60.991907, 25.671052), new google.maps.Point(28, 53), new google.maps.Size(54, 83), 0.6));
  	monuments.push(new Monument(map, '/images/icons/pikkuveskupuisto.png', new google.maps.LatLng(60.989827, 25.649198), new google.maps.Point(60, 78), new google.maps.Size(140, 154), 0.33));
  	monuments.push(new Monument(map, '/images/icons/urut.png', new google.maps.LatLng(60.987911, 25.647189), new google.maps.Point(44, 138), new google.maps.Size(86, 168), 0.33));
  	monuments.push(new Monument(map, '/images/icons/pianopaviljonki.png', new google.maps.LatLng(60.996278, 25.650571), new google.maps.Point(56, 50), new google.maps.Size(118, 91), 0.33));
	monuments.push(new Monument(map, '/images/icons/ristinkirkko.png', new google.maps.LatLng(60.985496, 25.659206), new google.maps.Point(78, 84), new google.maps.Size(156, 142), 0.33));
	monuments.push(new Monument(map, '/images/icons/tori.png', new google.maps.LatLng(60.9837, 25.657), new google.maps.Point(44, 51), new google.maps.Size(89, 86), 0.33));
	monuments.push(new Monument(map, '/images/icons/kaupungintalo.png', new google.maps.LatLng(60.981583, 25.655601), new google.maps.Point(96, 75), new google.maps.Size(193, 161), 0.33));
	monuments.push(new Monument(map, '/images/icons/teatteri.png', new google.maps.LatLng(60.985496, 25.671909), new google.maps.Point(92, 87), new google.maps.Size(221, 179), 0.33));
	monuments.push(new Monument(map, '/images/icons/umbrella.png', new google.maps.LatLng(60.977502, 25.63586), new google.maps.Point(37, 51), new google.maps.Size(78, 70), 0.33));
	monuments.push(new Monument(map, '/images/icons/umbrella2.png', new google.maps.LatLng(61.01446, 25.644305), new google.maps.Point(14, 89), new google.maps.Size(91, 120), 0.33));
	monuments.push(new Monument(map, '/images/icons/marketti.png', new google.maps.LatLng(60.971923, 25.664304), new google.maps.Point(92, 72), new google.maps.Size(196, 168), 0.33));
  	monuments.push(new Monument(map, '/images/icons/laituri1.png', new google.maps.LatLng(60.994863, 25.614951), new google.maps.Point(7, 9), new google.maps.Size(71, 55), 0.33));
	monuments.push(new Monument(map, '/images/icons/laituri2.png', new google.maps.LatLng(60.992949, 25.613063), new google.maps.Point(11, 13), new google.maps.Size(113, 87), 0.33));
}
