// JavaScript Document

// Extension to get URL vars from: http://jquery-howto.blogspot.com/2009/09/get-url-parameters-values-with-jquery.html
$.extend({
	getUrlVars: function () {
		var vars = [], hash;
		var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
		for (var i = 0; i < hashes.length; i++) {
			hash = hashes[i].split('=');
			vars.push(hash[0]);
			vars[hash[0]] = hash[1];
		}
		return vars;
	},
		getUrlVar: function (name){
		return $.getUrlVars()[name];
	}
});

// When de dom is ready
$(function (){
	
    if ($('div.slides div.item').length) {
		
		// Dit toegevoegd omdat de slideshow anders animaties cumuleert als het venster verborgen is en dan daarna alles snel afspeelt.
		function onBlur () {
			$("ul.slideTabs").data("slideshow").stop();
		}
		function onFocus () {
			$("ul.slideTabs").data("slideshow").play();
		}
		
		if (/*@cc_on!@*/false) { // check for Internet Explorer
			document.onfocusin = onFocus;
			document.onfocusout = onBlur;
		} else {
			window.onfocus = onFocus;
			window.onblur = onBlur;
		}
			
        // initialize scrollable
		var newHeight = $('.slideTabs').height()+15;
		$('div.slides').height(newHeight);

		$("ul.slideTabs").tabs("#items > .item", {
		
			// enable "cross-fading" effect
			effect: 'fade',
			fadeOutSpeed: 'fast',
			event: 'mouseover',
			tabs: 'li',
		
			// start from the beginning after the last tab
			rotate: true
		
		// use the slideshow plugin. It accepts its own configuration
		}).slideshow({
			autoplay:true, 
			interval:7000
		});

    }

	// Support placeholders fon non-supporting browsers		
	$( 'input[placeholder], textarea[placeholder]' ).placeHoldize();
	
	// select one or more elements to be overlay triggers
	if ($('div.photos').length) {
		toShowInFancyBox = $('a.trigger');
		toShowInFancyBox.fancybox({
			'titlePosition'	:	'over',
			'onComplete'	:	function() {
				$("#fancybox-wrap").hover(function() {
					$("#fancybox-title").show();
				}, function() {
					$("#fancybox-title").hide();
				});
			}
		});
	}	

	// Load twitterfeeds
	if ($('.twitter').length) {
		$(".twitter .tweets").getTwitter({
			userName: "InfraCollege",
			numTweets: 3,
			loaderText: "Tweets laden...",
			slideIn: true,
			slideDuration: 750,
			showHeading: false,
			headingText: "Laatste Tweets",
			showProfileLink: false,
			showTimestamp: false
		});
	}	

	// Add alt class to odd li's
	$('#twitter_update_list li:even').livequery(function(){
		$(this).addClass('alt');
	});
	
	// Print current page when user clicks 'print'
	$('a.printBut').click(function() {
		window.print();
	});


	/* load google map */
	function getDefaultMap(locLatLong, labelContent){
		$("#map").gmap3(
			{	action: 'init',
				options:{
					center:locLatLong,
					zoom:13,
					mapTypeId: google.maps.MapTypeId.ROADMAP,
					mapTypeControl: true,
					mapTypeControlOptions: {
						style: google.maps.MapTypeControlStyle.DROPDOWN_MENU
					},
					navigationControl: true,
					scrollwheel: true,
					streetViewControl: true
				}
			},
			{	action:'addMarker',
				latLng:locLatLong,
				infowindow:{
					options:{
						content:labelContent
					}
				},
				map:{
					center: true
				}
			}
		);
	}
	
	function getNewRoute(locLatLong, labelContent){
		$('#map').gmap3({
			action:'getRoute',
			options:{
				origin:$('#fromLoc').val(),
				destination:labelContent,
				travelMode: google.maps.DirectionsTravelMode.DRIVING
		},
		callback: function(results){
			if (!results) {return;}
			$(this).gmap3(
			{ action:'init', 
				options:{
					center:locLatLong,
					zoom:13,
					mapTypeId: google.maps.MapTypeId.ROADMAP,
					mapTypeControl: true,
					mapTypeControlOptions: {
						style: google.maps.MapTypeControlStyle.DROPDOWN_MENU
					},
					navigationControl: true,
					scrollwheel: true,
					streetViewControl: true
				}
			},
			{ action:'addDirectionsRenderer',
				options:{
					preserveViewport: false,
					draggable: true,
					directions:results
				}
			},
			{
				action:'setDirectionsPanel',
				id:'directionsPanel'
			});
		}
		});
	}
	
	if ($('#map').length) {
		var locatie = $.getUrlVar('locatie');
		switch(locatie){
			case 'Leek':{
				locLatLong = [53.16780, 6.37587];
				labelContent = "Infra College<br/>Industriepark 5-d, 9351 PA Leek";
				break;
			}
			case 'Assen':{
				locLatLong = [53.000416, 6.579893];
				labelContent = "Infra College<br/>Dr. A.F. Philipsweg 80, 9403 AD Assen";
				break;
			}
			case 'Drachten':{
				locLatLong = [53.118691, 6.097055];
				labelContent = "Infra College<br/>Ringweg 5, 9201 GT Drachten";
				break;
			}
			default:{
				locLatLong = [53.203623, 6.556606];
				labelContent = "Infra College<br/>Leonard Springerlaan 1, 9727 KB Groningen";
				break;
			}
		}
		getDefaultMap(locLatLong, labelContent);
	}
	
	$('#getRouteButton').click(function(){
		getNewRoute(locLatLong, labelContent);
	});
	
	$('#fromLoc').keyup(function(e) {
		//alert(e.keyCode);
		if(e.keyCode === 13) {
			//alert('Enter key was pressed.');
			getNewRoute(locLatLong, labelContent);
		}
	});
	
});

