document.write('<script type="text/javascript" src="' + (isBackend?'../':'') + 'lib/js/icpictures/icpictures.js"></script>');
document.write('<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false&amp;language=bg"></script>');

var map;
var im=0;
var marker = new Array();
 
function initMap(a, b, coordinates, coordinatesa, coordinatesb)
{
	 var myOptions = 
	 {
	    zoom: 12,
	    mapTypeId: google.maps.MapTypeId.ROADMAP
	 };
	 
	 map = new google.maps.Map(document.getElementById("googlemap"), myOptions);
   	 var centrePoint = new google.maps.LatLng(a,b);
	 map.setCenter(centrePoint, 14);
	     	 
   	 $(coordinates).value = a+ ', '+b;	
   	$(coordinatesa).setValue(a);
    $(coordinatesb).setValue(b);
    
	 $('coordinates').innerHTML = a+ ', '+b;
   	 
   	 marker[im] = new google.maps.Marker({map: map, position:  map.getCenter()});
   	 im=im+1;
   	 
   	 google.maps.event.addListener(map, 'click', function(event) 
   	 {	   	 
   	 	clearMarker(marker);        
	    map.setCenter(event.latLng);	    	 
	    $(coordinates).value = event.latLng.lat()+ ', '+event.latLng.lng();
	    $(coordinatesa).setValue(event.latLng.lat());
	    $(coordinatesb).setValue(event.latLng.lng());
	    $('coordinates').innerHTML = event.latLng.lat()+ ', '+event.latLng.lng();
	    marker[im]= new google.maps.Marker({map: map, position:  map.getCenter()});	 
	    im=im+1;	    
	  });   	 
}

function setMap(a, b, object, title)
{
	 var myOptions = 
	 {
	    zoom: 15,
	    mapTypeId: google.maps.MapTypeId.ROADMAP
	 };

	 map = new google.maps.Map(document.getElementById(object), myOptions);
	 
   	 var centrePoint = new google.maps.LatLng(a,b);
   	 
	 map.setCenter(centrePoint, 14);
	 
	 var marker = new google.maps.Marker({map: map, position:  map.getCenter()});
     
	 infowindow = new google.maps.InfoWindow({
			    content: title
			});     
     infowindow.open(map,marker);
     
     google.maps.event.addListener(marker, 'click', function() {
     	infowindow.open(map,marker);
     });
}

function clearMarker(markers)
{
	for(var i=0; i < markers.length; i++)
	{
        markers[i].setMap(null);
    }
}

function setMapCoords(city, address, coordinates, coordinatesa, coordinatesb, mapAddress)
{	 
	cityText = $(city).options[$(city).selectedIndex].text;
	addressText = $(address).value;	
	fullAddress = '';	
	if (addressText) fullAddress += addressText + ', ';
	if (cityText) fullAddress += cityText + ', България';
		
	$(mapAddress).value = fullAddress;
   
	var geocoder = new google.maps.Geocoder(); 
   		 
	geocoder.geocode( {'address': fullAddress}, 
  	function(results, status)
  	{
       if (status == google.maps.GeocoderStatus.OK) 
       {   		      		        	       	
       	 var a = results[0].geometry.location.lat();
       	 var b = results[0].geometry.location.lng();
       	 clearMarker(marker);	
       	 var centrePoint = new google.maps.LatLng(a,b);
	     map.setCenter(centrePoint);
	     $(coordinates).value = a+ ', '+b;	
	     $('coordinates').innerHTML = a+ ', '+b;	    	    
	     $(coordinatesa).setValue(a);
	     $(coordinatesb).setValue(b);
	     marker[im]= new google.maps.Marker({map: map, position:  map.getCenter()});	 
	    im=im+1;     	 
       }      
     }
   );
}

function setMarkerByCoords(coordinatesa,coordinatesb)
{
	coorda = $(coordinatesa).getValue();
	coordb = $(coordinatesb).getValue();
  	 clearMarker(marker);	
  	 var centrePoint = new google.maps.LatLng(coorda,coordb);
    map.setCenter(centrePoint);
    $('coordinates').innerHTML = coorda+ ', '+coordb;
    marker[im]= new google.maps.Marker({map: map, position:  map.getCenter()});
   im=im+1;
}

