﻿window.Error = function() {
    return true;
}

//解析成经纬度提高查询精确度
function GetLatlng(query) {
    var GCGeocoder = new GClientGeocoder();
    GCGeocoder.getLatLng(query, function(p) {
        return p;
    });
}

//将菜品加入到购物车里
function AddToCart(disid) {
    var count = $("#selCount" + disid).val();
    var remark = "";
    if (document.getElementById("txtReq" + disid) != null)
        remark = $("#txtReq" + disid).val().replace(/Request:/g, "");

    $.get("cart.aspx?DishID=" + disid + "&Count=" + count + "&Remark=" + remark + "&d=" + new Date().valueOf(), null, function(d) {
        window.location.reload();
    });
}

//将购物车里的某个菜品删除
function DelByDishID(disid) {
    $.get("cart.aspx?del=" + disid + "&d=" + new Date().valueOf(), null, function(d) {
        window.location.reload();
    });
}

//餐厅页面搜索事件
function GoSearch() {
    var Cuisine = document.getElementById("ddCuisine").value;
    var Kind = document.getElementById("ddKind").value;
    var Area = document.getElementById("ddArea").value;
    if (Cuisine == "" && Kind == "" && Area == "") {
        alert("Please select the conditions you want to search!"); return;
    }
    if (Cuisine != "") {
        Cuisine = "&Cuisine=" + escape(Cuisine);
    }
    if (Kind != "") {
        Kind = "&Kind=" + escape(Kind);
    }
    if (Area != "") {
        Area = "&Area=" + escape(Area);
    }
    var page = window.location.href.split('?')[0].split('/')[parseInt(window.location.href.split('?')[0].split('/').length) - 1];
    window.location.href = page + "?1=1" + Cuisine + Kind + Area;
}

//计算运费函数
function CountFreight(from, to, p, directions) {
    GEvent.addListener(directions, "load", function() {
        ShowFreight(p, directions);
    });
    directions.load("from: " + from + " to: " + to);
}

//运费查询完成函数,显示到页面
function ShowFreight(p, directions) {
    var kilometers = parseFloat(directions.getDistance().meters) / 1000;
    if (kilometers > parseInt(kilometers))
        kilometers = parseInt(kilometers) + 1;
    else
        kilometers = parseInt(kilometers);
    var res = 0;
    if (kilometers < 3)
        res = 15;
    else {
        if (GetCookie("City") != null) {
            if (GetCookie("City") == "Shanghai")
                res = (kilometers - 3) * 5 + 15;
            else if (GetCookie("City") == "Guangzhou") {
                res = (kilometers - 3) * 7 + 15;
                if (res > 50)
                    res = 50;
            }
            else if (GetCookie("City") == "Beijing") {
                res = (kilometers - 3) * 7 + 15;
            }
        }
        else
            res = (kilometers - 3) * 7 + 15;
    }
    p.html("From you: " + kilometers + "&nbsp;Km<br />Take time: " + parseInt(parseFloat(kilometers) * 5 + 30) + "&nbsp;Minutes<br />Delivery costs: " + parseInt(res) + "&nbsp;Yuan");
    if (document.getElementById("cart1_txtFrightHidden") != null) {
        $("#cart1_txtFrightHidden").val(parseInt(res));
        $("#cart1_txtFright").html(parseInt(res));
        var tatol = parseInt(parseFloat($("#cart1_txtAmount").html()) + parseFloat($("#cart1_txtFrightHidden").val()));
        $("#cart1_txtTatol").html(tatol);
        $("#cart1_txtTatolHidden").val(tatol);
    }
}

function CheckAddress(address) {
    var q = new String(address).toLowerCase();
    q = q.replace(/beijing|shanghai|guangzhou|北京|上海|广州|北京市|上海市|广州市/g, "") + "," + GetCookie("City");
    q = q.replace(/,,|,,,|,,,,|,/g, ",");
    $("#txtAddress").val(q);
    return q;
}

//校准用户输入的地址
function ShowMap() {
    $("#map").show().focus();
    var query = CheckAddress($("#txtAddress").val());
    var map = new GMap2(document.getElementById("mapContent"));
    var geocoder = new GClientGeocoder();
    geocoder.getLatLng(query, function(p) {
        map.setCenter(p, 13);
        map.clearOverlays();
        var marker = new GMarker(p, { title: "My current location", icon: new GIcon(G_DEFAULT_ICON), draggable: true });
        GEvent.addListener(marker, "click", function() {
            marker.openInfoWindowHtml(query);
        });
        GEvent.addListener(marker, "dragend", function(p) {
            SetCookie("LatLng", p.lat() + "," + p.lng());
            //位置确定后重新加载运费
            CheckFreight();
        });
        map.addOverlay(marker);
        SetCookie("LatLng", p.lat() + "," + p.lng());
        if (GetCookie("LatLng") == "")
            alert("Your address is not correct!");
        CheckFreight();
    });
    map.addControl(new GSmallMapControl());
    map.addControl(new GMapTypeControl());
}

//查找页面要计算运费的餐厅
function CheckFreight() {
    //初始化查询对象
    var directions = null;
    //调用查询运费函数
    if (GetCookie("LatLng") != null) {
        var to = unescape(GetCookie("LatLng"));
        $(".FreightP").each(function(i) {
            directions = new GDirections();
            var from = $(this).parent().find("input:hidden").val();
            CountFreight(from, to, $(this), directions);
        });
    }
    else {
        $(".FreightP").html("<span style='color:red;'>Enter address or login in!<br/>Can be displayed with your distance from the restaurant!</span>");
    }
}

//地图拖动代码
var move = false;
function StartDrag(obj) {
    if (event.button == 1 && event.srcElement.tagName.toUpperCase() == "DIV") {
        obj.setCapture();
        obj.style.background = "#999";
        move = true;
    }
}

function Drag(obj) {
    if (move) {
        var oldwin = obj.parentNode;
        oldwin.style.left = event.clientX - 200;
        oldwin.style.top = event.clientY - 17;
    }
}

function StopDrag(obj) {
    obj.style.background = "#fff";
    obj.releaseCapture();
    move = false;
}
