var Distributor = {
    GroupID : 0,
    ID : 0,
    Company : "",
    Address : "",
    Link : "",
    Tel : "",
    Fax : "",
    Marker : null,
    
    Create: function(groupid, id, company, address, link, tel, fax, marker)
    {
        this.GroupID = groupid;
        this.ID = id;
        this.Company = company;
        this.Address = address;
        this.Link = link;
        this.Tel = tel;
        this.Fax = fax;
        this.Marker = marker;
    }
}



var Distributors = {
    addressArray : new Array(),
    Map : null,

    init: function(map)
	{
	    this.Map = map;
	    // Set the markers
	    for(var i = 0; i < this.addressArray.length; i++)
	    {
	        var marker = this.addressArray[i].Marker;
	        			
	        map.addOverlay(marker);
	        
            GEvent.addListener(marker, "click", function() { Distributors.showDetails(this) } );
	    }
	    
	    // Set the navigation events
	    var distNav = $('#navPage a');
	    
	    for(var i = 0; i < distNav.length; i++)
	    {
		    $(distNav[i]).click( function() { Distributors.show(this.id.split("_")[1]); });
		}
	},
	
	show: function(groupid)
	{
	    this.resetDistributorMarkerImages();
        this.setGroupInformation(groupid);
	    this.setDistributorGroupMarkerImage(groupid);
	},
	
	findGroupAddresses: function(id)
	{
	    var groupArray = new Array();
	    var count = 0;
	    for(var i = 0; i < this.addressArray.length; i++)
	    {
	        if (this.addressArray[i].GroupID == id)
	        {
	            groupArray[count] = this.addressArray[i];
	            count++;
	        }
	    }
	    
	    return groupArray;
	},
	
	createHTML: function(distributor)
	{
        var locationHTML = "<dl style='width:300px;'><dt>" + distributor.Company + "</dt>";
        locationHTML += "<dd>" + distributor.Address + "</dd>";
        if (distributor.Tel != "")
            locationHTML += "<dd>Telephone: <strong>" + distributor.Tel + "</strong></dd>";
            
        if (distributor.Fax != "")
            locationHTML += "<dd>Fax: <strong>" + distributor.Fax + "</strong></dd>";
            
        if (distributor.Website != "")
            locationHTML += "<dd><a href=\"" + distributor.Link + "\" title=\"View the " + distributor.Company + " website\">" + distributor.Link + "</a></dd>";
            
        locationHTML += "</dl>";
        
        return locationHTML;
	},
	
	resetDistributorMarkerImages: function()
	{
	    for(var i = 0; i < this.addressArray.length; i++)
	    {
	        this.addressArray[i].Marker.setImage("/content/markers/red-dot.png");
	    }
	    
	    this.Map.closeInfoWindow();
	},
	
	setDistributorGroupMarkerImage: function(groupid)
	{
	    for(var i = 0; i < this.addressArray.length; i++)
	    {
	        if (this.addressArray[i].GroupID == groupid)
	            this.addressArray[i].Marker.setImage("/content/markers/blue-dot.png");
	    }
	},
	
	getDistributor: function(marker)
	{
	    var distributor;
	    
	    for(var i = 0; i < this.addressArray.length; i++)
	    {
	        if (this.addressArray[i].Marker == marker)
	        {
	            distributor = this.addressArray[i];
	            break;
	        }
	    }
	    
	    
	    return distributor;
	},
	
    showDetails: function(marker)
    {
        var distributor = this.getDistributor(marker);
        var html = "<div>" + this.createHTML(distributor) + "<\/div>";
        var groupid = distributor.GroupID;
        
        this.resetDistributorMarkerImages();
        
        marker.openInfoWindowHtml(html);

        this.setDistributorGroupMarkerImage(groupid);
    }
}