/*



*/
var clicked_marker = false;
var map = false;
var ws_index = new Array();
var gmapssession = '';
var boschicon = new GIcon();
boschicon.image = "images/googlemaps/logo_bosch.png";
boschicon.iconSize = new GSize(53, 38);
boschicon.iconAnchor = new GPoint(16, 38);
boschicon.infoWindowAnchor = new GPoint(16, 38);



function WorkshopIndexItem(coordinate, ws_id){
	this.coordinate = coordinate;
	this.visible = false;
	this.marker = false;
	this.ws_id = ws_id;
	
	this.show = function(){
		if (!this.marker)
			this.marker = this.createMarker();
		else{
			if (!this.visible)
				this.marker.show();
		}
		this.visible = true;
		
		// todo: implement
	}
	this.hide = function(){
		this.visible = false;
		if (this.marker)
			this.marker.hide();
		// todo: implement
	}
	this.createMarker = function(){
		// todo: implement
		//coord = new GLatLng(this.coordinate.lat()- (Math.random() / 10), this.coordinate.lng()- (Math.random()/10));
		marker = new GMarker(this.coordinate, boschicon);
		map.addOverlay(marker);
		marker.ws_id = this.ws_id;
		
		GEvent.addListener(marker, 'click', function(){
		
			clicked_marker = this;
			bounds = map.getBounds();
			ne = bounds.getNorthEast();
			sw = bounds.getSouthWest();
			
			distY = ne.lat() - sw.lat();

			//p = new GLatLng(this.getLatLng().lat() + distY*.3, this.getLatLng().lng());
			
			//map.panTo(p);
                        var new_lat = this.getLatLng().lat() + distY*.45;
                        var new_lng = this.getLatLng().lng();
			
			this.openInfoWindowHtml('Wij rijden uw auto\'s nu voor. Een ogenblik geduld a.u.b.');
		
			jQuery.get(site_root + 'ajax/josse/getDealerHtml.php?id=' + this.ws_id, function(data){
				clicked_marker.openInfoWindowHtml(data, {'onOpenFn': function () { map.panTo(new GLatLng(new_lat, new_lng));  } });
			});
		
		});
		
		return marker;
	}
	this.isWithin = function(sw,ne){
	
		return (sw.lat() < this.coordinate.lat() && ne.lat() > this.coordinate.lat()
				&&
			sw.lng() < this.coordinate.lng() && ne.lng() > this.coordinate.lng()
		);

	
	}
	
	
}



function drawZoomLocations(){
	var zoomlocations = new Array();
	
    var icon = new GIcon();
    icon.iconSize = new GSize(35, 35);
    icon.iconAnchor = new GPoint(17.5, 17.5);
    icon.image = site_root + 'images/googlemaps/bosch-vglas.png';
	
	zoomlocations.push(new GLatLng(53.199452,5.795288));
	zoomlocations.push(new GLatLng(53.215079,6.565704));
	zoomlocations.push(new GLatLng(52.994950,6.558838));
	zoomlocations.push(new GLatLng(52.515803,5.480118));
	zoomlocations.push(new GLatLng(51.984880,5.917511));
	zoomlocations.push(new GLatLng(50.851908,5.700531));
	zoomlocations.push(new GLatLng(51.691288,5.309143));
	zoomlocations.push(new GLatLng(52.378953,4.633484));
	zoomlocations.push(new GLatLng(52.512878,6.095352));
	zoomlocations.push(new GLatLng(52.091531,5.117226));
	zoomlocations.push(new GLatLng(51.498271,3.612442));
	zoomlocations.push(new GLatLng(52.068111,4.297714));
	
	for(var i=0; i<zoomlocations.length; i++){
		zoommarker = new GMarker(zoomlocations[i], icon);
		GEvent.addListener(zoommarker, 'click', function(){
			map.setCenter(this.getLatLng(), map.getZoom()+2);
		});
		map.addOverlay(zoommarker);
	}

}

function drawWS(callback){

	if (!callback){
		getWS();
	}
		
	for (var i=0; i<ws_index.length; i++){
		if (ws_index[i].isWithin(sw,ne)){
			ws_index[i].show();
		}
		else
			ws_index[i].hide();
	}
	
}

function getWS(){

	// our server knows which workshops have been indexed offline so it wont return these
	// our server returns 50 workshops at max
	
	// check the index
	bound = map.getBounds();
	sw = bound.getSouthWest();
	ne = bound.getNorthEast();
	
	// todo: make ajax call
	var request = GXmlHttp.create();
	request.open("GET", site_root + "ajax/josse/getDealers.php?sw=" + sw + '&ne=' + ne + '&session=' + gmapssession, true);
	request.onreadystatechange = function() {
		if (request.readyState == 4) {
		
			tmp  = request.responseText.split(/[\r\n]+/);
			for (var i=0; i<tmp.length; i++){
				if (tmp[i]!=''){
					if (tmp[i].substring(0,12) == 'GMapsSession'){
						gmapssession = tmp[i].substring(13,tmp[i].length);
					}
					else{
						ws = tmp[i].split(/\s*\,\s*/);
						ws_index.push(new WorkshopIndexItem(new GLatLng(ws[0]*1,ws[1]*1), ws[2]));
					}
				}
			}
			drawWS(true);
		}
	}
	request.send(null);
	
	

	

	

}

