
var wl = "";
var ca = "1";
var cf = "USD";
var ct = "KRW";

function getCurrency() {
	$.ajax({
		url: "/api/currency.php?a="+ ca +"&f="+ cf +"&t="+ ct,
		dataType: "text",
		success: function(data) {
			var json = eval("(" + data + ")");
			if (json.e == "0") {
				var html = "";
				html += getFlagTag(json.f) + json.a +" "+ json.f +" = ";
				html += getFlagTag(json.t) + json.r +" "+ json.t;
				$("#currency").html(html);
			}
		}
	});
}

function getGold() {
	$.ajax({
		url: "/api/currency.php?a=1&f=DON&t=KRW",
		dataType: "text",
		success: function(data) {
			var json = eval("(" + data + ")");
			if (json.e == "0") {
				var html = "";
				html += getFlagTag(json.f) + json.a +" "+ json.f +" = ";
				html += getFlagTag(json.t) + json.r +" "+ json.t;
				$("#gold").html(html);
			}
		}
	});
}

function changeCurrency() {
	ca = $("#ca_val").val();
	cf = $("#cf_val").val();
	ct = $("#ct_val").val();
	if (cf == ct) {
		return;
	}
	$("#currency2").html("<img src='http://static.spic.me/img/ajax-loading.gif' width='14' height='14' align='absmiddle' alt=''>");
	$.ajax({
		url: "/api/currency.php?a="+ ca +"&f="+ cf +"&t="+ ct,
		dataType: "text",
		success: function(data) {
			var json = eval("(" + data + ")");
			if (json.e == "0") {
				var html = "";
				html += getFlagTag(json.f) + json.a +" "+ json.f +" = ";
				html += getFlagTag(json.t) + json.r +" "+ json.t;
				$("#currency2").html(html);
			} else {
				$("#currency2").html("Access error. Please, retry.");
			}
		}
	});
}

function changeGold() {
	ca = $("#ca_val").val();
	cf = $("#cf_val").val();
	ct = "KRW";
	if (cf == ct) {
		return;
	}
	$("#currency2").html("<img src='http://static.spic.me/img/ajax-loading.gif' width='14' height='14' align='absmiddle' alt=''>");
	$.ajax({
		url: "/api/currency.php?a="+ ca +"&f="+ cf +"&t="+ ct,
		dataType: "text",
		success: function(data) {
			var json = eval("(" + data + ")");
			if (json.e == "0") {
				var html = "";
				html += getFlagTag(json.f) + json.a +" "+ json.f +" = ";
				html += getFlagTag(json.t) + json.r +" "+ json.t;
				$("#currency2").html(html);
			} else {
				$("#currency2").html("Access error. Please, retry.");
			}
		}
	});
}

function getFlagTag(v) {
	return "<img src='http://files.nalbam.com/images/country/"+ v +".png' width='16' height='10' align='absmiddle' alt=''> ";
}

function getWeather(v) {
	if (v == "") {
		v = "Seoul, South Korea";
	}
	$("#location").html(v);
	$("#wl_val").html(v);
	var url = "/api/weather.php?v="+ encodeURI(v);
	$.ajax({
		url: url,
		dataType: "text",
		success: function(data) {
			var json = eval("(" + data + ")");
			if (json.today.condition != "") {
				var html = "";
				html += " <img src='/api/weather_icon.php?q="+ json.today.icon +"' width='32' height='32' align='absmiddle' alt=''> ";
				html += json.today.condition +" / "+ json.today.temp_c +"°C ";
				$("#weather").html(html);
			}
		}
	});
}

function getCurrentPosition() {
	$("#wl_val").html("<img src='http://static.spic.me/img/ajax-loading.gif' width='14' height='14' align='absmiddle' alt=''>");
	try {
		navigator.geolocation.getCurrentPosition(getCurrentPositionSuccess, getCurrentPositionError, {timeout: 3000});
	} catch (e) {
		getWeather("");
	}
}

function getCurrentPositionSuccess(position) {
	if (position != null) {
		var latlng = position.coords.latitude +","+ position.coords.longitude;
		$.getJSON("/api/geocode.php?latlng="+latlng, function(data) {
			var addr = "";
			var results = data.results;
			if (results.length > 0) {
			//	var len = results[0].address_components.length;
			//	if (len > 1) {
			//		addr = results[0].address_components[len-2].long_name +", "+ results[0].address_components[len-1].long_name;
			//	} else if (results[0].address_components.length == 1) {
			//		addr = results[0].address_components[0].long_name;
			//	} else {
					addr = results[0].formatted_address;
			//	}
			}
			if (addr != "") {
				document.cookie = "wl="+ escape(addr) + "; path=/;";
			}
			getWeather(addr);
		});
	} else {
		getWeather("");
	}
}

function getCurrentPositionError(e) {
	getWeather("");
}

