var map = null;
var geocoder = null;

function initialize() {
  
  if (GBrowserIsCompatible()) {
    
	map = new GMap2(document.getElementById("map_canvas"));
    map.setCenter(new GLatLng(37.4419, -122.1419), 13);
    geocoder = new GClientGeocoder();
    
    var address = $('#addr').html();
    var info = address.match(/([A-Za-z\.\ ]+)/);
    
    var city = $('#location').html();
    var infoCity = city.match(/([A-Za-z\.\ ]+)/);
    
    showAddress(info[0]+" "+infoCity[0]);
    
  }
  
}

function initContactMap() {
	  
	  if (GBrowserIsCompatible()) {
	    
		map = new GMap2(document.getElementById("map_contact"));
	    map.setCenter(new GLatLng(53.1583,6.440735), 13);
	    
	    geocoder = new GClientGeocoder();
	    
	    showAddress("Friesestraatweg 18 Groningen");
	    
	  }
	  
	}
	
	

function initMap(address) 
{
	if(address == '')
		return;
	 	
	
	if (GBrowserIsCompatible()) 
	{	   
		map = new GMap2(document.getElementById("map_address"));
		map.setCenter(new GLatLng(53.1583,6.440735), 13);
	
		geocoder = new GClientGeocoder();
	
		showAddress(address);	    
	}	  
}	

function showAddress(address) {
  if (geocoder) {
    geocoder.getLatLng(
      address,
      function(point) {
        if (!point) {
          alert(address + " niet gevonden");
        } else {
          
          map.setCenter(point, 15);
          var marker = new GMarker(point);
          map.addOverlay(marker);
          map.addControl(new GSmallZoomControl());
          //marker.openInfoWindowHtml(address);
          
        }
      }
    );
  }
}



function initMenuMain() {
	
	/**
	 * Menu main
	 */
	    
	$('ul.sf-menu').superfish();
	 
	
    /**
     * Menu item huren
     */
    
	$('li.mm1').mouseover(function() {	
		
		$(this).children('a').children('img').attr('src', $(this).children('a').children('img').attr('src').replace('huren','huren_active'));
	
	});
	
	$('li.mm1').mouseout(function() {
	
		$(this).children('a').children('img').attr('src', $(this).children('a').children('img').attr('src').replace('huren_active','huren'));
	
	});
	
	/**
     * Menu item kopen
     */
    
	$('li.mm2').mouseover(function() {	
		
		$(this).children('a').children('img').attr('src', $(this).children('a').children('img').attr('src').replace('kopen','kopen_active'));
	
	});
	
	$('li.mm2').mouseout(function() {
	
		$(this).children('a').children('img').attr('src', $(this).children('a').children('img').attr('src').replace('kopen_active','kopen'));
	
	});
	
	/**
     * Menu item huurder
     */
    
	$('li.mm3').mouseover(function() {	
		
		$(this).children('a').children('img').attr('src', $(this).children('a').children('img').attr('src').replace('huurder','huurder_active'));
	
	});
	
	$('li.mm3').mouseout(function() {
	
		$(this).children('a').children('img').attr('src', $(this).children('a').children('img').attr('src').replace('huurder_active','huurder'));
	
	});
	
	/**
     * Menu item over ons
     */
    
	$('li.mm4').mouseover(function() {	
		
		$(this).children('a').children('img').attr('src', $(this).children('a').children('img').attr('src').replace('over-ons','over-ons_active'));
	
	});
	
	$('li.mm4').mouseout(function() {
	
		$(this).children('a').children('img').attr('src', $(this).children('a').children('img').attr('src').replace('over-ons_active','over-ons'));
	
	});
	
}

