﻿var isMajor = parseInt(navigator.appVersion);
var isMinor = parseFloat(navigator.appVersion);
var agt = navigator.userAgent.toLowerCase();
var isIe    = ((agt.indexOf("msie") != -1) && (agt.indexOf("opera") == -1));
var isIe3    = (isIe && (isMajor < 4));
var isIe4    = (isIe && (isMajor == 4) && (agt.indexOf("msie 4")!=-1) );
var isIe4Up = (isIe && (isMajor >= 4));
var isIe5    = (isIe && (isMajor == 4) && (agt.indexOf("msie 5.0")!=-1) );
var isIe5_5  = (isIe && (isMajor == 4) && (agt.indexOf("msie 5.5") !=-1));
var isIe6    = (isIe && (isMajor == 4) && (agt.indexOf("msie 6.0")!=-1) );
var isIe5Up  = (isIe && !isIe3 && !isIe4);
var isIe5_5Up = (isIe && !isIe3 && !isIe4 && !isIe5);

var isFirefox = ((agt.indexOf('firefox') != -1) || (agt.indexOf('Firefox') != -1));
var isFirefox1 = ((agt.indexOf('firefox/1.') != -1) || (agt.indexOf('Firefox/1.') != -1));
var isFirefox2 = ((agt.indexOf('firefox/2.') != -1) || (agt.indexOf('Firefox/2.') != -1));

var isNav   = ((agt.indexOf('mozilla')!=-1) && (agt.indexOf('spoofer')==-1)
        	&& (agt.indexOf('compatible') == -1) && (agt.indexOf('opera')==-1)
        	&& (agt.indexOf('webtv')==-1) && (agt.indexOf('hotjava')==-1));
var isNav6Up = (isNav && (isMajor >= 5));
var popUpWindowWidth = 800;
var popUpWindowHeight = 600;
var tailImage;
var tailImageId;

function checkRightClick(keyIn)
{
	var message = "富達物業版權所有";
    if (navigator.appVersion.indexOf("MSIE") != -1 && event.button == 2)
    {
        alert(message);
        return false;
    }
	else if (keyIn.which == 3)
    {
//        keyIn.preventDefault();
//        keyIn.stopPropagation();
        alert(message);
        return false;
    }
}
//document.onmousedown = checkRightClick;

function gotoSearchLink(baseUrl, categoryId, districtId)
{
    var searchLink = '/index/search'
    var url = baseUrl + searchLink + "?category=" + categoryId + "&district=" + districtId + "&rentOrSell=3&street=0&building=0&size=0&price=0";
    location.href = url;
}

function setKeepMenu(menuId, inputValue)
{
    keepMenu[menuId] = inputValue;
}

function startToHideMenu(menuName, id)
{
    if (keepMenu[id] == 1)
    {
//        showMenu(menuName, 1);
    }
    else
    {
        menuCounter[id]++;
    }

    if (menuCounter[id] == 3)
    {
        showMenu(menuName, 0);
//        document.getElementById('searchFormForHidden').style.visibility = 'visible';
        menuCounter[id] = 0;
    }
    else
    {
        menuTimer[id] = setTimeout(function(){callStartToHideMenu(menuName, id);}, 50);
    }
}

function callStartToHideMenu(menuName, id)
{
    startToHideMenu(menuName, id);
}

function showMenu(menuName, setShow)
{
    if (setShow == 1)
    {
        document.getElementById(menuName).style.display = 'block';
    }
    else
    {
        document.getElementById(menuName).style.display = 'none';
    }
}

function preloadImages()
{
	var d=document;
	document.preloadFinished=false;
	if(d.images)
	{
		if(!d.p)
			d.p=new Array();
		var i,j=d.p.length,a=preloadImages.arguments;
		for(i=0; i<a.length; i++)
			if (a[i].indexOf("#")!=0)
			{
				d.p[j]=new Image; d.p[j++].src=a[i];
			}
	}
	document.preloadFinished=true;
}

