﻿/* Queue the Site Load Events
===================================================================================== */
addLoadEvent(searchIcons)
//addLoadEvent(toggleCoList)
addLoadEvent(suppliers)
addLoadEvent(homePop)
addLoadEvent(initSectorMap)


var map;
var markers = new Array();
var info = new Array();
function initSectorMap() {
		
		if (!document.getElementById('ctl00_MainContent_chkSectors')) return ;
		
		var chks = document.getElementById('ctl00_MainContent_chkSectors').getElementsByTagName('input');
		var sectors = '';
		
		for (i=0; i<chks.length; i++) {
			if (chks[i].checked === true) {
				sectors = sectors + "," + chks[i].nextSibling.lastChild.nodeValue ;		
			}
		}
		if (sectors.length > 0) {
			sectors = sectors.substring(1, sectors.length)
		}
		
    ASP.busdir_map_aspx.LoadSectors(sectors, loadCompanies_Callback);


}
				
function loadCompanies_Callback(r){
    var dt = r.value;
    markers = new Array();
    
    for (i=0;i<dt.Rows.length;i++) {
        var ico = createPushpin(dt.Rows[i]["Sector"]);
        var m = new GMarker(new GLatLng(dt.Rows[i]["Lat"],dt.Rows[i]["Lng"]), ico);
        m.id = i;
        
        markers.push(m);
   
        var html = createInfoWin(dt.Rows[i]);
        info.push(html);
    }
    
    setTimeout("createMap();",1000);
}
function createMap(){
    if (GBrowserIsCompatible()) {
        map = new GMap2(document.getElementById("mapWrap"));
        map.addControl(new GLargeMapControl());
        map.clearOverlays();
        
        if (markers.length > 0) {
					map.setCenter(new GLatLng(markers[0].getPoint().lat(), markers[0].getPoint().lng()), 8);
				} else {
					map.setCenter(new GLatLng("52.660493", "1.284892"), 8);
				}
        
        for (i=0;i<markers.length;i++){
          map.addOverlay(markers[i]);
          GEvent.addListener(markers[i], "click", function() {
            this.openInfoWindowHtml(info[this.id]);
					});
        }        
       zoomToMarkers();
    }
}
function createPushpin(sector){
    var ico = new GIcon();
  
		if (sector == "Motorsport") {
			ico.image = "/Images/busDir/helmetPoint.png";
		} else if (sector == "Supplier") {
			ico.image = "/Images/busDir/letterPoint.png";
		} else if (sector == "Engineering") {
			ico.image = "/Images/busDir/cogPoint.png";
		}
		
    //ico.shadow = "/Images/mm_20_shadow.png";
    ico.iconSize = new GSize(20,26);
    ico.iconAnchor = new GPoint(14,12);
   // ico.shadowSize = new GSize(20, 26);
    
    ico.infoWindowAnchor = new GPoint(9,2);
    ico.infoShadowAnchor = new GPoint(18,25);
    
    return ico;    
}

function createInfoWin(dr){
    var ret;
        
    ret = '<div class="infowin">';
    ret += '<strong>' + dr["Name"] + '</strong>';
    ret += '<dl>';
    ret += '<dt>Address:</dt><dd>' + dr["Address"] + ', ' + dr["Postcode"], '</dd>';
    ret += '<dt>Telephone:</dt><dd>' + dr["Tel"] + '</dd>';
    ret += '<dt><a href="/business-community/search-the-directory?CompanyDetail=' + escape(dr["Name"]);
    ret += '">More information</a></dt><dd>&nbsp;</dd>';
    ret += '</dl>';    
    
    ret += '</div>';

    return ret;
}
function zoomToMarkers(){
    var count = 0;
    var thePoint, x, y, minX, maxX, minY, maxY, span;
    
    for (i = 0; i < markers.length; i++) {
        marker = markers[i];
        
        thePoint = marker.getPoint();
        x = thePoint.lat(); y = thePoint.lng();
        if (count == 0)
        {
	        minX = x; maxX = x; minY = y; maxY = y;
        }
        else
        {
	        if (x < minX) minX = x;
	        if (x > maxX) maxX = x;
	        if (y < minY) minY = y;
	        if (y > maxY) maxY = y;
        }
        count++;
    }
    
    if (count == 1)
        map.setCenter(new GLatLng(x,y), 12);
    else if (count > 1)
    {
        var center = new GLatLng((minX + maxX) / 2, (parseFloat(minY) + parseFloat(maxY)) / 2);
        span = new GSize(Math.abs(maxX - minX), Math.abs(maxY - minY));
        slopWid = 0;
        slopHgt = 0;
        deltaHgt = 0;
        var bounds = new GLatLngBounds(new GLatLng(minX-slopHgt, minY-slopWid), new GLatLng(maxX+slopHgt, maxY+slopWid)); // sw, ne
        var zoom = map.getBoundsZoomLevel(bounds);
        
        map.setCenter(center, zoom);
    }
}



