var map = null;
var geocoder = null;
var selectReady = 3;

$(document).ready(function(){
	$('ul.accordion').accordion({
		active: ".selected",
		autoHeight: false,
		header: ".opener",
		collapsible: true,
		event: "click"
	});

	$('ul.accordion2').accordion({
		autoHeight: false,
		header: ".opener",
		collapsible: true,
		fillSpace: true,
		event: "click"
	});	
	
	if ($("#googleMap").length > 0)
	{
		initGoogleMaps();
		setMarkers();
		handleFilters();
	}
	
	HandleTextBoxes();
	initValidation("#steleenvraag");
	initValidation("#deelnameform");
	initValidation("#contactform");
	initVideoWidget();
});

/* Google Maps scripts */
function Instelling(naam, land, plaats, postcode, coords, straat, link, inst, sectoren)
{
	this.showOnMap = true;
	this.naam = naam;
	this.land = land;
	this.plaats = plaats;
	this.straat = straat
	this.postcode = postcode;
	this.inst = inst;
	this.sectoren = sectoren;
	this.link = link;
	this.coords = coords;
}

function initGoogleMaps(){
	if ($("#googleMap").length > 0)
	{
		map = new GMap2(document.getElementById("googleMap"));
		map.setCenter(new GLatLng(51.778887, 5.968323), 7);
		geocoder = new GClientGeocoder();
		
		map.setUIToDefault();
	}
}

function setMarkers(){
	map.clearOverlays();	
	for (var i = 0; i < collectie.length; i++)
	{
		if (collectie[i].showOnMap)
		{
		var markernaam = ""+collectie[i].naam;
			var postcode = collectie[i].postcode;
			var plaats = collectie[i].plaats;
			var straat = collectie[i].straat;
			var land = collectie[i].land;
			var link = collectie[i].link;
			
			if (collectie[i].coords == "")
				getLatLong(markernaam,link,postcode +" "+ plaats +" "+ land);
			else
			{
				var coords = collectie[i].coords;
				coords = coords.split(",");
				var html = '<a href="'+link+'">'+markernaam+'</a>';
				addMarker(html, new GLatLng(coords[0],coords[1]),"/images/red-dot.gif");
			}
		}
	}
}

function getLatLong(naam, link, adres){
	//if (isDebug())
		//alert(naam +" - "+ adres);
	geocoder.getLatLng(
		adres,
		function(point){
			if (point)
			{
				//if (isDebug())
					//alert(naam);
				var html = '<a href="'+link+'">'+naam+'</a>';
				addMarker(html,point,"/images/red-dot.gif");
			}
			else
			{
				if (isDebug())
					alert(naam +" -"+adres +" | niet gevonden");
			}
		});
}

function addMarker(markernaam, point, image){
		//map.setCenter(point, 13);
		var icon = new GIcon();
		icon.image = image;
		icon.iconSize = new GSize(32, 32);
		icon.iconAnchor = new GPoint(0, 20);
		icon.infoWindowAnchor = new GPoint(5, 1);
		
		
        var marker = new GMarker(point, icon);
        GEvent.addListener(marker, "click", function(){
			marker.openInfoWindowHtml(markernaam);
		});
		map.addOverlay(marker);
        //marker.openInfoWindowHtml(adres);
	}

function in_array(stringToSearch, arrayToSearch) {
	for (s = 0; s < arrayToSearch.length; s++) {
		thisEntry = arrayToSearch[s].toString();
		if (thisEntry == stringToSearch)
		   return true;
	}
	return false;
}

function handleFilters(){

	$(".map-form select").change(function(){
		doFilter();
	});
}

function doFilter(){
	if (selectReady == 0)
	{
		var inst = $("#instelling").val();
		var land = $("#landkeuze").val();
		var sector = $("#sector").val();
		
		for (var i = 0; i < collectie.length; i++)
		{		
			if (collectie[i].inst == inst || inst == "all")
			{
				collectie[i].showOnMap = true;
	
				if (collectie[i].land == land  || land == "all")
				{
					if (sector != "all")
					{
						if (!in_array(sector, collectie[i].sectoren))
							collectie[i].showOnMap = false;
					}
				}	
				else
					collectie[i].showOnMap = false;
			}
			else
				collectie[i].showOnMap = false;
		}
		setMarkers();
	}
	else
		selectReady--; /* If 3 selects have been chenged (happens on onload) then we can filter */
}

function HandleTextBoxes(){
	$("input:text").click(function(){
		$(this).val("");
	});
}

function initVideoWidget(){
	$(".video .play").click(function(e){		
		e.preventDefault();
		var videoPath = $(this).attr("href");
		if (videoPath != "")
		{
			openVideoOverlay(videoPath);
		}
	});
}
function openVideoOverlay(video){
	if (video)
	{
		$("body").overlay({
			opacity: 75,
			window_width: 640,
			window_height: 480,
			close_img: '/images/btn-close.png',
			src: '<div class="overlay-video"><div id="videobox"></div></div>',
			callback: 'attachVideo("'+video+'")'
		});
		attachVideo(video);
	}
}

function attachVideo(video){
	var flashvars = { 
		videoPath			: video, 
		Width				: "640", 
		Height				: "480",
		vColor				: "0xc43930",
		rollColor			: "0xFFFFFF",
		AutoPlay			: "true",
		vidLoop				: "false",
		hideTime			: "6000"
	}; 
	var params = { 
	  allowFullScreen	: "true",
	  wmode				: "transparent"
	}; 
	var attributes = { 
	  id: "videobox", 
	  name: "myDynamicContent" 
	}; 
	swfobject.embedSWF("/swf/Advanced_video_player.swf", "videobox", "640", "480", "9.0.0","expressInstall.swf", flashvars, params, attributes); 

	
}

function isDebug(){
	var url = ""+window.location;
	url = url.split("?");
	if (url.length > 1)
	{
		var params = url[1].split("=");
		if (params.length > 1)
		{
			if (params[0] == "debug")
			return true;
		}
	}
	return false;
}
