// JavaScript Document

var map;
var geocoder;
var directionDisplay;
var directionsService = new google.maps.DirectionsService();
var markers = new Array();

$(document).ready(
	function()
	{	
		randomize_background();
		$("#calendar_info").dialog(
			{ 
				autoOpen: false, 
				modal: true, 
				resizable: false,
				width: 'auto',
				show: "fold",
				hide: "fold"
			}
		);
		load_page("news");
		prepare_calendar();
	}	
);

function randomize_background()
{
	var background = Math.floor(Math.random() * 3) + 1;
	$("#content").css("background-image", "url(./images/content_background_" + background + ".png)");
}

function load_page(page_name)
{
	$("#text").load("index.php/standard/load_page/" + page_name + "/" +  Math.random()*99999);
}

function load_admin_page(page_name)
{
	$("#admin_content").load("index.php/admin/load_page/" + page_name + "/" +  Math.random()*99999);
}

function prepare_calendar()
{	
	var dates = new Array();
	
	$.getJSON('index.php/standard/json_wedstrijden/', function(data) {		
		$.each(data, function(key, state) {	
			dates.push(new Date(state.date.split("-")[0], parseInt(state.date.split("-")[1].replace(/^[0]+/g,"")) - 1, state.date.split("-")[2]));
		});
		
		$("#calendar").datepicker({
			dateFormat: 'dd-mm-yy',
			beforeShowDay: function(date) {
				for (var i = 0; i < dates.length; i++) {
					if (dates[i] - date == 0) {
							return [true, 'highlight'];
					}
				}
				return [true, ''];	
			},
			onSelect: function(dateText, inst) {
				var formatted_date = dateText.split("-")[2] + "-" + dateText.split("-")[1] + "-" + dateText.split("-")[0];
				$( "#calendar_info" ).dialog("option", "title", "Wedstrijden op " + dateText);
				var titles = "";
				
				$("#calendar_info").html("");
				$.getJSON('index.php/standard/json_wedstrijden_for_date/' + formatted_date, function(data) {
					titles = "<p>Klik op een wedstrijd voor meer info over deze wedstrijd.<p><ul>";
					
					$.each(data, function(key, state) {	
						titles = titles + "<li onclick='get_wedstrijd_info(" + state.id + ")'><span>" + state.title + "</span></li>";
					});
					
					titles = titles + "</ul>"; 
					$("#calendar_info").html(titles);
					$("#calendar_info").dialog("open");
				});
			}
		});	
	});
}

function get_wedstrijd_info(id)
{
	if($("#calendar_info").dialog("isOpen") == true)
	{
		$("#calendar_info").dialog("close");
	}
	$("#text").load("index.php/standard/load_wedstrijd_info/" + id);		
}

function show_chalet_preview(location)
{
	$("#chalet_preview img").attr("src", "images/chalets/" + location);
	$("#chalet_preview").dialog("open");
}

function show_clubs_edit(id)
{
	$("tr[id^='rowClubs']").show();
	$("tr[id^='rowClubsEdit']").hide();
	$("#rowClubs" + id).hide();
	$("#rowClubsEdit" + id).show();
}

function show_uitslag_edit(id)
{
	$("tr[id^='rowUitslag']").show();
	$("tr[id^='rowUitslagEdit']").hide();
	$("#rowUitslag" + id).hide();
	$("#rowUitslagEdit" + id).show();
}

function show_news_edit(id)
{
	$("tr[id^='rowNews']").show();
	$("tr[id^='rowNewsEdit']").hide();
	$("#rowNews" + id).hide();
	$("#rowNewsEdit" + id).show();
}

function show_chalet_edit(id)
{
	$("tr[id^='rowChalet']").show();
	$("tr[id^='rowChaletEdit']").hide();
	$("#rowChalet" + id).hide();
	$("#rowChaletEdit" + id).show();
}

function show_cat_edit(id)
{
	$("tr[id^='rowCat']").show();
	$("tr[id^='rowCatEdit']").hide();
	$("#rowCat" + id).hide();
	$("#rowCatEdit" + id).show();
}

function show_ann_edit(id)
{
	$("tr[id^='rowAnn']").show();
	$("tr[id^='rowAnnEdit']").hide();
	$("#rowAnn" + id).hide();
	$("#rowAnnEdit" + id).show();
}

