/*global $, google */

function slideSwitch() {
	"use strict";
	var $next = "", $active = $("#slideshow img.active");

	if ($active.length === 0) {
		$active = $("#slideshow img:last");
	}

	$next = $active.next().length ? $active.next() : $("#slideshow IMG:first");

	$active.addClass("last-active");

	$next.css({ opacity: 0.0 })
		.addClass("active")
		.animate({ opacity: 1.0 }, 1000, function () {
			$active.removeClass("active last-active");
		});
}

function initializeGoogleMap() {
	var map, request, officeMarker, officeWindow, directionsDisplay, directionsService, officeLatLng;

	$("#map_canvas").css("width", "697px");
	$("#map_canvas").css("height", "400px");
	$("#map_canvas").html("Laddar karta...");
	$("#map_origin").css("display", "block");
	$("#map_textorigin").focus();

	$("#getDirection").submit(function () {
		request = {
			origin: $("#map_textorigin").val(), 
			destination: "Årsta Skolgränd 4, Stockholm, Sverige",
			travelMode: google.maps.DirectionsTravelMode.DRIVING,
		};
		directionsService.route(request, function(response, status) {
			if (status == google.maps.DirectionsStatus.OK) {
				directionsDisplay.setDirections(response);
				directionsDisplay.setPanel(document.getElementById("directionsPanel"));
			}
		});
		return false;
	});

	officeLatLng = new google.maps.LatLng(59.3016637, 18.0273936)
	directionsDisplay = new google.maps.DirectionsRenderer({
		draggable: true
	});
	directionsService = new google.maps.DirectionsService();

	map = new google.maps.Map(document.getElementById("map_canvas"), {
		zoom: 14,
		center: officeLatLng,
		mapTypeId: google.maps.MapTypeId.ROADMAP
	});

	directionsDisplay.setMap(map);

	officeMarker = new google.maps.Marker({
		position: officeLatLng
	});

	officeMarker.setMap(map);

	officeWindow = new google.maps.InfoWindow({
		content: "SAMSU Ideell f&ouml;rening<br />&Aring;rsta Skolgr&auml;nd 4<br />117 43, Stockholm<br />",
	});
	officeWindow.open(map, officeMarker);
		google.maps.event.addListener(officeMarker, "click", function() {
		officeWindow.open(map, officeMarker);
	});
		google.maps.event.addListener(map, "click", function(event) {
		prompt("Koordinater", event.latLng.lat() + "," + event.latLng.lng());
	});
}

function initializeContactForm() {
	var selectField = "", i = 0, error = false, checking = ["meddelande", "amne", "epost", "namn"], reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;

	$("#namn").focus();
	$("#contactForm").submit(function (event) {
		for (i = 0; i < checking.length; i = i + 1) {
			if ((checking[i] === "epost" && reg.test($("#" + checking[i]).val()) === false) || ($("#" + checking[i]).val() === null || $("#" + checking[i]).val() === "")) {
				error = true;
				$("#" + checking[i] + "error").css("display", "inline");
				selectField = checking[i];
			} else {
				$("#" + checking[i] + "error").css("display", "none");
			}
		}

		if (error) {
			$("#" + selectField).focus();
			error = false;
			return false;
		}
		$("input").attr("disabled", true);
		$("textarea").attr("disabled", true);
			$.post($("#contactForm").attr("action"), { namn: $("#namn").val(), epost: $("#epost").val(), amne: $("#amne").val(), meddelande: $("#meddelande").val(), kopia: $("#kopia").val(), javascript: 1 }, function (data) {
			$("#formbox").html(data);
		});
		event.preventDefault();
	});
}

function loadPage(currentID, currentHREF) {
	"use strict";

	if (currentID === "contact2") {
		currentID = "contact";
	}
	$.get("view" + currentHREF + "?rand=" + Math.random(), function (data) {
		if ($("#textcontent").html(data)) {
			$("li a").removeClass("activetab");
			$("#" + currentID).addClass("activetab");
		}

		if (currentID === "contact") {
			initializeContactForm();
		} else if (currentID === "findus") {
			initializeGoogleMap();
		}
	});
}

$(document).ready(function () {
	"use strict";
	//setInterval("slideSwitch()", 7500);

	if (document.getElementById("map_canvas")) {
		initializeGoogleMap();
	} else if (document.getElementById("contactForm")) {
		initializeContactForm();
	}

//	$("a").attr("href", "#");
	$("a").click(function (event) {
		event.preventDefault();
		loadPage($(this).attr("id"), $(this).attr("href"));
	});
});
