function id(id){return document.getElementById(id);}
function site(){return window.location.protocol+'//'+window.location.hostname;}


////////////////////////////////////////////////////
// photo gallery
////////////////////////////////////////////////////
pid = '1'; // photo id

function gallery(photoid,imgbig,img,gitem,alt)
{
	var galitm = id('photogallery').getElementsByTagName('div');
	
	for(i=0;i<galitm.length;i++)
	{
		galitm[i].className = 'img';
	}

	// add photo id
	pid = photoid;
	
	gitem.parentNode.className = 'img selected';

	var mainimg = id('mainfoto');
	mainimg.src = imgbig;
	mainimg.alt = mainimg.title = alt;

	var mainimglink = id('mainfotolink');
	mainimglink.href = img;
	mainimglink.alt = mainimglink.title = alt;

	return false;
}

function opengallery(photonum, photoid)
{
	if (photoid == '0')
	{
		currentphoto = id('photo'+pid).href;
		currentphotoalt = id('photo'+pid).alt;
		prevphotoid = 1;
	}
	else
	{
		currentphoto = id('photo'+photoid).href;
		currentphotoalt = id('photo'+photoid).alt;
		pid = photoid;
	}
	
	if (pid > 1)
	{
		prevphotoid = pid - 1;
		prevbutton = '<img id="prev_photo" onclick="return opengallery('+photonum+','+prevphotoid+');" src="http://www.housecz.ru/core/tpl/admin/img/prev_photo.gif" title="Предыдущая фотография" />';
	}
	else
	{
		prevbutton = '<img id="prev_photo" src="http://www.housecz.ru/core/tpl/admin/img/empty_button.png" title="" class="empty_button" />';
		prevphotoid = prevphotoid - 1;
	}
	
	if (photonum > pid)
	{
		nextphotoid = prevphotoid + 2;
		nextbutton = '<img id="next_photo" onclick="return opengallery('+photonum+','+nextphotoid+');" src="http://www.housecz.ru/core/tpl/admin/img/next_photo.gif" title="Следующая фотография" />';
	}
	else
	{
		nextbutton = '<img id="next_photo" src="http://www.housecz.ru/core/tpl/admin/img/empty_button.png" title="" />';
		nextphotoid = '';
	}

	id('bigphotospace').innerHTML = '<div id="photoshade">'+'<div id="photoframe">'+'<img src="'+currentphoto+'" alt=""  title="Закрыть" onclick="return closegallery(this);" id="bigphoto">'+prevbutton+''+nextbutton+'<img src="http://www.housecz.ru/core/tpl/admin/img/close_photo.gif" alt="Закрыть" title="Закрыть" id="close_photo" onclick="return closegallery(this);">'+'</div>'+'</div>';

	return false;
}

function closegallery()
{
	var mainimg = id('mainfoto');
	
	mainimg.src = currentphoto;
	
	currentphoto = currentphoto.replace(".jpg","_m.jpg");
	currentphoto = currentphoto.replace(".jpeg","_m.jpeg");
	currentphoto = currentphoto.replace(".png","_m.png");
	currentphoto = currentphoto.replace(".gif","_m.gif");
	mainimg.src = currentphoto;
	
	id('photoshade').style.display='none';
	//$("#photoshade").fadeOut("fast");
}


function currency_convert(origin)
{
    var origin_value = eval("document.currency.c"+origin+".value");
    var euro_equivalent = rate[origin];
    var v;
    for (i=0; i<rate.length; i++)
    {
		if (i!=origin)
		{
		    v = Math.round(rate[i]*origin_value/euro_equivalent*100)/100;
		    
		    if (isNaN(v))
		    {
		    	v = 0;
		    }
		    
		    eval("document.currency.c"+i+".value = "+v);
		}
    }
    return true;
}

/*
function initialize()
{
	var myLatlng = new google.maps.LatLng('Praha');
	var myOptions = {
	  zoom: 8,
	  center: myLatlng,
	  mapTypeId: google.maps.MapTypeId.ROADMAP
	}
	var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
	
	    /*
   // Create a draggable marker which will later on be binded to a
        // Circle overlay.
        var marker = new google.maps.Marker({
          map: map,
          position: new google.maps.LatLng(0.1, 0),
          draggable: false,
          title: 'Drag me!'
        });

        // Add a Circle overlay to the map.
        var circle = new google.maps.Circle({
          map: map,
          radius: 3000000 // 3000 km
        });

        // Since Circle and Marker both extend MVCObject, you can bind them
        // together using MVCObject's bindTo() method.  Here, we're binding
        // the Circle's center to the Marker's position.
        // http://code.google.com/apis/maps/documentation/v3/reference.html#MVCObject
        circle.bindTo('center', marker, 'position');


}
  
*/