function load(){

	$('.no_js').removeClass('no_js');
	$('.has_js').css('display', 'none');

	$('img[@src$=.png], div.carousel').ifixpng();
	
	if (document.getElementById('googlemapoverview')){
		// this page has google maps overview!
		// init map
		if (GBrowserIsCompatible()) {
			map = new GMap2(document.getElementById('googlemapoverview'));
			map.setCenter(new GLatLng(52.500000,5.300000), 8);
			map.addControl(new GLargeMapControl());

			drawZoomLocations();
			
			GEvent.addListener(map, "zoomend", function(prevZoom, currZoom) {

				if (currZoom<=8){
					if (prevZoom>8){	// garages worden getoond. We gaan weer zoomlensjes weergeven
						map.clearOverlays();
						drawZoomLocations();
					}
					else{
					
					}
				}
				else{
					if (prevZoom<=8){	// zoomlensjes worden getoond, we gaan garages tonen
						map.clearOverlays();
						
					}

					drawWS();
				}
			
			});
			
			GEvent.addListener(map, 'dragend', function(){
				if (map.getZoom()<=8)
				{}
				else{
					drawWS();
				}
			});
			
			
			boschicon = new GIcon();
		    boschicon.image = "images/googlemaps/logo_bosch.png";
                    boschicon.iconSize = new GSize(53, 38);
                    boschicon.iconAnchor = new GPoint(16, 38);
                    boschicon.infoWindowAnchor = new GPoint(16, 38);

		}
		
	}

}
function emptySelectBox(el){
	len = el.options.length;
	for (var i=0; i<len; i++)
		el.options[len-i-1] = null;
}
function findModels(brand_index){
	// empty model list
	listitem = document.getElementById('model');
	emptySelectBox(listitem);

	if (brand_index<1){
		// no brand selected
		listitem.disabled = true;
		listitem.options[0] = new Option('Kies eerst een merk', 0);
		listitem.selectedIndex=0;
	}
	else{
		options = brand_options[brand_index-1];
		listitem.options[0] = new Option('Alle modellen', 0);
		for (var i=0; i<options.length; i++){
			listitem.options[i+1] = new Option(options[i], options[i]);
		}
		listitem.disabled = false;
	}
}

function findTypeSpec(){

	// dit doen we met ajax
	listitem = document.getElementById('uitvoering');
	emptySelectBox(listitem);
	
	if (document.getElementById('model').selectedIndex<1){
		listitem.disabled = true;
		listitem.options[0] = new Option('Kies eerst een model', 0);
		listitem.selectedIndex = 0;
	}
	else{
		

		listitem.disabled = true;
		listitem.options[0] = new Option('Even zoeken...', 0);
		listitem.selectedIndex = 0;
		jQuery.get(site_root + 'ajax/josse/getTypeSpec.php?merk=' + document.getElementById('merk').value + '&model=' + document.getElementById('model').value, function(data){
			
			listitem = document.getElementById('uitvoering');
			emptySelectBox(listitem);
			
			listitem.options[0] = new Option('Alle uitvoeringen', 0);
			
			tmp = data.split(/[\r\n]+/);
			for (var i=0; i<tmp.length; i++){
				if (tmp[i]!=''){
					listitem.options[listitem.options.length] = new Option(tmp[i], tmp[i]);
				}
			}
			listitem.disabled = false;	
		});

	
	}	

}

$('document').ready(function(){load();});


// Toon een element
function showID(id) {
    document.getElementById(id).style.display = "block";
    }

// Verberg een element
function hideID(id) {
    document.getElementById(id).style.display = "none";
    }

// Tabbladen
function tabblad (activeTab, name){
    tab_prefix = name ? 'tab_' + name + '_' : 'tab_';
    var i=1;
    while (element = document.getElementById(tab_prefix + i)){
        if (i == activeTab){
            element.className = 'current';
            document.getElementById('content' + tab_prefix + i).className = 'showit';
        }   
        else{
            element.className = '';
            document.getElementById('content' + tab_prefix + i).className = 'hide';
        }
        i++;
    }   
}

function initGoogleMapDetail(lat_txt,lng_txt,user_id){
	map = new GMap2(document.getElementById('detailmap'));
	map.setCenter(new GLatLng(lat_txt,lng_txt), 8);
	map.addControl(new GLargeMapControl());
	ws_index[0] = new WorkshopIndexItem(new GLatLng(lat_txt*1, lng_txt*1), user_id);
	ws_index[0].show();
}