function changeImageOver(imgObject, originalFileName, imageId)
{
	if (document.preloadFinished)
		imgObject.src = document.p[parseInt(imageId)].src;

/* this part is for dojo menu, dojo v1.4 */
    clearTimeout(menuTimer[imageId]);
    menuTimer[imageId] = -1;
/* this part is to hide the select box */
    if (isIe6)
    {
        if (imageId != 0)
            document.getElementById('searchFormForHidden').style.visibility = 'hidden';

        anyMouseOverImage[imageId] = 1;
        setInterval(function(){
            var goAhead = 1;
            for (var i=1;i<anyMouseOverImage.length;i++)
            {
                if (anyMouseOverImage[i] == 1)
                {
                    goAhead = 0;
                    break;
                }
            }
            for (var i=1;i<keepMenu.length;i++)
            {
                if (keepMenu[i] == 1)
                {
                    goAhead = 0;
                    break;
                }
            }
            if (goAhead == 1)
                document.getElementById('searchFormForHidden').style.visibility = 'visible';
        }, 1200);
    }
}

function changeImageOriginal(imgObject, originalFileName, overOrOut, imageId)
{
	if(imgObject.src)
	{
		if (overOrOut == 1)
			imgObject.src = "/public/images/" + originalFileName + "Over.gif";
		else
			imgObject.src = "/public/images/" + originalFileName + ".gif";
	}
    anyMouseOverImage[imageId] = 0;
}

function resizeWindow(width, height)
{
    if (parseInt(navigator.appVersion)>3)
    {
        if (navigator.appName=="Netscape")
        {
            top.outerWidth = width;
            top.outerHeight = height;
        }
        else top.resizeTo(width, height);
    }
}

function myOpenWin(linkToGo, sizeWidth, sizeHeight, windowName)
{
    var winname = window.open(linkToGo, windowName, "scrollbars=1,status=1,location=0,directories=0,menubar=0,resizable=1,width=" + sizeWidth + ",height=" + sizeHeight + "\"");
    winname.top.moveTo((screen.width - sizeWidth)/2, (screen.height - sizeHeight)/2);
    winname.focus();
}

function getDivLeft(elementId)
{
    var imageLeft;
    if (isIe5Up)
    {
        if (document.all[elementId].style.left == "")
            imageLeft = 0;
        else
            imageLeft = document.all[elementId].style.left;
    }
    else if (isFirefox)
    {
        if (document.getElementById(elementId).style.left == "")
            imageLeft = 0;
        else
            imageLeft = document.getElementById(elementId).style.left;
    }
    return parseInt(imageLeft);
}

function getDivTop(elementId)
{
    var imageTop;

    if (isIe5Up)
    {
        if (document.all[elementId].style.top == "")
            imageTop = 0;
        else
            imageTop = document.all[elementId].style.top;
    }
    else if (isFirefox)
    {
        if (document.getElementById(elementId).style.top == "")
            imageTop = 0;
        else
            imageTop = document.getElementById(elementId).style.top;
    }
    return parseInt(imageTop);
}

function startScroll(listOfImage, listOfImageWidth, listOfImageHeight, provideUniqueId, direction, paddingTop, paddingLeft, step, imageSeparation, speed)
{
    storeTimer = setTimeout(function(){scrollingInAction(listOfImage, listOfImageWidth, listOfImageHeight, provideUniqueId, direction, paddingTop, paddingLeft, globolStep, imageSeparation, speed);} , 1000/speed);
}

function setScrollStartStop(inputValue)
{
    mouseOverStop = inputValue;
}

