$(document).ready(function() {
    googleMapApiReady();
});

$(function(){
	mapHeight = parseInt($(".noOffersBg").parent().height());
	$(".noOffersBg").height(mapHeight + "px");
});
var googleMapsObjects = new Array();
var googleMapsObjectsLatitudes = new Array();;
var googleMapsObjectsLongitudes = new Array();;

function googleMapApiReady(){
	if($('#map_canvas').length == 0) {
        return;
    }

    var myZoom = 6;
    var centerLat = 49.75;
    var centerLng = 15.50;

    if ((typeof(googleMapsObjects) == 'object') && (googleMapsObjects.length > 0)) {
        $('#map_canvas_background').hide();
        $('#map_canvas_text').hide();

        var minLatitude = Math.min.apply(Math, googleMapsObjectsLatitudes);
        var maxLatitude = Math.max.apply(Math, googleMapsObjectsLatitudes);
        var minLongitude = Math.min.apply(Math, googleMapsObjectsLongitudes);
        var maxLongitude = Math.max.apply(Math, googleMapsObjectsLongitudes);

        var latDiff = maxLatitude - minLatitude;
        var lngDiff = maxLongitude - minLongitude;

        var lngLatDiff = Math.max(latDiff, lngDiff);

        centerLat = (minLatitude + maxLatitude) / 2;
        centerLng = (minLongitude + maxLongitude) / 2;

        myZoom = 13;
        // TODO overwrite
        if (lngLatDiff > 0.075) {
            myZoom = 12;
        }
        if (lngLatDiff > 0.12) {
            myZoom = 11;
        }
        if (lngLatDiff > 0.15) {
            myZoom = 10;
        }
        if (lngLatDiff > 0.4) {
            myZoom = 9;
        }
        if (lngLatDiff > 0.8) {
            myZoom = 8;
        }
        if (lngLatDiff > 2) {
            myZoom = 7;
        }
        if (lngLatDiff > 4) {
            myZoom = 6;
        }
    }

    //console.log(myZoom);
    //console.log(lngLatDiff);

	var myLatlng = new google.maps.LatLng(centerLat, centerLng);
	var myOptions = {
			zoom: myZoom,
			center: myLatlng,
			mapTypeId: google.maps.MapTypeId.ROADMAP,
            mapTypeControl: false
	}
	map = new google.maps.Map($('#map_canvas').get(0), myOptions);

    google.maps.event.addListener(map, 'zoom_changed', function() {
        zoomLevel = map.getZoom();
        if (zoomLevel > 14) {
            map.setZoom(14);
        }
        if (zoomLevel < 6) {
            map.setZoom(6);
        }
    });

    var markers = new Array();

    for (id in googleMapsObjects) {
        var object = googleMapsObjects[id];

        var latLng = new google.maps.LatLng(object.latitude, object.longitude);

        markers[id] = new google.maps.Marker({
            id: id,
            map: map,
            clickable: true,
            draggable: false,
            position: latLng,
            onclick: function() {openInfoWindow(this);}
        });

        google.maps.event.addListener(markers[id], 'click', markers[id].onclick);
    }

    function openInfoWindow(marker) {
        infoWindow = new google.maps.InfoWindow({
            position : marker.position,
            content : loadOfferContent(marker.id)
        });
        infoWindow.open(map);
    }

    function loadOfferContent(id) {
        var html = '';

        formVars = {};
        formVars['random'] = new Date().getTime();
        formVars['offer_id'] = id;
        ajaxUrl = '/ajax/offers/?ajaxAction=gmapsInfoWindow';
        if (typeof(languageCode) != 'undefined')
            ajaxUrl = '/' + languageCode + ajaxUrl;
    	$.ajax({
            type: 'POST',
            timeout: 30,
            url: ajaxUrl,
            data: formVars,
            async: false,
            dataType: 'xml',
            success: function(xmlResponse){
                if ($('response', xmlResponse).length > 0 && $('result', xmlResponse).length > 0 && $('data', xmlResponse).length > 0) {
                    html = $('data', xmlResponse).text();
                }
            },
            error: function(err){
                if ((err.status != 200) && (typeof(err.statusText) != 'undefined') && (err.statusText != '')) {
                   alert(err.statusText);
                }
            }
        });
        return html;
    }

}