function findAddress(city, area, sender, coordinates, coordinatesa, coordinatesb)
{	   		 
	var geocoder = new google.maps.Geocoder();
	address = $(sender).value;
	
	//cityName = $(city).getSelectedOptionHTML();
	
	/*if (!(element = $(area)))
	{
		areaName = false;
	}
	else
	{
		var index = element.selectedIndex;
		areaName = index >= 0 ? element.options[index].innerHTML : undefined
	}
	if(areaName.length > 0 && areaName!='-')
	{
		address = 'ж.к. ' + areaName + (address.length>0?', ':'') + address;
	}*/
	
	if (!(element = $(city)))
	{
		cityName = false;
	}
	else
	{
		var index = element.selectedIndex;
		cityName = index >= 0 ? element.options[index].innerHTML : undefined
	}
	if(cityName.length > 0 && cityName!='-')
	{
		address = cityName + (address.length>0?', ':'') + address;
	}
	
    geocoder.geocode( {'address': address}, 
  	function(results, status)
  	{
       if (status == google.maps.GeocoderStatus.OK) 
       {   		      		        	
       	 var a = results[0].geometry.location.lat();
       	 var b = results[0].geometry.location.lng();

       	 clearMarker(marker);	
       	 var centrePoint = new google.maps.LatLng(a,b);
	     map.setCenter(centrePoint);
	     $(coordinates).value = a+ ', '+b;	
	     $(coordinatesa).setValue(a);
	     $(coordinatesb).setValue(b);
	     $('coordinates').innerHTML = a+ ', '+b;	         	    
	     marker[im]= new google.maps.Marker({map: map, position:  map.getCenter()});	 
	    im=im+1;     	 
       }      
     }
   );
   return false;
}

imageCount  = 0;
function AddImageInput()
{
    var tdFileInputsTemp = document.getElementById('tdFileInputsTemp');
    var tdFileInputs = document.getElementById('tdFileInputs');
    var fileInput = document.getElementById('fileInputTemp');
    var a = tdFileInputsTemp.lastChild;
    // Create a new file input
    var newFileInput = fileInput.cloneNode(false);
    newFileInput.value = null;
    var id = 'dynamic_image_' + (new Date()).getTime();
    newFileInput.id = id // A unique id
    newFileInput.name = newFileInput.id;
    tdFileInputs.appendChild(newFileInput);
    
    // Create a new a input
    var newa = a.cloneNode(false);
    newa.id = 'remove_' + id; // A unique id
    newa.innerHTML = "Изтрии";
    newa.onclick = RemoveImageInput;
    tdFileInputs.appendChild(newa);
    var lnkAttch = document.getElementById('lnkAttch');
    imageCount++;
    if(imageCount>0)
    {
        lnkAttch.innerHTML = "Добави още снимки";
    }
    else
    {
        lnkAttch.innerHTML = "Добави снимка";
    }
}

function RemoveImageInput(e)
{
    var Event = e ? e : window.event; 
    var obj = Event.target ? Event.target : window.event.srcElement; 
    var tdFileInputs = document.getElementById('tdFileInputs');
    var a = document.getElementById(obj.id);
    tdFileInputs.removeChild(a);
    var fileInputId = obj.id.replace('remove_','');
    var fileInput = document.getElementById(fileInputId);
    tdFileInputs.removeChild(fileInput);
    var lnkAttch = document.getElementById('lnkAttch');
    imageCount--;
    if(imageCount>0)
    {
         lnkAttch.innerHTML = "Добави още снимки";
    }
    else
    {
         lnkAttch.innerHTML = "Добави снимка";
    }
    
    return false;
}


fileCount  = 0;
function AddFileInput()
{
    var tdFileInputsTemp = document.getElementById('tdFileInputsTemp2');
    var tdFileInputs = document.getElementById('tdFileInputs2');
    var fileInput = document.getElementById('fileInputTemp2');
    var a = tdFileInputsTemp.lastChild;
    // Create a new file input
    var newFileInput = fileInput.cloneNode(false);
    newFileInput.value = null;
    var id = 'dynamic_file_' + (new Date()).getTime();
    newFileInput.id = id // A unique id
    newFileInput.name = newFileInput.id;
    tdFileInputs.appendChild(newFileInput);
    
    // Create a new a input
    var newa = a.cloneNode(false);
    newa.id = 'remove_' + id; // A unique id
    newa.innerHTML = "Изтрии";
    newa.onclick = RemoveFileInput;
    tdFileInputs.appendChild(newa);
    var lnkAttch = document.getElementById('lnkAttch2');
    imageCount++;
    if(imageCount>0)
    {
        lnkAttch.innerHTML = "Добави още файлове";
    }
    else
    {
        lnkAttch.innerHTML = "Добави файл";
    }
}

function RemoveFileInput(e)
{
    var Event = e ? e : window.event; 
    var obj = Event.target ? Event.target : window.event.srcElement; 
    var tdFileInputs = document.getElementById('tdFileInputs2');
    var a = document.getElementById(obj.id);
    tdFileInputs.removeChild(a);
    var fileInputId = obj.id.replace('remove_','');
    var fileInput = document.getElementById(fileInputId);
    tdFileInputs.removeChild(fileInput);
    var lnkAttch = document.getElementById('lnkAttch2');
    fileCount--;
    if(imageCount>0)
    {
        lnkAttch.innerHTML = "Добави още файлове";
    }
    else
    {
        lnkAttch.innerHTML = "Добави файл";
    }
    
    return false;
}