////////////////////////////
// Gmap AP V3
////////////////////////////
var map;
var geocoder;
var centerChangedLast;
var reverseGeocodedLast;
var currentReverseGeocodeResponse;
var markerstatus = 1;

function initialize() {
	var latlng = new google.maps.LatLng(54.136980698171456,53.39965376640624);
	var myOptions = {
	  zoom: 2,
	  center: latlng,
	  mapTypeId: google.maps.MapTypeId.ROADMAP
	};
	
	map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
	geocoder = new google.maps.Geocoder();

	setupEvents();
	centerChanged();
}

function setupEvents()
{
	reverseGeocodedLast = new Date();
	centerChangedLast = new Date();
	
	setInterval(function() {
	  if((new Date()).getSeconds() - centerChangedLast.getSeconds() > 1) {
	    if(reverseGeocodedLast.getTime() < centerChangedLast.getTime())
	      reverseGeocode();
	  }
	}, 1000);
	
	google.maps.event.addListener(map, 'zoom_changed', function() {
	 // document.getElementById("zoom_level").innerHTML = map.getZoom();
	});
	
	google.maps.event.addListener(map, 'center_changed', centerChanged);
	
	google.maps.event.addDomListener(document.getElementById('crosshair'),'dblclick', function() {
	   map.setZoom(map.getZoom() + 1);
	});
}

function getCenterLatLngText()
{
	return '(' + map.getCenter().lat() +', '+ map.getCenter().lng() +')';
}

function centerChanged()
{
	centerChangedLast = new Date();
	var latlng = getCenterLatLngText();
	//document.getElementById('latlng').innerHTML = latlng;
	//document.getElementById('formatedAddress').innerHTML = '';
	currentReverseGeocodeResponse = null;

	// Create a draggable marker which will later on be binded to a
    // Circle overlay.
    if (markerstatus == 2)
    {
        var image = 'http://www.housecz.ru/core/tpl/admin/img/home.gif';

        var marker = new google.maps.Marker({
          map: map,
          position: new google.maps.LatLng(map.getCenter().lat(), map.getCenter().lng()),
          draggable: false,
          // icon: image,
          title: ''
        });
        
         // Add a Circle overlay to the map.
        var circle = new google.maps.Circle({
          map: map,
          fillColor:"#FFD164",
          strokeColor:"#FE8800",
          strokeWeight:1,
          radius: 200 // metr
          
        });

        // Since Circle and Marker both extend MVCObject, you can bind them
        // together using MVCObject's bindTo() method.  Here, we're binding
        // the Circle's center to the Marker's position.
        // http://code.google.com/apis/maps/documentation/v3/reference.html#MVCObject
        circle.bindTo('center', marker, 'position');
    }
    
    markerstatus = markerstatus+1;
    //alert(markerstatus);
}

function reverseGeocode()
{
	reverseGeocodedLast = new Date();
	geocoder.geocode({latLng:map.getCenter()},reverseGeocodeResult);
}

function reverseGeocodeResult(results, status)
{
	currentReverseGeocodeResponse = results;
	
	if(status == 'OK')
	{
		if(results.length == 0)
		{
			//document.getElementById('formatedAddress').innerHTML = 'None';
			 document.getElementById("map").style.display = 'none';
		}
		else
		{
			//document.getElementById('formatedAddress').innerHTML = results[0].formatted_address;
			// document.getElementById("map").style.display = 'block';
		}
	}
	else
	{
		//document.getElementById('formatedAddress').innerHTML = 'Error';
		 document.getElementById("map").style.display = 'none';
	}
}

function geocode()
{
	var address = document.getElementById("map_address").value;
	
	geocoder.geocode({
	  'address': address,
	  'partialmatch': true}, geocodeResult);
}

function geocodeResult(results, status)
{
	if (status == 'OK' && results.length > 0) {
		map.fitBounds(results[0].geometry.viewport);
	}
	else
	{
		//alert("Geocode was not successful for the following reason: " + status);
		document.getElementById("map").style.display = 'none';
	}
}

function addMarkerAtCenter()
{
	var marker = new google.maps.Marker({
	    position: map.getCenter(),
	    map: map
	});
	
	var text = 'Lat/Lng: ' + getCenterLatLngText();
	
	if(currentReverseGeocodeResponse)
	{
		var addr = '';
		
		if(currentReverseGeocodeResponse.size == 0)
		{
			addr = 'None';
		}
		else
		{
			addr = currentReverseGeocodeResponse[0].formatted_address;
		}
		
		text = text + '<br>' + 'address: <br>' + addr;
	}
	
	var infowindow = new google.maps.InfoWindow({ content: text });
	
	google.maps.event.addListener(marker, 'click', function() {
		infowindow.open(map, marker);
	});
}

$(document).ready(function()
{
	if ($("#map_canvas").html() > '0')
	{		
		initialize();
		geocode();
	}

});
