var flashvars = {};
var params = {};
var attributes = {};
swfobject.embedSWF("jyvas-parkki_knoppitieto.swf", "replace_with_flash", "580", "330", "10.0.0", false, flashvars, params, attributes);

jQuery.noConflict();

jQuery(document).ready(function($)
{
	// Carousel - activate .first element
	$('div#parkki_navigation ul.navigation li.first').addClass('active');
	
	// Carousel - emphasize current after selection
	function carouselActivateCurrent(o)
	{
		// Current selected li class name
		$carouselSelected = $(o[0]).attr('class');
		
		// Activate the one which has current li's class name in navigation
		$('div#parkki_navigation ul.navigation li').each( function()
		{
			if ($(this).hasClass($carouselSelected))
			{
				$(this).addClass('active');
			}
		});
	}
	
	$("div#pysakointitalot").jCarouselLite(
	{
		auto: null,
		circular: false,
		speed: 1000,
		visible: 1,
		scroll: 1,
		btnGo: ["div#parkki_navigation .navigation .pysakointitalot", "div#parkki_navigation .navigation .p-asema", "div#parkki_navigation .navigation .p-cygnaeus", "div#parkki_navigation .navigation .p-kolmikulma", "div#parkki_navigation .navigation .p-matkakeskus", "div#parkki_navigation .navigation .p-paviljonki", "div#parkki_navigation .navigation .p-sairaala", "div#parkki_navigation .navigation .p-sokos", "div#parkki_navigation .navigation .p-tori"],
		beforeStart: function() { $('div#parkki_navigation ul.navigation li.active').removeClass('active'); },
		afterEnd: function(o) { carouselActivateCurrent(o); }
	});
	
	// Yhteydenottolomakkeen validointi
	$("form.yhteydenotto").validate(
	{
		errorElement: "span",
		rules:
		{
			yhteydenotto_nimi: { required: true, minlength: 2 },
			yhteydenotto_email: { required: true, email: true },
			yhteydenotto_viesti: { required: true, minlength: 10 }
		},
		messages:
		{
			yhteydenotto_nimi: " on pakollinen",
			yhteydenotto_email: { required: " on pakollinen", email: " on virheellinen"},
			yhteydenotto_viesti: " on pakollinen"
		},
		errorPlacement: function(error, element)
		{
			error.appendTo( element.prev() );
		}
	});
	
});


/* Google map funktioita
	-----------------------------------------------------------------*/
function initGarageArray() {
	
	// Parking garages 
	
	
	garagearray = new Array();
	
	garagearray[0] = new Array();
	garagearray[0][0] = 'P-Asema';
	garagearray[0][1] = 62.242406;
	garagearray[0][2] = 25.753026;
	garagearray[0][5] = '<span class="nimi"><b>P-Asema</b></span><br /><span class="infot">Väinönkatu 8<br />Avoinna 24h<br /><a href="p-asema.php">Katso täältä kokorajoitukset ja hinnat.</a></span>';
	
	garagearray[1] = new Array();
	garagearray[1][0] = 'P-Kolmikulma';
	garagearray[1][1] = 62.244708;
	garagearray[1][2] = 25.751706;
	garagearray[1][5] = '<span class="nimi"><b>P-Kolmikulma</b></span><br /><span class="infot">Ilmarisenkatu 1<br />Avoinna 24h<br /><a href="p-kolmikulma.php">Katso täältä kokorajoitukset ja hinnat.</a></span>';
	
	garagearray[2] = new Array();
	garagearray[2][0] = 'P-Matkakeskus';
	garagearray[2][1] = 62.243139;
	garagearray[2][2] = 25.756041;
	garagearray[2][5] = '<span class="nimi"><b>P-Matkakeskus</b></span><br /><span class="infot">Hannikaisenkatu 22<br />Avoinna 24h<br /><a href="p-matkakeskus.php">Katso täältä kokorajoitukset ja hinnat.</a></span>';

	garagearray[3] = new Array();
	garagearray[3][0] = 'P-Paviljonki';
	garagearray[3][1] = 62.238622;
	garagearray[3][2] = 25.758991;
	garagearray[3][5] = '<span class="nimi"><b>P-Paviljonki</b></span><br /><span class="infot">Uno Savolan katu 6<br />Avoinna 24h<br /><a href="p-paviljonki.php">Katso täältä kokorajoitukset ja hinnat.</a></span>';

	garagearray[4] = new Array();
	garagearray[4][0] = 'P-Sairaala';
	garagearray[4][1] = 62.230715;
	garagearray[4][2] = 25.705519;
	garagearray[4][5] = '<span class="nimi"><b>P-Sairaala</b></span><br /><span class="infot">Kukkumäentie 34<br />Avoinna 24h<br /><a href="p-sairaala.php">Katso täältä kokorajoitukset ja hinnat.</a></span>';

	garagearray[5] = new Array();
	garagearray[5][0] = 'P-Sokos';
	garagearray[5][1] = 62.242320;
	garagearray[5][2] = 25.750569;
	garagearray[5][5] = '<span class="nimi"><b>P-Sokos</b></span><br /><span class="infot">Vapaudenkatu 55<br />Avoinna 24h<br /><a href="p-sokos.php">Katso täältä kokorajoitukset ja hinnat.</a></span>';

	garagearray[6] = new Array();
	garagearray[6][0] = 'P-Tori';
	garagearray[6][1] = 62.244349;
	garagearray[6][2] = 25.745226;
	garagearray[6][5] = '<span class="nimi"><b>P-Tori</b></span><br /><span class="infot">Yliopistonkatu 15<br />Avoinna 24h<br /><a href="p-tori.php">Katso täältä kokorajoitukset ja hinnat.</a></span>';
	
	garagearray[7] = new Array();
	garagearray[7][0] = 'P-Cygnaeus';
	garagearray[7][1] = 62.237288;
	garagearray[7][2] = 25.740515;
	garagearray[7][5] = '<span class="nimi"><b>P-Cygnaeus</b></span><br /><span class="infot">Vapaudenkatu 35<br />Avoinna 24h<br /><a href="p-cygnaeus.php">Katso täältä kokorajoitukset ja hinnat.</a></span>';
	}


