var noPrepareService = 0;

var pathimages = '';

function opacity(id, opacStart, opacEnd, millisec) {
    //speed for each frame 
    var speed = Math.round(millisec / 100); 
    var timer = 0; 

    //determine the direction for the blending, if start and end are the same nothing happens 
    if(opacStart > opacEnd) {
    	//alert("hide");
      for(i = opacStart; i >= opacEnd; i--) { 
      		//if(i%10==0) alert("changeOpac(" + i + ",'" + id + "','"+(opacStart- opacEnd)+"','false','"+(i==opacEnd)+"')");
          setTimeout("changeOpac(" + i + ",'" + id + "','"+(opacStart- opacEnd)+"','0')",(timer * speed)); 
  				timer++; 
      } 
    } else if(opacStart < opacEnd) {
    	//alert("show");
      for(i = opacStart; i <= opacEnd; i++) {
      		//if(i%10==0) alert("changeOpac(" + i + ",'" + id + "','"+(opacEnd - opacStart)+"','true','"+(i==opacEnd)+"')");
      		setTimeout("changeOpac(" + i + ",'" + id + "','"+(opacEnd - opacStart)+"','1')",(timer * speed)); 
          timer++; 
      } 
    } 
} 

//change the opacity for different browsers 
function changeOpac(opacity, id, diffOp, expand) {
    var object = document.getElementById(id).style; 
    var divWidth = 560;
    var divHeight = 180;
    
    
    object.opacity = (opacity / 100); 
    object.MozOpacity = (opacity / 100); 
    object.KhtmlOpacity = (opacity / 100); 
    object.filter = "alpha(opacity=" + opacity + ")";
    var widthL = parseFloat(object.width);
    var heightL = parseFloat(object.height);
    
    if(expand=="0") {
    	//reduce the height
			object.height = (heightL - (divHeight/diffOp)) + "px";
		} else {
			object.height = (heightL + (divHeight/diffOp)) + "px";
		}
}

function showDiv(idshow, idhide) {
	document.getElementById(idshow).style.display = "block";
	document.getElementById(idhide).style.display = "none";
}

function submitform()
{
	enablePrepareService();
  load();
  showAddress(); 
}

function getNodeValue(element) {
	var value = "";
	if ((element != null) && (element.firstChild != null)) {
		value = element.firstChild.nodeValue;
	}
	return value;
}
			
function updateM(){	
	if (--flag>0) { 
		return;
	}
	flag=0;
	prepareService(0,0,zmap);
}

function setM(){

	flag=0;
	GEvent.addListener(map, "zoomend", function() {
  		flag++;
  		window.setTimeout(updateM,1000);
 
  	}     
	);
 
	GEvent.addListener(map, "dragend", function() {	  
	 		if (flagZoom==0){
	 			flag++;
	 			window.setTimeout(updateM,1000);	  
	 		}
 		}
	);
   
	GEvent.addListener(map, "moveend", function() {	  
    	if (flagClick==0){
    		flag++;
    		window.setTimeout(updateM,1000);	  
    	}
 		}
	);
}

function createXMLHttpRequest () {
	if (window.ActiveXObject) {
		// Internet Explorer
		xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
	} else if (window.XMLHttpRequest) {
		// Firefox, Opera 8.0+, Safari
		xmlHttp=new XMLHttpRequest();
	} else {
		alert("Your browser does not support AJAX!");
	}
	//alert(xmlHttp)
}

function createDirectionTab(addressString, bottonString, latitudine, longitudine) {
	
	var  htmlText = "<table cellpadding=\"2\" cellspacing=\"2\" border=\"0\" >";
	
	var fromAddr = getCookie('_gmap_dl_fromAddr');
	
	//row 1
	htmlText += "<tr>";
	htmlText += "<td>" + addressString  + "</td>";
	htmlText += "</tr>";
	
	//row 2
	htmlText += "<tr>";
	htmlText += "<td><input type=\"text\" value=\""+fromAddr+"\" class=\"input\" size=\"60\" id=\"address_from\" name=\"address_from\"></td>";
	htmlText += "</tr>";
	
	//row 3
	htmlText += "<tr>";
	htmlText += "<td align=\"right\"><a href=\"javascript:getDirection(" + latitudine + "," +  longitudine + ")\">" + bottonString + "&gt;</a></td>";
	htmlText += "</tr>";
	
	
	htmlText += "</table>";
	
	
	//alert(htmlText);
	
	return htmlText;
	
	
}

function testStartPoint(latitudine, longitudine) {
	
}

