/*
Created on : 2017/11/08
Author : Linh Dinh
*/
if (typeof (itsmo) == 'undefined') {
var itsmo = {};
}
if (typeof (itsmo.taxi) == 'undefined') {
itsmo.taxi = {};
}
itsmo.taxi.textCurrentLocation = '現在地';
itsmo.taxi.countTextField = 0;
itsmo.taxi.listMsg = null;
itsmo.taxi.listSuggest = null;
itsmo.taxi.allFee = [];
itsmo.taxi.distanceLimit = 0;
itsmo.taxi.flagNotSuggest = false;
itsmo.taxi.viaPoints = [
];
itsmo.taxi.focusingRouteInputTxt = null;
itsmo.taxi.add_point = '
';
$(window).ready(function ($) {
itsmo.taxi.initCurrentLocation();
$(".point-add").click(function () {
itsmo.taxi.blockAddPoint();
});
$(".block-search .block-wrap-search").on('click', '.add-more .close-add', function (){
itsmo.taxi.onClickDeleteViaPoint($(this));
});
$(".block-search .block-wrap-search").on('blur', '.add-more input', function (){
itsmo.taxi.onFocusOutViaPoint($(this));
itsmo.taxi.addPlaceholder($(this));
});
$(".block-search .block-wrap-search .stop input").on('blur', function (){
itsmo.taxi.onFocusOutViaPoint($(this));
itsmo.taxi.addPlaceholder($(this));
});
$(".btn-custom").click(function () {
$(this).removeClass('active');
});
$(".input-group input").on('focus keyup', function (e) {
itsmo.taxi.removePlaceholder($(this));
itsmo.taxi.removeTextCurLocation($(this));
itsmo.taxi.appendListSuggest($(this), 220, e);
});
$(".input-group input").on('blur', function (e) {
itsmo.taxi.addPlaceholder($(this));
});
$(".input-group").on('click', '.suggestion-block ul li', function () {
itsmo.taxi.replaceValue($(this));
itsmo.taxi.deleteSuggestDiv($(this));
});
$(".input-group .suggestion-block").on('keydown', 'ul', function (e) {
itsmo.taxi.onKeyDown($(this), e);
});
$(".input-group .suggestion-block").on('focusout', 'ul', function (e) {
itsmo.taxi.deleteSuggestDivFromUl($(this));
});
$(".stop input").on('focus keyup', function (e){
itsmo.taxi.removePlaceholder($(this));
itsmo.taxi.removeTextCurLocation($(this));
itsmo.taxi.appendListSuggest($(this), 180, e);
});
$(".block-search .block-wrap-search").on('focus keyup', '.add-more input', function (e){
itsmo.taxi.removePlaceholder($(this));
itsmo.taxi.removeTextCurLocation($(this));
itsmo.taxi.appendListSuggest($(this), 170, e);
});
$(".block-search .block-wrap-search").on('click', '.add-more .suggestion-block ul li', function (){
itsmo.taxi.replaceValue($(this));
itsmo.taxi.deleteSuggestDiv($(this));
});
$(".block-search .block-wrap-search").on('keydown', '.add-more .suggestion-block ul', function (e){
itsmo.taxi.onKeyDown($(this), e);
});
$(".block-search .block-wrap-search").on('focusout', '.add-more .suggestion-block ul', function (e){
itsmo.taxi.deleteSuggestDivFromUl($(this));
});
if (typeof itsmo.taxi.onload === "function") {
itsmo.taxi.onload();
}
$(".group-switch label input").on('click', function () {
itsmo.taxi.onClickSwitch($(this));
});
itsmo.taxi.heightOfLine();
});
itsmo.taxi.removeTextCurLocation = function (el) {
if(el.val() == itsmo.taxi.textCurrentLocation) {
el.val('');
}
}
itsmo.taxi.removePlaceholder = function (el) {
var ph = el.attr('placeholder');
el.attr('bak-ph', ph);
el.removeAttr('placeholder');
}
itsmo.taxi.addPlaceholder = function (el) {
var ph = el.attr('bak-ph');
el.attr('placeholder', ph);
}
itsmo.taxi.initCurrentLocation = function () {
var getIn = $('#get-in').val();
if (getIn == itsmo.taxi.textCurrentLocation) {
itsmo.taxi.currentLocation();
}
}
itsmo.taxi.onKeyDown = function (el, e) {
e.preventDefault();
if (e.keyCode == 40) {
var lastActive = el.find('li.focus-active');
if (lastActive.is(':last-child')){
return false;
}
lastActive.removeClass('focus-active');
lastActive.next().addClass('focus-active');
return false;
} else if (e.keyCode == 38) {
var lastActive = el.find('li.focus-active');
if (lastActive.is(':first-child')){
return false;
}
lastActive.removeClass('focus-active');
lastActive.prev().addClass('focus-active');
return false;
} else if (e.keyCode == 13) {
el.find('li.focus-active').click();
}
}
itsmo.taxi.deleteSuggestDiv = function (textField) {
textField.parent().parent().addClass('display-none');
};
itsmo.taxi.deleteSuggestDivFromUl = function (textField) {
textField.parent().addClass('display-none');
};
itsmo.taxi.replaceValue = function (el) {
var value = el.html();
var latlon = el.html() + '_' + el.data('latlon');
el.parent().parent().prev('input').val(value);
if (value != itsmo.taxi.textCurrentLocation) {
itsmo.taxi.viaPoints.push({
name: value,
name_latlon: latlon
});
} else {
itsmo.taxi.currentLocation();
}
};
itsmo.taxi.appendListSuggest = function (textField, width, e) {
var freeWord = textField.val();
itsmo.taxi.focusingRouteInputTxt = textField;
itsmo.taxi.focusingRouteInputTxt.next('div').css("width", width + "px");
if (e.keyCode == 40) {
itsmo.taxi.focusingRouteInputTxt.next('div').find('li:first-child').addClass('focus-active');
itsmo.taxi.focusingRouteInputTxt.next('div').find('ul').focus();
} else {
textField.next('div').children().remove();
if (freeWord == itsmo.taxi.textCurrentLocation) {
itsmo.taxi.currentLocation();
return false;
}
if (freeWord.length >= 2) {
itsmo.suggest.requestSgtDataForTaxi(freeWord, itsmo.taxi.renderSuggest);
} else {
if(itsmo.suggest.saveRequestSgt != null){
itsmo.suggest.saveRequestSgt.abort();
}
}
if (freeWord.length == 0) {
textField.next('div').removeClass('display-none');
var stringListSugHtml = '';
itsmo.taxi.focusingRouteInputTxt.next('div').css("width", width + "px");
textField.next('div').append(stringListSugHtml);
}
}
};
itsmo.taxi.renderSuggest = function (suggestData) {
var listSuggest = suggestData.result;
if(itsmo.taxi.focusingRouteInputTxt != null && (listSuggest.station.length != 0 || listSuggest.poi.length != 0 || listSuggest.address.length != 0)){
itsmo.taxi.focusingRouteInputTxt.next('div').removeClass('display-none');
var stringListSugHtml = '';
for(i = 0; i < listSuggest.station.length; i++) {
stringListSugHtml += '- ' + listSuggest.station[i]['nm'] + '
';
}
for(i = 0; i < listSuggest.poi.length; i++) {
stringListSugHtml += '- ' + listSuggest.poi[i]['nm'] + '
';
}
for(i = 0; i < listSuggest.address.length; i++) {
stringListSugHtml += '- ' + listSuggest.address[i]['nm'] + '
';
}
stringListSugHtml += '
';
itsmo.taxi.focusingRouteInputTxt.next('div').append(stringListSugHtml);
}
};
itsmo.taxi.onFocusOutViaPoint = function (el) {
var viaValue = el.val();
if (el.next('div').find('li:first-child').hasClass('focus-active')) {
return false;
} else {
setTimeout(function(){
el.next('div').addClass('display-none');
},200);
}
};
itsmo.taxi.blockAddPoint = function () {
if (itsmo.taxi.countTextField == 0) {
$(".input-group-add.stop").after(itsmo.taxi.add_point);
} else {
$(".input-group.input-group-add.add-more").after(itsmo.taxi.add_point);
}
itsmo.taxi.countTextField++;
if (itsmo.taxi.countTextField == 2) {
$(".point-add").css('display','none');
}
};
itsmo.taxi.onClickDeleteViaPoint = function (el) {
el.parent('.input-group-add').remove();
itsmo.taxi.countTextField--;
if (itsmo.taxi.countTextField < 2) {
$(".point-add").css('display','inline-block');
}
}
itsmo.taxi.clickExchange = function () {
var getIn = $('#get-in').val();
var getOff = $('#get-off').val();
$('#get-in').val(getOff);
$('#get-off').val(getIn);
};
itsmo.taxi.changeContentGetIn = function (e) {
var getIn = $('#get-in').val();
var getOff = $('#get-off').val();
if ($('#get-in').next('div').find('li:first-child').hasClass('focus-active')) {
return false;
} else {
setTimeout(function(){
$('#get-in').next('div').addClass('display-none');
},200);
}
};
itsmo.taxi.changeContentGetOff = function (e) {
var getIn = $('#get-in').val();
var getOff = $('#get-off').val();
if ($('#get-off').next('div').find('li:first-child').hasClass('focus-active')) {
return false;
} else {
setTimeout(function(){
$('#get-off').next('div').addClass('display-none');
},200);
}
};
itsmo.taxi.clickSubmit = function () {
var getIn = $('#get-in').val();
var getOff = $('#get-off').val();
var via = $('input[name="via"]').map(function(){
return this.value;
}).get();
via = via.filter(function(n){return n; });
var midnight = $('#midnight').is(":checked");
var toll = $('#toll').is(":checked");
var isSame = false;
//check get in or get off empty
if (getIn == '' || getOff == '') {
alert(itsmo.taxi.listMsg['W004']);
return false;
}
//check get in is same get off and via point empty
if (getIn === getOff && via == '') {
alert(itsmo.taxi.listMsg['W001']);
return false;
}
//check all is same
if (getIn === getOff && via != '') {
for(i = 0; i < via.length; i++) {
if (getIn != via[i]) {
isSame = false;
break;
}
isSame = true;
}
}
if (isSame == true) {
alert(itsmo.taxi.listMsg['W001']);
return false;
}
//check pattern is same
if (via.length == 1) {
if(getIn == via[0] || via[0] == getOff){
alert(itsmo.taxi.listMsg['W001']);
return false;
}
}
if (via.length == 2) {
if(getIn == via[0] || via[0] == via[1] || via[1] == getOff){
alert(itsmo.taxi.listMsg['W001']);
return false;
}
}
if (via.length == 3) {
if(getIn == via[0] || via[0] == via[1] || via[1] == via[2] || via[2] == getOff){
alert(itsmo.taxi.listMsg['W001']);
return false;
}
}
//check get current location
if (getIn == itsmo.taxi.textCurrentLocation) {
if ($('#current-location').val() != '') {
getIn = $('#current-location').val();
} else {
alert(itsmo.taxi.listMsg['W002']);
return false;
}
}
if (getOff == itsmo.taxi.textCurrentLocation) {
if ($('#current-location').val() != '') {
getOff = $('#current-location').val();
} else {
alert(itsmo.taxi.listMsg['W002']);
return false;
}
}
for(i = 0; i < via.length; i++) {
if (via[i] == itsmo.taxi.textCurrentLocation) {
if ($('#current-location').val() != '') {
via[i] = $('#current-location').val();
} else {
alert(itsmo.taxi.listMsg['W002']);
return false;
}
}
}
//get lat lon for suggest data
for(i = 0; i < via.length; i++) {
for(j = 0; j < itsmo.taxi.viaPoints.length; j++) {
if (via[i] == itsmo.taxi.viaPoints[j]['name']) {
via[i] = itsmo.taxi.viaPoints[j]['name_latlon'];
}
}
}
for(j = 0; j < itsmo.taxi.viaPoints.length; j++) {
if (getIn == itsmo.taxi.viaPoints[j]['name']) {
getIn = itsmo.taxi.viaPoints[j]['name_latlon'];
}
if (getOff == itsmo.taxi.viaPoints[j]['name']) {
getOff = itsmo.taxi.viaPoints[j]['name_latlon'];
}
}
//check distance
if ((getIn.match(/_/g) || []).length == 0 ) {
itsmo.taxi.flagNotSuggest = true;
}
if ((getOff.match(/_/g) || []).length == 0 ) {
itsmo.taxi.flagNotSuggest = true;
}
for(i = 0; i < via.length; i++) {
if ((via[i].match(/_/g) || []).length == 0 ) {
itsmo.taxi.flagNotSuggest = true;
break;
}
}
if (itsmo.taxi.flagNotSuggest == false && itsmo.taxi.calculateDistance(getIn, getOff, via) > (itsmo.taxi.distanceLimit * 1000)) {
alert(itsmo.taxi.listMsg['W005']);
return false;
}
//create URL
var url = "/taxi/?getin=" + getIn + "&getoff=" + getOff;
if (via != '') {
url += "&via=" + via;
}
if (midnight == true) {
url += "&midnight=1";
}
if (toll == true) {
url += "&toll=1";
}
window.location = url;
}
itsmo.taxi.currentLocation = function () {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(itsmo.taxi.getAddressFromLatLon);
}
};
itsmo.taxi.getAddressFromLatLon = function (position) {
var latlon = new ZDC.LatLon(position.coords.latitude, position.coords.longitude);
latlon = ZDC.wgsTotky(latlon);
var lat = latlon.lat;
var lon = latlon.lon;
var f = latlon.f;
var h = latlon.h;
$.ajax({
type:"GET",
url: '/map/right_click.php',
dataType:"json",
cache: false,
crossDomain: true,
headers: {
'X-Suggest-Auth':'NINAVW'
},
data:{lat:lat, lon:lon , f:f, h:h},
success: function(data) {
if (!(data.err == 1 || data[0] == null)) {
var currentLocation = data[0].address.text + '_' + Math.round(data[0].address.point.lat * 3600 * 1000) + '_' + Math.round(data[0].address.point.lon * 3600 * 1000);
$('#current-location').val(currentLocation);
}
}
});
};
itsmo.taxi.searchPage = function (page) {
var url = URL_FOR_PAGING;
if (url.indexOf('?') != -1) {
if (page > 1) {
url += '&page=' + page;
}
} else {
if (page > 1) {
url += '?page=' + page;
}
}
window.location.href = url;
};
itsmo.taxi.heightOfLine = function () {
var heightOfRout = $(".route-wrap").height();
var heightOfStart = $(".block-point.start").height();
var heightOfEnd = $(".block-point.end").height();
$(".line").css("height", (heightOfRout - heightOfStart/2 - heightOfEnd/2 - 68) + "px");
};
itsmo.taxi.onClickSwitch = function (el) {
if (el.is(":checked")) {
$('#number-fee').html(itsmo.taxi.allFee.feeNight.full);
$('#fee-unit-1').html('約' + itsmo.taxi.allFee.feeNight.twoPerson + '円/人');
$('#fee-unit-2').html('約' + itsmo.taxi.allFee.feeNight.threePerson + '円/人');
$('#fee-unit-3').html('約' + itsmo.taxi.allFee.feeNight.fourPerson + '円/人');
} else {
$('#number-fee').html(itsmo.taxi.allFee.feeNormal.full);
$('#fee-unit-1').html('約' + itsmo.taxi.allFee.feeNormal.twoPerson + '円/人');
$('#fee-unit-2').html('約' + itsmo.taxi.allFee.feeNormal.threePerson + '円/人');
$('#fee-unit-3').html('約' + itsmo.taxi.allFee.feeNormal.fourPerson + '円/人');
}
};
itsmo.taxi.calculateDistance = function (getIn, getOff, viaPoint) {
var getInSplited = getIn.split("_");
var getOffSplited = getOff.split("_");
var viaPointSplited = [];
var totalDistance = 0;
if (viaPoint.length == 0) {
totalDistance = totalDistance + ZDC.getLatLonToLatLonDistance(new ZDC.LatLon(ZDC.msTodeg(getInSplited[1]), ZDC.msTodeg(getInSplited[2])), new ZDC.LatLon(ZDC.msTodeg(getOffSplited[1]), ZDC.msTodeg(getOffSplited[2])));
} else {
for(i = 0; i < viaPoint.length; i++) {
viaPointSplited[i] = viaPoint[i].split("_");
}
totalDistance = totalDistance + ZDC.getLatLonToLatLonDistance(new ZDC.LatLon(ZDC.msTodeg(getInSplited[1]), ZDC.msTodeg(getInSplited[2])), new ZDC.LatLon(ZDC.msTodeg(viaPointSplited[0][1]), ZDC.msTodeg(viaPointSplited[0][2])));
if (viaPoint.length > 1) {
for(i = 0; i < viaPoint.length - 1; i++) {
totalDistance = totalDistance + ZDC.getLatLonToLatLonDistance(new ZDC.LatLon(ZDC.msTodeg(viaPointSplited[i][1]), ZDC.msTodeg(viaPointSplited[i][2])), new ZDC.LatLon(ZDC.msTodeg(viaPointSplited[i + 1][1]), ZDC.msTodeg(viaPointSplited[i + 1][2])));
}
}
totalDistance = totalDistance + ZDC.getLatLonToLatLonDistance(new ZDC.LatLon(ZDC.msTodeg(viaPointSplited[viaPoint.length - 1][1]), ZDC.msTodeg(viaPointSplited[viaPoint.length - 1][2])), new ZDC.LatLon(ZDC.msTodeg(getOffSplited[1]), ZDC.msTodeg(getOffSplited[2])));
}
return totalDistance;
}
itsmo.taxi.setMapData = function(mapInfo){
var mapData = JSON.parse(mapInfo);
$.ajax({
url: '/map_with_shape.php',
data: {
action: 'taxiroute',
size: '597,328',
latlons: mapData.latlons,
mpoints: mapData.via,
getIn: mapData.getIn,
getOff: mapData.getOff
},
type: 'post',
dataType: 'text',
success: function(result) {
$("#map-image").attr('src', 'data:image/png;base64, ' + result);
}
});
};