function initializeMap() {
	
	// General map options
	
	centerpoint = new google.maps.LatLng(62.242406, 25.753026)
	
    Options = {
      zoom: 14,
      center: centerpoint,
      mapTypeId: google.maps.MapTypeId.ROADMAP
    };

    map = new google.maps.Map(document.getElementById("map_canvas"), Options);
	image = 'icon_2.png';
	
	// Direction options
	
	directionsService = new google.maps.DirectionsService();
	directionsDisplay = new google.maps.DirectionsRenderer();
	directionsDisplay.setMap(map);
    directionsDisplay.setPanel(document.getElementById("directionsPanel"));
	
	// Geocoder options
	
	geocoder = new google.maps.Geocoder();
	
}

function makeInfoWindowEvent(map, infowindow, marker) {  

		// Funktiotehdas, joka auttaa iteratiivisessa infoikkunan luonnissa
   		return function() {  
      		infowindow.open(map, marker);
   		};  
}

function initGarages() {
	
	// Lähintä hakua varten luodaan kartalle kaikki pysäköintitalot
	
	var i = 0;
		
	var garlength = garagearray.length;
	for (i = 0; i < garlength; i++)
	{
		garagearray[i][3] = new google.maps.LatLng(garagearray[i][1], garagearray[i][2])
		garagearray[i][4] = new google.maps.Marker({
   	   		map:map,
   	   		draggable:false,
   	   		animation: google.maps.Animation.DROP,
   	   		position: garagearray[i][3],
		  	icon: image,
		  	title: garagearray[i][0]
		});
		
		// Init InfoWindows
		
		garagearray[i][6] = new google.maps.InfoWindow({
        	content: garagearray[i][5]
		});
		
		google.maps.event.addListener(garagearray[i][4], 'click', makeInfoWindowEvent(map, garagearray[i][6], garagearray[i][4]));;
	}
	
}


function initGaragesFrontPage() {
	
	image = 'icon_2.png';

	// Etusivun karusellia varten luodaan sekä kartat että kohteet
	
	var i = 0;
		
	var garlength = garagearray.length;
	for (i = 0; i < garlength; i++)
	{
		
		// Luodaan koordinaatit uudelle objektille
		garagearray[i][3] = new google.maps.LatLng(garagearray[i][1], garagearray[i][2]);
		
		// Iteraation omat kartta-asetukset
		var garoptions = {
      		zoom: 14,
      		center: garagearray[i][3],
      		mapTypeId: google.maps.MapTypeId.ROADMAP
    	};
		
		// Luodaan karttaobjekti arrayyn
		garagearray[i][10] = new google.maps.Map(document.getElementById("map_canvas_"+garagearray[i][0]), garoptions);
		
		garagearray[i][4] = new google.maps.Marker({
   	   		map:garagearray[i][10],
   	   		draggable:false,
   	   		animation: google.maps.Animation.DROP,
   	   		position: garagearray[i][3],
		  	icon: image,
		  	title: garagearray[i][0]
		});
		
		// Init InfoWindows
		/*
		garagearray[i][6] = new google.maps.InfoWindow({
        	content: garagearray[i][5]
		});
		
		google.maps.event.addListener(garagearray[i][4], 'click', makeInfoWindowEvent(garagearray[i][10], garagearray[i][6], garagearray[i][4]));
		*/
	}
	
}


