﻿// scripts specific to property details page

function play() {
    if (ytplayer) {
        ytplayer.playVideo();
    }
}


function ShowYoutubeVideo(anchObj) {
    return hs.htmlExpandNoPageCover(anchObj, iframeOptionsYT);
}


function MakeMoreLinks(ParentDiv) {
    var table = ParentDiv;
    //var rows = table.getElementsByTagName('tr');
    var Table_Div = table.getElementsByTagName('div');

    var MoreDivIndex = 0;
    for (var j = 0; j < Table_Div.length; j++) {

        div = Table_Div[j];


        if (div.innerHTML.indexOf('more') > 0) {
            MoreDivIndex = j;

        }
        else {
            if (div.innerHTML.length > 25) {
                var temp = div.innerHTML.substr(0, 25);
                if (temp.lastIndexOf(' ') > 0) {
                    Table_Div[MoreDivIndex].innerHTML = div.innerHTML.substr(0, temp.lastIndexOf(' ')) + ' ' + '<a ID="concat(Details,$DTLType)"  href="javascript://" onclick="return ToggleDiv(this.parentNode,\'' + div.innerHTML + '\')">more</a>';
                }
                else {
                    Table_Div[MoreDivIndex].innerHTML = div.innerHTML.substr(0, temp.lastIndexOf(',')) + ' ' + '<a ID="concat(Details,$DTLType)"  href="javascript://" onclick="return ToggleDiv(this.parentNode,\'' + div.innerHTML + '\')">more</a>';
                }
            }
        }
    }

}

function MakeMoreRemarksLink() {
    var OrgRemarks = document.getElementById('OrgFullRemarks');
    var Remarks_Div = document.getElementById('RemarksDiv');
    if (OrgRemarks.innerHTML.length > 620) {
        var temp = OrgRemarks.innerHTML.substr(0, 620);
        if (temp.lastIndexOf(' ') > 0) {
            Remarks_Div.innerHTML = OrgRemarks.innerHTML.substr(0, temp.lastIndexOf(' ')) + ' ' + '<a ID="RemarkAnchor" href="#" class="divPadding" onclick="return hs.htmlExpandNoPageCover(this, sizeForMoreDetails)">more</p></a><div id="RemarkWindow" class="highslide-maincontent" style="padding:0px 10px 10px 10px;">' + OrgRemarks.innerHTML + '</div>';
        }
        else {
            Remarks_Div.innerHTML = OrgRemarks.innerHTML.substr(0, temp.lastIndexOf(',')) + ' ' + '<a ID="RemarkAnchor" href="#" class="divPadding" onclick="return hs.htmlExpandNoPageCover(this, sizeForMoreDetails)">more</p></a><div id="RemarkWindow" class="highslide-maincontent" style="padding:0px 10px 10px 10px;">' + OrgRemarks.innerHTML + '</div>';
        }
    }


}
function appendJS(filename) {
    if (!window.attachEvent) {
        var script = document.createElement("script");
        script.src = "http://dev.virtualearth.net/mapcontrol/v6.2/js/atlascompat.js";
        script.type = "text/javascript";
        document.getElementsByTagName("head")[0].appendChild(script);
    }
    var fileref = document.createElement('script');
    fileref.setAttribute("type", "text/javascript");
    fileref.setAttribute("src", filename);
    document.getElementsByTagName("head")[0].appendChild(fileref);
}
function onscriptload() {
    if (document.getElementById('myMap')) {
        //get rid of our load animation and load the map
        var mapContent = document.getElementById(prefix+'mapContent').value;
        var mapDetails = mapContent.split('^');
        map = new VEMap('myMap');
        map.LoadMap();
        if (navigator.userAgent.indexOf('Safari') != -1)
            document.getElementById("myMap").childNodes[0].style.cursor = "default";
        map.HideDashboard();
        map.SetMapStyle(VEMapStyle.Aerial);
        map.DeleteAllPushpins();
        eval(mapDetails[2]);
    }
}
function showOpenHouseDT(HouseIcoObj, divId) {
    if (document.getElementById(divId).innerHTML != '')
        toggle(divId, 0);
    document.getElementById(divId).style.left = findPosX(HouseIcoObj) + 'px';
    document.getElementById(divId).style.top = findPosY(HouseIcoObj) - 170 + 'px';
}