function getDirection(latitudine, longitudine) {
	var startGeopoint = document.myform.address_from.value;

	document.myform.myAddressFrom.value = startGeopoint;

	if(startGeopoint==null||startGeopoint=='') {
		return;
	}	
	
	document.getElementById("direction").innerHTML = "<div class=\"direction\" id=\"direction\"></div>";

	noPrepareService = 1; 
		gdir.load("from: " + startGeopoint + ", "+ cod_Tara + " to: " + latitudine + "," + longitudine, { "locale": cod_Lang }, {getPolyline:true} );
	
	GEvent.addListener(gdir, "load", function() {
		if(gdir.getStatus().code == G_GEO_SUCCESS) {;
			document.getElementById("direction").style.visibility= "visible"; 
			document.getElementById("direction").style.height= "250px"; 
			map.closeInfoWindow();
			map.clearOverlays(); 
		}
	});
	
	GEvent.addListener(gdir, "error", function() {
	  var code = gdir.getStatus().code;
	  var reason="Code "+code;
	  if (reasons[code]) {
	    reason = reasons[code]
	  } 
	  alert(reason);
	});


 	for (var i=0; i<mt.length; i++) {
    mt[i].getMinimumResolution = function() {return 0;}
    flagZoom=1;
  }    

	document.getElementById('direction').style.display='block';
	document.getElementById('div_print_map_1').style.display='block';	
	document.getElementById('div_print_map_2').style.display='block';	
	
	document.getElementById('div_prev_search').style.display='block';	
	
	document.getElementById('div_search').style.display='none';	
	document.getElementById('div_reset').style.display='none';	
		
	setCookie('_gmap_dl_fromAddr',startGeopoint,'');
	
}

function prepareService(lat,lng,zoom_level){
	if(noPrepareService==1) {
		return;
	}

	map.addControl(mapControl);
	map.addControl(typeControl);
	mt = map.getMapTypes();

	for (var i=0; i<mt.length; i++) {
	  mt[i].getMinimumResolution = function() {return minres;}
	}   

	flagZoom=0;
	document.getElementById("message").innerHTML ='';

	if ((lat!=0)&&(lng!=0)){
		latCenter=lat;
		lngCenter=lng;
		map.setCenter(new GLatLng(lat,lng),zoom_level);
	}

	gmarkers=[];
	ginfo=[];
	ginfo1=[];
	
	map.disableDragging();
	var bounds1 = map.getBounds();
  var southWest = bounds1.getSouthWest();
	var northEast = bounds1.getNorthEast();
	var lngSpan = northEast.lng() - southWest.lng();
	var latSpan = northEast.lat() - southWest.lat();
	var label = new ELabel(map.getCenter(), ltxtd, "style1",new GSize(-120,0), 65);

	var loadingIcon = new GIcon(G_DEFAULT_ICON);
	loadingIcon.image = pathimages+'/dealer/wait-BS.gif';
	loadingIcon.iconSize = new GSize(32, 32);
	markerOptions = { icon:loadingIcon };
	map.addOverlay(new GMarker(map.getCenter(), markerOptions));

	selectService(southWest.lat(),southWest.lng(),northEast.lat(),northEast.lng());
	document.getElementById('first_search').value='N';

	setCookie('_gmap_dl_lat',lat,'');
	setCookie('_gmap_dl_lng',lng,'');
	setCookie('_gmap_dl_zoom',zoom_level,'');

}

function enablePrepareService() {
	document.getElementById("direction").innerHTML = "<div class=\"direction\" id=\"direction\"></div>";
	document.getElementById("direction").style.visibility= "hidden"; 
	document.getElementById("direction").style.height= "0px"; 
	noPrepareService=0;
}


function prevSearch() {

	flagClick=0;

	document.getElementById("message").innerHTML ='';
	document.getElementById('search').value='';
	document.getElementById('first_search').value='Y';
	document.getElementById('direction').style.display='none';
	document.getElementById('div_prev_search').style.display='none';
	document.getElementById('div_search').style.display='block';	
	document.getElementById('div_reset').style.display='block';	

	resetDataDealer();    

	var prev_lat = '';
	var prev_lng = '';
	var prev_zoom = '';

	try {
		prev_lat = getCookie('_gmap_dl_lat');
		prev_lng = getCookie('_gmap_dl_lng');
		prev_zoom = getCookie('_gmap_dl_zoom');
	}
	catch(e) {
		prev_lat = '';
		prev_lng = '';
		prev_zoom = '';		
	}	
	
	load();

	if(prev_lat!=''&&prev_lng!='') {
		prepareService(prev_lat,prev_lng,prev_zoom);
	}

}	


function getDirection2(from, latitudine, longitudine) {
	var startGeopoint = from;

	if(startGeopoint==null||startGeopoint=='') {
		return;
	}	
	
	noPrepareService = 1; 
	gdir.load("from: "  +  startGeopoint + " " + cod_Tara + " to: " + latitudine + "," + longitudine, { "locale": cod_Lang }, {getPolyline:true} );
	
	GEvent.addListener(gdir, "load", function() {
		if(gdir.getStatus().code == G_GEO_SUCCESS) {;
			document.getElementById("direction").style.visibility= "visible"; 
			document.getElementById("direction").style.height= "auto"; 
			map.closeInfoWindow();
			map.clearOverlays(); 
		}
	});
	
	GEvent.addListener(gdir, "error", function() {
	  var code = gdir.getStatus().code;
	  var reason="Code "+code;
	  if (reasons[code]) {
	    reason = reasons[code]
	  } 
	  alert(reason);
	});
	document.getElementById('direction').style.display='block';
	
	addMarkerInfo(latitudine, longitudine);

}
