function MyApplication(state) {

    this.icon = new GIcon();
    this.icon.image = "imgs/dekstaviva.gif";
    this.icon.iconSize = new GSize(16, 16);
    this.icon.iconAnchor = new GPoint(16, 16);
    this.icon.infoWindowAnchor = new GPoint(10, 1);

    this.zoom = 7;

    if (state == 'cz') {
	    this.latitude = 50;
	    this.longitude = 16;    	
    } else {
        this.latitude = 48.5;
        this.longitude = 20;            	
    }

    this.map = new GMap2(document.getElementById("mapContaines"));
    this.map.setCenter(new GLatLng(this.latitude, this.longitude), this.zoom);
    this.map.setMapType(G_NORMAL_MAP);
    

    this.map.addControl(new GLargeMapControl());
    this.map.addControl(new GMapTypeControl());

    this.map.enableContinuousZoom();
    this.map.enableScrollWheelZoom();

    this.geocoder = new GClientGeocoder();

    new GKeyboardHandler(this.map); 
  
    this.mt = this.map.getMapTypes();

    for (var i=0; i < this.mt.length; i++) {

        this.mt[i].getMinimumResolution = function() {
            return 6;
        }

    }

    loadObjectFromXML(this.map, this.icon, state);

    refMapa = this.map;
    $('#regionSelect').change(function (ch){

        idRegion = $(this).attr('value');
        
        if (idRegion != '') {

            $('#pobocky').load('ajax_change_region.php', {regionId: idRegion}, function() {

                $('a.selPob').click(function (cl){
                    cl.preventDefault();
                    selectOffice(refMapa, $(this).attr('id'), state);
                });
                
                $('a.selPob').mouseover(function (cl){
                    $('div.detail').css('display', 'none');
                    $(this).parent().parent().find('div.detail').css('display', 'block');
                    selectOffice(refMapa, $(this).attr('id'), state);                	
                });

            });

        }

    });

}


function addPoint(map, latitude, longitude, options, htmlText) {

    var point = new GLatLng(latitude, longitude);
    var marker = new GMarker(point, options);

    GEvent.addListener(marker, "click", function() {
       marker.openInfoWindowHtml(htmlText);
    });

    map.addOverlay(marker);

}

function loadObjectFromXML(map, icon, state) {

    map.clearOverlays();

    if (state == 'cz') {
    	file = 'xml/dektrade.xml';
    } else {
        file = 'xml/dektradesk.xml';    	
    }

    $.get(file, {}, function(xml) {

        $('marker', xml).each(function (i) {

            var object = $(this);
            var idOffice = $(this).attr('id');

            var nadpis = $(this).find('header').text();
            var divize = $(this).find('html').text();
            var latitude = $(this).attr('latitude');
            var longitude = $(this).attr('longitude');

            var htmlText = appendTvrToMap(idOffice);

	        options = { icon:icon };

	        if (htmlText != '') {
               addPoint(map, latitude, longitude, options, htmlText);
	        }

        });

    });

}

function selectOffice(map, idOffice, state) {

    if (state == 'cz') {
        file = 'xml/dektrade.xml';
    } else {
        file = 'xml/dektradesk.xml';        
    }

    $.get(file, {}, function(xml) {

        $('marker', xml).each(function (i) {

            if ($(this).attr('id') == idOffice) {

	            var object = $(this);
	
	            var nadpis = $(this).find('header').text();
	            var divize = $(this).find('html').text();
	            var latitude = $(this).attr('latitude');
	            var longitude = $(this).attr('longitude');
	            var pozice = new GLatLng(latitude, longitude);

	            var htmlText = appendTvrToMap(idOffice);

	            map.setCenter(pozice);    
	            map.openInfoWindowHtml(pozice, htmlText);
	            
	            return;

            }

        });

    });

}

function appendTvrToMap(idOffice) {

    text = '';

	$('span.' + idOffice).each(function (each) {
	    
	    office = $(this).parent();
	    text = office.find('div.htmlText').html();
	
	});

    return text; 	

}


$(document).ready(function() {

    var state = 'cz';

    if (GBrowserIsCompatible()) {

        var application = new MyApplication(state);

    }   

    $('a.cz').click(function (cl) {

    	cl.preventDefault();
    	state = 'cz';

	    if (GBrowserIsCompatible()) {
	
	        var application = new MyApplication(state);
	
	    }   

    });

    $('a.sk').click(function (cl) {

        cl.preventDefault();
        state = 'sk';

	    if (GBrowserIsCompatible()) {
	
	        var application = new MyApplication(state);
	
	    }   

    });

});

