var iconStar = "img/star.svg";
var iconNostar = "img/notstar.svg";
var iconSearch = "img/search.svg";


loadBusstopsList();

$(document).ready(function() {

	// Initialize

	$(".bus-list").hide();
	$(".station").removeClass("expanded");

	// User Events

	$(".station").click(function() {
		if ($(this).hasClass("expanded")) {
			$(this).children(".bus-list").slideUp(200);
			$(this).removeClass("expanded");
		} else {
			$(this).children(".bus-list").slideDown(200);
			$(this).addClass("expanded");
		}
	});

	$(".station-star").click(function() {
		if ($(this).hasClass("js-starred")) {
			// Remove Favorite
			$(this).attr("src", iconNostar);
			$(this).removeClass("js-starred");
			$(this).parents(".station").remove();
		} else {
			// Add new Favorite
			$(this).attr("src", iconStar);
			$(this).addClass("js-starred");
			$(".favorites").append($(this).parents(".station"));
		}
	});

});

$(".js-search").bind("input", function() {
	console.log("New input", $(".js-search").val());
});

//test match function
function testMatch(input) {
	return matchInput(getBusstopList(), input);
}
//match input with busstops name and citys
function matchInput(list, input) {
	var pattern = new RegExp(input, "i");
	//input = input.split(" ");
	var suggests = new Array();
	for (var i = 0; i < list.length && suggests.length < 5; i++) {
		var found = list[i].ORT_GEMEINDE.match(pattern);
		if (found == null)
			found = list[i].ORT_NAME.match(pattern);
		if (found != null)
			suggests[suggests.length] = list[i];
	}
	return suggests;
}

// cache busstops
function loadBusstopsList() {
	console.log("Start Request");
	var apiUrl = "http://opensasa.info/SASAplandata/?type=BASIS_VER_GUELTIGKEIT";
	request(apiUrl, validitySuccess, "jsonp");
}

function validitySuccess(data) {
	if (!localStorage.version || localStorage.version != data[0].VER_GUELTIGKEIT) {
		localStorage.clear();
		localStorage.version = data[0].VER_GUELTIGKEIT;
		if (!localStorage.busstops) {
			console.log("New Data");
			var apiUrl = "http://opensasa.info/SASAplandata/?type=REC_ORT";
			request(apiUrl, busstopsSuccess, "jsonp");
		}
	}

}

function busstopsSuccess(data) {
	localStorage.setItem('busstops', JSON.stringify(data));
}

// Return the busstop list as json which is stored in the localStorage
function getBusstopList() {
	return JSON.parse(localStorage.busstops);
}

// callback is the name of the callback arg
function request(urlAPI, success, callback) {
	$.ajax({
		url: urlAPI,
		dataType: 'jsonp',
		jsonp: callback,
		success: function( data ) {
			console.log("success: " + urlAPI);
			success(data);
		},
		error: function( data ) {
			console.log("Error: " + urlAPI);
		}
	});
}



@@ -944,158 +944,130 @@
  %7B%0A%09
-console.log(%22New input%22, $(%22.js-search%22).val());%0A%7D);%0A%0A//test match function%0Afunction testMatch(input) %7B%0A%09return matchInput(getBusstopList(), input
+var suggests = matchInput(getBusstopList(), $(%22.js-search%22).val());%0A%09console.log(suggests);%0A%09printSuggests(suggests
 );%0A%7D
+);%0A
 %0A//m
@@ -1296,9 +1296,9 @@
 h %3C 
-5
+3
 ; i+
@@ -1501,16 +1501,381 @@
 gests;%0A%7D
+%0A//output suggests%0Afunction printSuggests(suggests) %7B%0A%09if (suggests.length %3E 0)%0A%09%09printNext($(%22.search-results%22).find(%22.station:first%22), suggests, 0);%0A%7D%0A%0Afunction printNext(el, suggests, i) %7B%0A%09el.find(%22.station-title%22).text(suggests%5Bi%5D.ORT_NAME);%0A%09if (el.next(%22.station%22).length != 0 && suggests%5Bi+1%5D != undefined)%0A%09%09printNext(el.next(%22.station%22), suggests, i+1);%0A%7D
 %0A%0A// cac

print suggests