function showNewListingDT(HouseIcoObj, divId) {
    if (document.getElementById(divId).innerHTML != '')
        toggle(divId, 0);
    document.getElementById(divId).style.left = findPosX(HouseIcoObj) - 80 + 'px';
    document.getElementById(divId).style.top = findPosY(HouseIcoObj) - 170 + 'px';
}


function altRows(id) {
    if (document.getElementsByTagName) {

        var table = document.getElementById(id);
        var rows = table.getElementsByTagName("tr");

        for (i = 0; i < rows.length; i++) {
            if (i % 2 == 0) {
                rows[i].className = "blueRow";
            } else {
                rows[i].className = "whiteRow";
            }
        }
    }
}

function hideDropDownsLuxury() {
    if (document.all) {
        var arrDD = document.documentElement.getElementsByTagName('select');
        for (i = 0; i < arrDD.length; i++) {

            if (arrDD[i].parentNode.parentNode.parentNode.parentNode.id != "FnATable")
                arrDD[i].style.display = "none";
            //arrDD[i].style.visibility = "hidden";
        }
    }
}

function OpenPhotoInMain(PhotoURL) {
    document.getElementById('PropertyMainPhoto').setAttribute('src', PhotoURL);
}



function DisplayMoreDetails(Remark) {
    var temp;
    var temp1 = '';
    var start = 0;
    var y, z;

    do {
        y = 0;
        z = 0;
        temp = Remark.substr(start, 25);
        y = temp.lastIndexOf(' ');
        z = temp.lastIndexOf(',');
        if (y != -1) {
            temp1 = temp1 + temp.substr(0, y + 1) + '<br>';
            start = start + y + 1;
        }
        if (y == -1 && z != -1) {
            temp1 = temp1 + temp.substr(0, z + 1) + '<br>';
            start = start + z + 1;
        }
        if (temp.length < 25)
            temp1 = temp1 + temp;
    } while (start < Remark.length && temp.length >= 25);


    Remark = temp1.substr(0, temp1.length);
    return Remark;
}

function ToggleDiv(Id, Remark) {
    var elemSummary = document.getElementById(Id.id);
    var elemSummaryLinkTxt
    var elemSummaryLink


    if (Id.id.substr(Id.id.length - 1, 1) == '0') {
        elemSummaryLinkTxt = Id.id.substr(0, Id.id.length - 1) + '1';
        elemSummaryLink = document.getElementById(elemSummaryLinkTxt);
        elemSummaryLink.innerText = "";
        elemSummaryLink.innerHTML = DisplayMoreDetails(Remark);
    }
    else {
        elemSummaryLinkTxt = Id.id.substr(0, Id.id.length - 1) + '0';
        elemSummaryLink = document.getElementById(elemSummaryLinkTxt);
        Id.innerText = "";
        Id.innerHTML = DisplayMoreDetails(Remark);
    }

    //var elemSummaryLink=document.getElementById(elemSummaryLink);


    if (elemSummary.style.display == 'none') {
        elemSummaryLink.style.visibility = 'hidden';
        elemSummaryLink.style.display = 'none';
        elemSummary.style.visibility = 'visible';
        elemSummary.style.display = 'block';
    }
    else {
        elemSummaryLink.style.visibility = 'visible';
        elemSummaryLink.style.display = 'block';
        elemSummary.style.visibility = 'hidden';
        elemSummary.style.display = 'none';
    }
}

function AddFavorites(mlsCompanyID, mlsNumber) {

    if (typeof (mlsCompanyID) == 'undefined' || typeof (mlsNumber) == 'undefined')//user login without click on Add to favorite link of the listing
    {
        document.location.href = document.location.href;
        return;
    }

    var result = PropertyDetails.AddFavorites(mlsCompanyID, mlsNumber);
    if (result.value == null || result.value == 0) {

        var h = document.body.scrollTop;
        window.scroll(0, h); // horizontal and vertical scroll increments
        OpenLoginDiv("You must be logged in to save this listing.", mlsCompanyID + "^" + mlsNumber);
    }
    else
        document.getElementById('favorite_span').innerHTML = "<img alt='Added to Favorites' title='Already Added to favorites' src='../images/AddToFav-added.png' style='border:none'/>";

    return false;
}