function view_updatewedstrijd(id, date, title, descr)
{
	$("#form_wedstrijden_2").attr("action", "index.php/admin/update_wedstrijd/" + id);
	$("#form_wedstrijden_2 #txtDate_2").val(date);
	$("#form_wedstrijden_2 #txtTitle").val(title);
	$("#form_wedstrijden_2 #txtDescription").val(descr);
	$("#update_wedstrijd").dialog("open");
}

function view_updatefoto(id, descr, cat)
{
	$("#form_foto_2").attr("action", "index.php/admin/update_omschrijving_foto/" + id);
	$("#form_foto_2 #txtDescr").val(descr);
	$("#form_foto_2 #txtCat").val(cat);
	$("#update_foto").dialog("open");
}

function view_updategastenboek(id, naam, mail, message)
{
	$("#form_guestbook_1").attr("action", "index.php/admin/update_message/" + id);
	$("#form_guestbook_1 #txtNaam").val(naam);
	$("#form_guestbook_1 #txtMail").val(mail);
	$("#form_guestbook_1 #txtBericht").val(message);
	$("#update_message").dialog("open");
}

function view_addchaletfoto(id)
{
	$("#chalet_foto").attr("action", "index.php/admin/add_chalet_foto/" + id);
	$("#add_chalet_foto").dialog("open");
}

function view_addmessage()
{
	$("#post_message").dialog("open");
	$("#frmPostMessage").ajaxForm(
		function() 
		{
			$("#post_message").dialog("close");
			load_page('guestbook');
		}
	);
}

function switch_guestbook_page(start)
{
	$("#text").load("index.php/standard/load_gastenboek/" + start);
	//scroll(0, 0);	
}

function view_update_results(id)
{
	$("#admin_content").load("index.php/admin/load_uitslagen/" + id);	
}

function show_upload(docname)
{
	$("#frmDocument").show();
	$("#frmDocument").attr("action", "index.php/admin/change_reglement/" + docname.split(".")[0]);
	$("#doc_name").html("U kan onderaan een nieuw document opladen voor " + docname + ":");	
}

function load_fotos(categorie)
{
	$("#text").load("index.php/standard/load_fotoalbum/" + categorie + "/" +  Math.random()*99999, function() {
		$("#photoalbum").photoalbum();
	});	
}

function load_fotos_for_categorie(categorie)
{
	$("#admin_content").load("index.php/admin/load_fotoalbum/" + categorie + "/" +  Math.random()*99999);	
}

function google_map()
{
	directionsDisplay = new google.maps.DirectionsRenderer( { suppressInfoWindows: true, suppressMarkers: true, preserveViewport: true } ); 
	geocoder = new google.maps.Geocoder();  
	var myOptions = {zoom: 15, mapTypeId: google.maps.MapTypeId.ROADMAP};     
	map = new google.maps.Map(document.getElementById("map_canvas"), myOptions); 
	
	directionsDisplay.setMap(map); 
	directionsDisplay.setPanel(document.getElementById("map_panel"));
	
	get_location('Barelstraat 228, 2880 Bornem', false);	
}

function get_location(val, guest) 
{
	var address;
	var correct = false;
	if(guest == false){
		address = val;
		correct = true;
	} else {
		address = $("#txtRoute").val();
		if($("#txtRoute").val() != ""){
			correct = true;
		}
	}
	
	if(correct == true){
		geocoder.geocode( 
			{'address': address}, function(results, status) {
				if (status == google.maps.GeocoderStatus.OK) {
					place_marker(results[0].geometry.location, guest);
				} else {
					alert("Geocode was not successful for the following reason: " + status);
				}
			}
		);
	}
}

function place_marker(location, guest)
{
	map.setCenter(location);
	var marker = new google.maps.Marker({
		position: location, 
		map: map
	}); 
	
	if(guest == false){
		markers[0] = marker;
	} else {
		markers[1] = marker;
		calc_route();
	}
}

function calc_route()
{
	var request = {
		origin: markers[1].getPosition(), 
		destination: markers[0].getPosition(),
		travelMode: google.maps.DirectionsTravelMode.DRIVING
	};
	directionsService.route(request, function(response, status) {
		if (status == google.maps.DirectionsStatus.OK) {
			directionsDisplay.setDirections(response);
		}
	});	
}