function homePop() {
	if (getElementsByClass('pop', null, 'div') == '') return ;
	var close = getElementsByClass('close', null, 'a')[0].onclick = function() {
		this.parentNode.parentNode.style.display = 'none' ;
		return false;
	}
}

function toggleCoList() {
	// Function to hide list of companies unless rev select message type "messages from rev"
	var tDd = getElementsByClass('contentType', null, 'select') ;
	var cos = getElementsByClass('coList', null, 'div') ;
	var tar = getElementsByClass('targetList', null, 'div') ;
	if (tDd == '') return ;
	if (cos == '') return ;
	if (tar == '') return ;
	tDd = tDd[0] ;
	cos = cos[0] ;
	tar = tar[0] ;
	tDd.value = tDd.options[tDd.selectedIndex].value ;
	
	if (tDd.value != 'message') {
		cos.style.display = 'none' ;
	} else {
		tar.style.display = 'none' ;
	}
	tDd.onchange = function() {
		if (tDd.value == 'message') {
			cos.style.display = 'block' ;
			tar.style.display = 'none' ;
		} else {
			cos.style.display = 'none' ;
			tar.style.display = 'block' ;
		}
	}
	return true;
} 
function searchIcons() {
	var iconWraps = getElementsByClass('icons', null, 'div') ;
	if (iconWraps == '') return ;
	for (i=0; iconWraps.length > i; i++) {
		var icons = iconWraps[i].getElementsByTagName('img') ;
		for (j=0; icons.length > j; j++) {
			icons[j].smlSrc = icons[j].src ;
			icons[j].lrgSrc = icons[j].src.replace('Small', 'Large') ;
			img = new Image() ;
			img.src = icons[j].lrgSrc ;
			icons[j].onmouseover = toggleIcon ;
			icons[j].onmouseout = toggleIcon ;
		}
	}
}
function toggleIcon() {
	if (this.src.indexOf('Small') > 0) {
		this.src = this.lrgSrc ;
		this.style.position = 'absolute' ;
		this.style.width = '76px' ;
		this.style.height = '100px' ;
		this.style.left = '-50%' ;
		this.style.top = '-20px' ;
		this.parentNode.style.zIndex = '99' ;
	} else {
		this.src = this.smlSrc ;
		this.style.position = 'static' ;
		this.style.width = 'auto' ;
		this.style.height = 'auto' ;
		this.parentNode.style.zIndex = '1' ;
	}
}
function suppliers() {
	if (!document.getElementById("suppliers")) return ;
	var fs = document.getElementById("suppliers")	 ;
	var dd = getElementsByClass("ddSectors", null, "select") ;
	if (dd == '') return ;
	dd = dd[0] ;
	fs.style.display = 'none' ;	
	
	checkSelection() ;	
	dd.onchange = checkSelection ;
	
	function checkSelection() {	
		var i = dd.options.length - 1 ;
		do {
			if (dd.options[ i ].selected) {
				fs.style.display = (dd.options[ i ].text == 'Supplier' ? 'block' : 'none') ;
				return ;
			}
			i-- ;
		} while (i >= 0) ;
	}
}