function codeAddress(addressinput) {
	
	// Geocode the address
	
    var addressjkl = addressinput + ", Jyväskylä";
    geocoder.geocode( { 'address': addressjkl}, function(results, status) {
      if (status == google.maps.GeocoderStatus.OK) {
		 
		 // Call for calcRoute
		 closestGarage(results[0].geometry.location);
	  
      } else {
        alert("Osoitteen paikantamisessa tapahtui seuraava virhe: " + status);
      }
    });
	
	return false;
 }


function sortByMatka(a, b)
{
	/**
	* Hienosti vastaanotettu tekijältä Hobo.fi
	* Alkuperäinen tietty pöllitty jostain netistä
	*/
	var x = a.distance;
	var y = b.distance;
	return ((x < y) ? -1 : ((x > y) ? 1 : 0));
}


function closestGarage(start) {
	
	// Find the closest Garage to the user entered address
	
	distancearray = new Array();
	
	var current_lat = start.lat();
	var current_lng = start.lng();
	
	var i = 0;
	for (i = 0; i <= garagearray.length - 1; i++)
	{
		var offset_lat = garagearray[i][1] - current_lat;
		var offset_lng = garagearray[i][2] - current_lng;
		distancearray[i] = {id:i, distance:Math.sqrt(offset_lat*offset_lat + offset_lng*offset_lng)};
	}
	
	distancearray.sort(sortByMatka);
	
	// Avaa lähimmän pysäköintitalon infoikkuna
	garagearray[distancearray[0].id][6].open(map, garagearray[distancearray[0].id][4]);
	calcRoute(start,garagearray[distancearray[0].id][3]);
	
}
	
	
function calcRoute(start,end) {
	
	var request = {
        origin:end, 
        destination:start,
        travelMode: google.maps.DirectionsTravelMode.WALKING
    };
	
    directionsService.route(request, function(response, status) {
      if (status == google.maps.DirectionsStatus.OK) {
		directionsDisplay.setDirections(response);
      }
	});
 
}

/*var function drawZones() {
	 
     vyohyke1 = [
        new google.maps.LatLng(62.242575, 25.742630),
        new google.maps.LatLng(62.244161, 25.745848),
		new google.maps.LatLng(62.244539, 25.745006),
		new google.maps.LatLng(62.245196, 25.746320),
		new google.maps.LatLng(62.244843, 25.747200),
		new google.maps.LatLng(62.245967, 25.749480),
        new google.maps.LatLng(62.246012, 25.750902),
		new google.maps.LatLng(62.244571, 25.754265),
        new google.maps.LatLng(62.244376, 25.755767),
		new google.maps.LatLng(62.243687, 25.755703),
		new google.maps.LatLng(62.242655, 25.755113),
		new google.maps.LatLng(62.241823, 25.754281),
		new google.maps.LatLng(62.239559, 25.749577),
		new google.maps.LatLng(62.240901, 25.746406),
		new google.maps.LatLng(62.240841, 25.746278),
		new google.maps.LatLng(62.241111, 25.745655),
		new google.maps.LatLng(62.241206, 25.745730),
		new google.maps.LatLng(62.241471, 25.745173),
		new google.maps.LatLng(62.240291, 25.742823),
		new google.maps.LatLng(62.240381, 25.742662),
		new google.maps.LatLng(62.241546, 25.745022)
    ];*/
	