function scrollingInAction(listOfImage, listOfImageWidth, listOfImageHeight, provideUniqueId, direction, paddingTop, paddingLeft, step, imageSeparation, speed)
{
    var left = paddingLeft;
    var top = paddingTop;
    var imageLeft;
    var imageTop;

    for (i=0;i<listOfImage.length;i++)
    {
        imageLeft = getDivLeft(provideUniqueId + i);
        imageTop = getDivTop(provideUniqueId + i);

/* store the last image */
        if (step == 0)
        {
            tailImage = provideUniqueId + (listOfImage.length - 1);
            tailImageId = (listOfImage.length - 1);
        }

        if (direction == 'left')
        {
            if (step == 0)
            {
/* step = 0 means in setup phase */
//                left = paddingLeft + parseInt(imageLeft) + i * (((i == 0 ) ? listOfImageWidth[i]:listOfImageWidth[i - 1]) + imageSeparation) - step;
                if (i == 0)
                    left = 0;
                else
                    left += listOfImageWidth[i - 1] + (i * imageSeparation);
            }
            else
                left = parseInt(imageLeft) - step;
            if (left < -(listOfImageWidth[i]))
            {
                left = getDivLeft(tailImage) + (listOfImageWidth[tailImageId] + imageSeparation) - step;
/* change the last image */
                tailImage = provideUniqueId + i;
                tailImageId = i;
            }
            top = paddingTop + 0;
        }
        else if (direction == 'right')
        {
            if (step == 0)
            {
                if (i == 0)
                    left = listOfImageWidth[i];
                else
                    left -= listOfImageWidth[i] - (i * imageSeparation);
            }
            else
                left = parseInt(imageLeft) + step;
            if (left > listOfImageWidth[i])
            {
                left = getDivLeft(tailImage) - (listOfImageWidth[i] + imageSeparation) + step;
                tailImage = provideUniqueId + i;
                tailImageId = i;
            }
            top = paddingTop + 0;
        }
        else if (direction == 'down')
        {
            if (step == 0)
            {
                if (i == 0)
                    top = listOfImageHeight[i];
                else
                    top -= listOfImageHeight[i] - (i * imageSeparation);
            }
            else
                top = parseInt(imageTop) + step;
            if (top > listOfImageHeight[i])
            {
                top = getDivTop(tailImage) - (listOfImageHeight[i] + imageSeparation) + step;
                tailImage = provideUniqueId + i;
                tailImageId = i;
            }
            left = paddingLeft + 0;
        }
        else if (direction == 'top')
        {
            if (step == 0)
            {
                if (i == 0)
                    top = 0;
                else
                    top += listOfImageHeight[i - 1] + (i * imageSeparation);
            }
            else
                top = parseInt(imageTop) - step;
            if (top < -(listOfImageHeight[i]))
            {
                top = getDivTop(tailImage) + (listOfImageHeight[tailImageId] + imageSeparation) - step;
                tailImage = provideUniqueId + i;
                tailImageId = i;
            }
            left = paddingLeft + 0;
        }
        if (mouseOverStop == 0)
        {
            if (isIe5Up)
            {
                document.all[provideUniqueId + i].style.top = top;
                document.all[provideUniqueId + i].style.left = left;
            }
            else if (isFirefox)
            {
                document.getElementById(provideUniqueId + i).style.top = top + 'px';
                document.getElementById(provideUniqueId + i).style.left = left + 'px';
            }
        }
    }
    if (listOfImage.length > 1)
        startScroll(listOfImage, listOfImageWidth, listOfImageHeight, provideUniqueId, direction, paddingTop, paddingLeft, globolStep, imageSeparation, speed);
}

function simpleImagesScrolling(listOfImage, attachedElement, listOfImageWidth, listOfImageHeight, provideUniqueId, direction, imageSeparation, speed)
{
    var printContent = '';

    var tbodyTag = dojo.byId(attachedElement);
    for (i=0;i<listOfImage.length;i++)
    {
        printContent += "<a href='javascript:void(0)'><div class='scrollImageStyle' id='" + provideUniqueId + i + "' onmouseover=\"setScrollStartStop(1)\" onmouseout=\"setScrollStartStop(0)\">" + "<img src='" + listOfImage[i] + "' width=" + listOfImageWidth[i] + " height=" + listOfImageHeight[i] + " border='0'>" + "</div></a>";
    }

    tbodyTag.innerHTML = printContent;
    scrollingInAction(listOfImage, listOfImageWidth, listOfImageHeight, provideUniqueId, direction, 0, 0, 0, imageSeparation, speed);
}

function centerMessageBoard(layerName)
{
    var layerHeight = fGetElementHeight(layerName);
    var layerWidth = fGetElementWidth(layerName);
    var leftOffset = 0;

    if (isIe5Up)
    {
        document.all[layerName].style.top = fClientHeight()/2 - layerHeight/2 + fScrollTop();
        document.all[layerName].style.left = fClientWidth()/2 - layerWidth/2 + fScrollLeft() + leftOffset;
    }
    else if (isFirefox)
    {
        document.getElementById(layerName).style.top = (fClientHeight()/2 - layerHeight/2 + fScrollTop()) + 'px';
        document.getElementById(layerName).style.left = (fClientWidth()/2 - layerWidth/2 + fScrollLeft() + leftOffset) + 'px';
//        DOMViewerObj = document.getElementById(layerName);
//        window.open('/eduresources/index/debug');
    }
}

function displayLoading(isDisplay)
{
    if (isDisplay == true)
        lightBoxLoading.show();
    else
        lightBoxLoading.hide();
    return 1;
    var divName = "messageLayer";

    if (isIe5Up || isFirefox)
    {
        if (isDisplay)
        {
            centerMessageBoard(divName);
            document.getElementById(divName).style.visibility = "visible";
        }
        else
            document.getElementById(divName).style.visibility = "hidden";
    }
}