// Start: Listing photos image library
var secs
var timerID = null
var timerRunning = false
var delay = 1000
var photoNumber = 0;

function StartSlideShow() {
    photoNumber = 0;
    InitializeTimer();
}

function StartScroll(scrollDirection) {
    InitializeTimerForScrolling(scrollDirection);
}

function InitializeTimerForScrolling(scrollDirection) {
    // Set the length of the timer, in seconds
    secs = 4
    delay = 10
    StopTheClock()
    StartTheTimerForScrolling(scrollDirection)
}

function StartTheTimerForScrolling(scrollDirection) {
    if (secs == 0) {
        StopTheClock()
        // Here's where you put something useful that's
        // supposed to happen after the allotted time.
        // For example, you could display a message:
        if (scrollDirection == 'down')
            scrollDown();
        else
            scrollUp();
        InitializeTimerForScrolling(scrollDirection);

    }
    else {
        self.status = secs
        secs = secs - 1
        timerRunning = true
        timerID = self.setTimeout("StartTheTimerForScrolling('" + scrollDirection + "')", delay)
    }
}


function InitializeTimer() {
    // Set the length of the timer, in seconds
    secs = 2
    delay = 1000
    StopTheClock()
    StartTheTimer()
}

function StopTheClock() {
    if (timerRunning)
        clearTimeout(timerID)
    timerRunning = false
}

function StartTheTimer() {
    if (secs == 0) {
        StopTheClock()
        // Here's where you put something useful that's
        // supposed to happen after the allotted time.
        // For example, you could display a message:
        var ImageList = document.getElementById(prefix + 'ImageList');
        if (photoNumber < ImageList.options.length) {
            setImage(ImageList.options[photoNumber].value);
            photoNumber = photoNumber + 1;
            InitializeTimer();
        }
    }
    else {
        self.status = secs
        secs = secs - 1
        timerRunning = true
        timerID = self.setTimeout("StartTheTimer()", delay)
    }
}
function scrollDown() {
    var photoDiv = document.getElementById("PhotoDiv");
    photoDiv.style.position = "relative";
    photoDiv.style.top = "0px";
    var actualwidth = photoDiv.offsetWidth;
    var photoDivContainer = document.getElementById("PhotoDivContainer");
    photoDivContainer.style.position = "relative";
    if (parseInt(photoDiv.style.left) > (actualwidth * (-1)) + 550) //if scroller hasn't reached the end of its height
        photoDiv.style.left = parseInt(photoDiv.style.left) - 10 + "px" //move scroller upwards
}

function scrollUp() {
    var photoDiv = document.getElementById("PhotoDiv");
    photoDiv.style.position = "relative";
    photoDiv.style.top = "0px";
    var actualwidth = photoDiv.offsetWidth;
    var photoDivContainer = document.getElementById("PhotoDivContainer");
    photoDivContainer.style.position = "relative";
    if (parseInt(photoDiv.style.left) < 0) //if scroller hasn't reached the end of its height
        photoDiv.style.left = parseInt(photoDiv.style.left) + 10 + "px" //move scroller upwards
}
function setImage(photoURL) {
    
    if (document.getElementById('highSlideDiv') != null) {
        document.getElementById('highSlideDiv').style.display = 'block';
    }

    var MainPhotoHref = document.getElementById('MainPhoto');
    var centralImage = document.getElementById('centralImage');
    centralImage.setAttribute("src", photoURL);
    MainPhotoHref.setAttribute("href", photoURL);
    if (document.getElementById("PhotoDivContainer").style.top == "") {
        setTimeout('document.getElementById("PhotoDivContainer").style.top = "0px";', 100);
    }
}
// END: Listing photos image library
