﻿var url = 'http://' + document.domain;

// String Buffer Section

 function StringBuffer() { 
   this.buffer = []; 
 } 

 StringBuffer.prototype.append = function append(string) { 
   this.buffer.push(string); 
   return this; 
 }; 

 StringBuffer.prototype.toString = function toString() { 
   return this.buffer.join(""); 
 }; 


// /String Buffer Section



// === Method that shows points of a given category
function show(category) {
    for (var i=0; i<markers.length; i++) {
         if (markers[i].mycategory == category) {
            markers[i].show();
         }
     }
}

// === Method that hides points of a given category
function hide(category) {
    for (var i=0; i<markers.length; i++) {
     if (markers[i].mycategory == category) {
       markers[i].hide();
     }
    }
    mapE.closeInfoWindow();
}

// === Method that deals with the click event on the entity name   
var objOldMarker = "";
    
function myclick(i) {
    var html = new StringBuffer();

    var obj = document.getElementById("detail_" + i);

    if(obj && markers.length >= i){
        
        if (obj.innerHTML == "") {
		
			if (objOldMarker) {
				objOldMarker.innerHTML = "";       
				objOldMarker.style.display = "none";				
			}
		
		    if (markers[i].myspeciality != "") {
                html.append(markers[i].myspeciality);
                html.append('<br /><br />');		    
		    }
            html.append('&nbsp;&nbsp;Tl:');
            html.append(markers[i].myphone);
            html.append('<br />&nbsp;&nbsp;Fax:');
            html.append(markers[i].myfax);
            html.append('<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href="#" class="SaberMais"  onclick="Detalhe_Click(');
            html.append(markers[i].myid);
            html.append(',\'');
            html.append(markers[i].myaddressType);
            html.append('\')" >');
            html.append(markers[i].mylink);
            html.append('</a>');   
            
            obj.style.display = "block";	
            obj.innerHTML = html.toString();       

			objOldMarker = obj;
            GEvent.trigger(markers[i],"click");     
			            
        } else {
		
            obj.innerHTML = "";       
            obj.style.display = "none";	
            mapE.closeInfoWindow();     
        }
        
    }
}

// === Creates markers       
function createMarker(point, category, name, address, city, postalcode, phone, fax, id,addressType, speciality, link) 
{
	var marker = new GMarker(point,icons[category]);
	marker.mycategory = category;
	marker.myname = name;
	marker.myaddress = address;
	marker.myphone = phone;
	marker.myfax = fax;
	marker.myid = id;
	marker.mylink = link;
	marker.myspeciality = speciality;
	marker.myaddressType = addressType
		
	GEvent.addListener(marker, "click", function() {
	    var infotext = new StringBuffer();
	    infotext.append('<div class="titleInfoWindow">');
	    infotext.append('<p>');
	    infotext.append(name);
	    infotext.append('</p>');
	    infotext.append('<table border="0" cellspacing="0" cellpading="0" width="100%">');
	    infotext.append('<tr>');
	    infotext.append('<td colspan="2">');
	    infotext.append(address);
	    infotext.append('</td>');
	    infotext.append('</tr>');
	    infotext.append('<tr>');
	    infotext.append('<td colspan="2">');
	    infotext.append(postalcode);
	    infotext.append('&nbsp;');
	    infotext.append(city);
	    infotext.append('</td>');
	    infotext.append('</tr>');
	    infotext.append('<tr>');
	    infotext.append('<td colspan="2">');
	    infotext.append('Tl:');
	    infotext.append(phone);
	    infotext.append('</td>');
	    infotext.append('</tr>');
	    infotext.append('<tr>');
	    infotext.append('<td>');
	    infotext.append('Fax:');
	    infotext.append(fax);
	    infotext.append('</td>');
	    infotext.append('<td align="right">');
        infotext.append('<a href="#" class="SaberMais" onclick="Detalhe_Click(');
        infotext.append(id);
         infotext.append(',\'');
        infotext.append(addressType);
        infotext.append('\')" >');
        infotext.append(link);
        infotext.append('</a>');               	            
	    infotext.append('</td>');
	    infotext.append('</tr>');
	    infotext.append('</table>');
	    infotext.append('</div>');

		// Gmarker.openInfoWindowHtml has a bug with onOpenFn
		mapE.openInfoWindowHtml(marker.getLatLng(), infotext.toString(), {onOpenFn: function() { mapE.getInfoWindow().reset(mapE.getInfoWindow().getPoint(), mapE.getInfoWindow().getTabs(), new GSize(200, 100), null, null); }});
    });
	markers.push(marker);
	totalMarkers++;
	return marker;
}


// === Method that builds entity sidebar
function makeSidebar() {

    var html = new StringBuffer();
    if (document.getElementById("side_bar")) {
        html.append('<ul>');

        for (var i=0; i<markers.length; i++) {
            // only if is shown
            if (!markers[i].isHidden()) {
                   html.append('<li>');
                   html.append('<a href="javascript:myclick(');
                   html.append(i);
                   html.append(')">');
                   html.append(markers[i].myname); 
                   html.append('</a>');
                   html.append('</li>');
                   html.append('<p id="detail_');
                   html.append(i);
                   html.append('" style="display:none">');
                   html.append('</p>');                   
            }     
        }
        html.append('</ul>');
        document.getElementById("side_bar").innerHTML = html.toString();
    }
}

// === Method that sends the id of the clicked entity
function Detalhe_Click(id,addressType)
{
   __doPostBack('clickedDetail',id + "_" + addressType);
}
// === A method that recovers hidden markers


// === Toggles Map buttons, returns true if selected
function toggleMapButton (category) {
	 
		var retVal = false;

	   		var a = document.getElementById('btn' + category.toLowerCase()); 
	        var aclass = category.toLowerCase();

			if (a) {
			if (a.className == aclass ) {
				a.className =category.toLowerCase()+'_Selected';
				retVal = true;
			} else {
				a.className =aclass ;
				retVal = false;
			}
		}

		return retVal;
}

// === Metohd that updates map if any of the filters is pressed
function UpdateSearchMap(category,id) {
    
    if (toggleMapButton (category)) {
        hide(category);  
        document.getElementById(id).value="false";   
    }
    else{
        show(category);
        document.getElementById(id).value="true";
    }     

    makeSidebar();
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}
function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}