function fGetElementHeight(divName)
{
    if (document.getElementById(divName).style.height)
        return document.getElementById(divName).style.height;
    else if (document.documentElement)
        return document.getElementById(divName).clientHeight;
}
function fGetElementWidth(divName)
{
    if (document.getElementById(divName).style.width)
        return document.getElementById(divName).style.width;
    else if (document.documentElement)
        return document.getElementById(divName).clientWidth;
}
function fClientWidth() {
	return f_filterResults (
		window.innerWidth ? window.innerWidth : 0,
		document.documentElement ? document.documentElement.clientWidth : 0,
		document.body ? document.body.clientWidth : 0
	);
}
function fClientHeight() {
	return f_filterResults (
		window.innerHeight ? window.innerHeight : 0,
		document.documentElement ? document.documentElement.clientHeight : 0,
		document.body ? document.body.clientHeight : 0
	);
}
function fScrollLeft() {
	return f_filterResults (
		window.pageXOffset ? window.pageXOffset : 0,
		document.documentElement ? document.documentElement.scrollLeft : 0,
		document.body ? document.body.scrollLeft : 0
	);
}

function fScrollTop() {
	return f_filterResults (
		window.pageYOffset ? window.pageYOffset : 0,
		document.documentElement ? document.documentElement.scrollTop : 0,
		document.body ? document.body.scrollTop : 0
	);
}

function f_filterResults(n_win, n_docel, n_body) {
	var n_result = n_win ? n_win : 0;
	if (n_docel && (!n_result || (n_result > n_docel)))
		n_result = n_docel;
	return n_body && (!n_result || (n_result > n_body)) ? n_body : n_result;
}

function getQuerySting(fieldName)
{
    wholeString = window.location.search.substring(1);
    separatedString = wholeString.split("&");
    for (i=0;i<separatedString.length;i++)
    {
        allFieldName = separatedString[i].split("=");
        if (allFieldName[0] == fieldName)
        {
            return allFieldName[1];
        }
    }
    return 0;
}

function printPriceId(inputId)
{
    var previousPriceId = getQuerySting('price');
    if (previousPriceId == inputId)
        return "'" + inputId + "' selected";
    else
        return "'" + inputId + "'";
}

function changePriceList()
{
    var rentOrSell = document.getElementById('rentOrSell');
    var tbodyTag = dojo.byId('priceListSpan');
    if (rentOrSell.options[rentOrSell.selectedIndex].value == 1)
    {
        printContent = "<select name='price' id='price'>";
        printContent += "<option value=" + printPriceId(0) + " label='-- 全部 --'>-- 全部 --</option>";
        printContent += "<option value=" + printPriceId(10) + " label='1,000 - 3,000'>1,000 - 3,000</option>";
        printContent += "<option value=" + printPriceId(11) + " label='3,001 - 5,000'>3,001 - 5,000</option>";
        printContent += "<option value=" + printPriceId(12) + " label='5,001 - 10,000'>5,001 - 10,000</option>";
        printContent += "<option value=" + printPriceId(13) + "' label='&gt; 10,000'>&gt; 10,000</option>";
        printContent += "</select>";
    }
    else if (rentOrSell.options[rentOrSell.selectedIndex].value == 2)
    {
        printContent = "<select name='price' id='price'>";
        printContent += "<option value=" + printPriceId(0) + " label='-- 全部 --'>-- 全部 --</option>";
        printContent += "<option value=" + printPriceId(1) + " label='50 - 100 萬'>50 - 100 萬</option>";
        printContent += "<option value=" + printPriceId(2) + " label='101 - 500 萬'>101 - 500 萬</option>";
        printContent += "<option value=" + printPriceId(3) + " label='501 - 1,000 萬'>501 - 1,000 萬</option>";
        printContent += "<option value=" + printPriceId(4) + " label='&gt; 1,000 萬'>&gt; 1,000 萬</option>";
        printContent += "</select>";
    }
    else if (rentOrSell.options[rentOrSell.selectedIndex].value == 3)
    {
        printContent = "<select name='price' id='price'>";
        printContent += "<option value='0' label='-- 全部 --'>-- 全部 --</option>";
        printContent += "</select>";
    }
    tbodyTag.innerHTML = printContent;
}

function confirmRemove(link)
{
    var yesOrNo = confirm("真的要刪除?");
    if (yesOrNo)
    {
        location.href = link;
    }
}