function initRentalForm() {
	
	try {
	
		var formOptions = { success: modifyForm };
		
		$('#frmRental').ajaxForm(formOptions);
				
		$('#locations').change(function() {
						
			$('#frmAction').attr( {value: 'updDistrict'} );
			$('#frmRental').submit();
		
		});
		
		$('#districts').change(function() 
		{		
			$('#frmAction').attr( {value: 'updStreet'} );
			$('#frmRental').submit();
		
		});
		
		$('#streets').change(function() {
			
			$('#frmAction').attr( {value: 'updType'} );
			$('#frmRental').submit();
		
		});
		
		$('#frmRentalSubmit').click(function(){
			
			$('#frmAction').attr( {value: 'searchRental'} );
			$('#frmRental').ajaxFormUnbind();
			
		});
		
	} catch (e) {
		
		alert(e);
		
	}
	
}


function modifyForm(response, status) {
	
	if (status == "success") {
				
		switch ($('#frmAction').val()) {
		
			case "updDistrict":
				
				var opt = response.split(',');
				
				$('#districts').removeOption(/./);
				$('#districts').addOption("", "");
				
				for (i=0; i<opt.length; i++) {
					
					$('#districts').addOption(opt[i], opt[i]);
					
				}
				
				$('#districts').selectOptions("");
				
				break;
				
			case "updStreet":
				
				var opt = response.split(';');
				
				$('#streets').removeOption(/./);
				$('#streets').addOption("", "");
				
				for (i=0; i<opt.length; i++) {
					
					$('#streets').addOption(opt[i], opt[i]);
					
				}
				
				$('#streets').selectOptions("");
				
				break;
				
			case "updType":
				
				var opt = response.split(',');
				
				$('#types').removeOption(/./);
				$('#types').addOption("", "");
				
				for (i=0; i<opt.length; i++) {
					
					$('#types').addOption(opt[i], opt[i]);
					
				}
				
				$('#types').selectOptions("");
				
				break;
		
		}
		
	}
	
}


function loadMap() {
		
	$('#map_canvas').css('display','block');
	initialize();
	$('#image').fadeOut(function (){ showAddress('nieuw amsterdamstraat leeuwarden'); });
	
}


function handleATags() {
	
	$('#outerWrapper a').each(function (){
		
		if ($(this).attr('target') == '_blank') {
			
			$(this).click(function(){
				
				if (confirm("U opent nu een andere pagina. Weet u dit zeker? ")) {
					
					return true;
					
				} else {
					
					return false;
					
				}
				
			});
			
		}
	
	});
	
}


/**
 * DOM READY
 */

$(document).ready(function(){ 
	    
	/**
	 * Init menu main
	 */
	
	initMenuMain();
	
	/*
	 $("ul.sf-menu").superfish({ 
         animation: {height:'show'},   // slide-down effect without fade-in 
         delay:     1200               // 1.2 second delay on mouseout 
     });
	*/
	/**
	 * Init rental search form
	 */
	
	if ($('#frmRental')) {
			
		initRentalForm();
		
	}
	
	/**
	 * Set FAQ
	 */
	
	if ($('#accordion')) {
			
		$("#accordion").accordion( {  
			autoHeight: false,
			fillSpace: false
		} );
		
	}
	
	if ($('#map_contact').length > 0) {
		
		initContactMap();		
		
	}
	
	if ($('#map_canvas').length > 0) {
		
		initialize();
		
	}
	
	/**
	 * Set font switch
	 */
	
	if ($('.fontWrapper')) {
	
		var switches = $('.fontWrapper a');
				
		for (i=0; i<switches.length; i++) {
		
			$(switches[i]).click(function () {
				
				var size = $(this).attr('size');
				
				$.post("/", { action: 'setFont', size: size }, function (data) {
				
					if (data) {
											
						$("#fontSwitch").attr('href', '/themes/default/css/font/font_' + size + '.css');
						
					}
				
				});
			
			});
			
		}
		
	}
	
	handleATags();
	
	
	if($('#homeBottom').length == 1) {
		
		var pos = $("#addressInfo").position();
		$('#homeBottom').css({'position' : 'absolute', 'top': pos.top});
		
	}	
	
	
}); 
