﻿if (window.addEventListener) { FixPrototypeForGecko(); }

function FixPrototypeForGecko() {
    HTMLElement.prototype.__defineGetter__("runtimeStyle", element_prototype_get_runtimeStyle);
    window.constructor.prototype.__defineGetter__("event", window_prototype_get_event);
    Event.prototype.__defineGetter__("srcElement", event_prototype_get_srcElement);
    Event.prototype.__defineGetter__("fromElement", element_prototype_get_fromElement);
    Event.prototype.__defineGetter__("toElement", element_prototype_get_toElement);
}

function element_prototype_get_runtimeStyle() { return this.style; }
function window_prototype_get_event() { return SearchEvent(); }
function event_prototype_get_srcElement() { return this.target; }

function element_prototype_get_fromElement() {
    var node;
    if (this.type == "mouseover") node = this.relatedTarget;
    else if (this.type == "mouseout") node = this.target;
    if (!node) return;
    while (node.nodeType != 1)
        node = node.parentNode;
    return node;
}

function element_prototype_get_toElement() {
    var node;
    if (this.type == "mouseout") node = this.relatedTarget;
    else if (this.type == "mouseover") node = this.target;
    if (!node) return;
    while (node.nodeType != 1)
        node = node.parentNode;
    return node;
}

function SearchEvent() {
    if (document.all) return window.event;
    func = SearchEvent.caller;

    while (func != null) {
        var arg0 = func.arguments[0];
        if (arg0 instanceof Event) {
            return arg0;
        }
        func = func.caller;
    }
    return null;
}

/**
* 將一個 "203px" 的字串，抽取其中數字的部分，回傳 203
*/
function ev(str) {
    var ii = str.indexOf("px");
    if (ii <= 0) return parseInt(str);

    var s = str.substring(0, ii);
    return parseInt(s);
}

function getMouseY() {
    if (document.all) {
        return event.clientY + document.documentElement.scrollTop;
    } else {
        return window.event.pageY;
    }
}

var smallWindow = null;
var SMALL_WIN_OFFSET_X = 10;
var SMALL_WIN_OFFSET_Y = 10;
function showSmallWindow(areaObj) {
    var smallHousePic = document.getElementById("smallHousePic");
    var smallHouseLink = document.getElementById("smallHouseLink");
    smallHouseLink.href = "";
    var house = houseArray[areaObj.id];
    smallWindow = document.getElementById("smallWindow");
    var mainBody = document.getElementById("mainBody");
    parseCoords(areaObj.coords, house);

    smallWindow.style.left = (window.event.clientX + document.body.scrollLeft + SMALL_WIN_OFFSET_X) + "px";
    var finalRightValue = ev(smallWindow.style.left) + ev(smallWindow.style.width);
    if (finalRightValue > document.body.scrollWidth) {
        smallWindow.style.left = (document.body.scrollWidth - ev(smallWindow.style.width) - 10) + "px";
    }
    updateSmallWindowData(house, areaObj);
    smallWindow.style.top = (getMouseY() + SMALL_WIN_OFFSET_Y) + "px";
    smallWindow.style.display = "block";
}

function updateSmallWindowData(house, areaObj) {
    var smallHouseName = document.getElementById("smallHouseName");
    var smallHousePic = document.getElementById("smallHousePic");
    var smallHouseLink = document.getElementById("smallHouseLink");

    smallHouseName.innerHTML = house.houseName;
    smallHouseLink.href = "HouseDetailView.aspx?hid=" + areaObj.id + "&from=pw";
    smallHousePic.src = "Images/HousePhotos/" + house.picId + "s.jpg";
}

function hideSmallWindow() {
    var srcTag = event.fromElement.id;
    var targetTag = event.toElement.id;

    if (isEmpty(targetTag) || targetTag.indexOf("small") != 0) {
        var smallHousePic = document.getElementById("smallHousePic");
        smallHousePic.src = "Images/Shared/loading.gif";
        smallWindow = document.getElementById("smallWindow");
        smallWindow.style.display = "none";
    }
}

function isEmpty(str) {
    return (str == null || str.length == 0);
}
function parseCoords(coords, house) {
    var values = coords.split(",");
    house.x1 = parseInt(values[0]);
    house.y1 = parseInt(values[1]);
    house.x2 = parseInt(values[2]);
    house.y2 = parseInt(values[3]);
    return house;
}

function SmallWindowObject() {
    var picId;
    var houseName;
    var starNo;
    var clickCount;
    //			var ranking;
    var relayCount;
    var travelNotesCount;
    var x1, y1, x2, y2;
} 