function drawZones() {
	 
    vyohyke1 = [
        new google.maps.LatLng(62.241361, 25.740216),
new google.maps.LatLng(62.24263, 25.74264), 
new google.maps.LatLng(62.24283, 25.742447), 
new google.maps.LatLng(62.246187, 25.743606), 
new google.maps.LatLng(62.246307, 25.743949), 
new google.maps.LatLng(62.245868, 25.744851), 
new google.maps.LatLng(62.246837, 25.746889), 
new google.maps.LatLng(62.245888, 25.749164), 
new google.maps.LatLng(62.245997, 25.749485), 
new google.maps.LatLng(62.246037, 25.750816), 
new google.maps.LatLng(62.244549, 25.75455), 
new google.maps.LatLng(62.244329, 25.755858), 
new google.maps.LatLng(62.243239, 25.755601), 
new google.maps.LatLng(62.243189, 25.755944), 
new google.maps.LatLng(62.24308, 25.755923), 
new google.maps.LatLng(62.24313, 25.755494), 
new google.maps.LatLng(62.24225, 25.754871), 
new google.maps.LatLng(62.24169, 25.754056), 
new google.maps.LatLng(62.241471, 25.754614), 
new google.maps.LatLng(62.241151, 25.754077), 
new google.maps.LatLng(62.241351, 25.753369), 
new google.maps.LatLng(62.239852, 25.750258), 
new google.maps.LatLng(62.238312, 25.747168), 
new google.maps.LatLng(62.241261, 25.740409), 
new google.maps.LatLng(62.241361, 25.740216)
];

var vyohyke2 = [[

new google.maps.LatLng(62.238982,25.73601),
new google.maps.LatLng(62.239242,25.73586),
new google.maps.LatLng(62.241301,25.740066),
new google.maps.LatLng(62.238003,25.747855),
new google.maps.LatLng(62.236983,25.745795),
new google.maps.LatLng(62.237203,25.745001),
new google.maps.LatLng(62.235983,25.742855),
new google.maps.LatLng(62.236863,25.740988),
new google.maps.LatLng(62.238912,25.73616),
new google.maps.LatLng(62.238982,25.73601)

],[

new google.maps.LatLng(62.247776,25.740731),
new google.maps.LatLng(62.248006,25.741203),
new google.maps.LatLng(62.248176,25.740795),
new google.maps.LatLng(62.247926,25.740323),
new google.maps.LatLng(62.247776,25.740731)								  

],[

new google.maps.LatLng(62.246757,25.746675),
new google.maps.LatLng(62.247296,25.745623),
new google.maps.LatLng(62.247846,25.745645),
new google.maps.LatLng(62.247826,25.746009),
new google.maps.LatLng(62.247386,25.745945),
new google.maps.LatLng(62.246877,25.746889),
new google.maps.LatLng(62.246757,25.746675)

],[

new google.maps.LatLng(62.247127,25.747447),
new google.maps.LatLng(62.247137,25.747726),
new google.maps.LatLng(62.247766,25.747705),
new google.maps.LatLng(62.247776,25.747383),
new google.maps.LatLng(62.247127,25.747447)

],[

new google.maps.LatLng(62.245268,25.752747),
new google.maps.LatLng(62.246287,25.754743),
new google.maps.LatLng(62.246127,25.755107),
new google.maps.LatLng(62.245158,25.753047),
new google.maps.LatLng(62.245268,25.752747)

],[

new google.maps.LatLng(62.24164,25.740731),
new google.maps.LatLng(62.24191,25.740108),
new google.maps.LatLng(62.24253,25.741546),
new google.maps.LatLng(62.24233,25.742126),
new google.maps.LatLng(62.24164,25.740731)

]];
	

	
    // Construct the polygon
    RED_vyohyke1 = new google.maps.Polygon({
      paths: vyohyke1,
      strokeColor: "#e90011",
      strokeOpacity: 0.8,
      strokeWeight: 2,
      fillColor: "#e90011",
      fillOpacity: 0.2
    });
	
	YELLOW_vyohyke2 = new google.maps.Polygon({
      paths: vyohyke2,
      strokeColor: "#eef750",
      strokeOpacity: 0.8,
      strokeWeight: 2,
      fillColor: "#eef750",
      fillOpacity: 0.2
    });

   RED_vyohyke1.setMap(map);
   YELLOW_vyohyke2.setMap(map);
   
   
   RED_infowindow = new google.maps.InfoWindow({
        content: 'Vyöhyke 1 - arkisin 8 - 20, la (8 - 16)&nbsp;&nbsp;<br><br>2€ / tunti<br><br>Vuorokausi 24€ (lauantait 16€)',
		position: new google.maps.LatLng(62.242620, 25.749249)
    });
   
   YELLOW_infowindow = new google.maps.InfoWindow({
        content: 'Vyöhyke 2 - arkisin 8 - 18, la (8 - 16)&nbsp;&nbsp;<br><br>ensimmäinen tunti 2€<br>toinen tunti 1€<br>kolmas tunti 1€<br>neljäs tunti 1€<br>seuraavat tunnit á 0,5€<br><br>Vuorokausi 8€ (lauantait 7€)',
		position: new google.maps.LatLng(62.239102, 25.742662)
    });
   
    
    
	google.maps.event.addListener(RED_vyohyke1, 'click', function() {
		RED_infowindow.open(map);
	});
	
	google.maps.event.addListener(YELLOW_vyohyke2, 'click', function() {
		YELLOW_infowindow.open(map);
	});
	
	
	
	
	google.maps.event.addListener(RED_vyohyke1,"mouseover",function(){ this.setOptions({fillOpacity: 0.5}); });
	google.maps.event.addListener(RED_vyohyke1,"mouseout",function(){ this.setOptions({fillOpacity: 0.2}); });
	
	google.maps.event.addListener(YELLOW_vyohyke2,"mouseover",function(){ this.setOptions({fillOpacity: 0.75}); });
	google.maps.event.addListener(YELLOW_vyohyke2,"mouseout",function(){ this.setOptions({fillOpacity: 0.2}); });
	
	

}
