/*
* The programming and software materials herein are copyright Cyberhomes LLC (CH).
* The programming and software materials are owned, held, or licensed by CH. Personal, educational,
* non-commercial, commercial or any other use of these materials, without the written permission of the
* CH, is strictly prohibited.
*/

function showFeaturedListingDisplay(html, imgsrc) {
    // If the price element doesn't exist, then Spry may still be processing the DOM
    if (!Spry.$('price')) {
        setTimeout(function() {
            showFeaturedListingDisplay(html, imgsrc);
        }, 500);
        return;
    }

    Spry.$('price').innerHTML = html;
    Spry.$('listingimg').src = imgsrc;
    document.getElementById('FeatListings_DIV').style.display = 'block';
}

function displayOnLoad(notificationType, notifier, data) {
    if (notificationType != "onPostLoad")
        return;

    // Update the image path before showing, and format the currency
    var data = _dsFeatListings.getData();
    var curRowID = getCurRowID(data);
    var curListing = data[curRowID];	
    var imgsrc = getListingImageSrc(curListing);

    showFeaturedListingDisplay('$' + addCommas(curListing.ListPriceHigh), imgsrc);    
}

function getCurRowID(data) {
    var dataLen = data.length;
    var curRowID = _dsFeatListings.getCurrentRowID();
    if (curRowID >= dataLen)
        curRowID = 0;
    else if (curRowID < 0)
        curRowID = dataLen - 1;
    return curRowID;
}

function updateListingDisplay(curListing) {
	Spry.$("price").innerHTML = "$" + addCommas(curListing.ListPriceHigh);
	Spry.$("street").innerHTML = ((curListing.UnstructuredAddress  != undefined)?curListing.UnstructuredAddress:"");
	Spry.$("citystatezip").innerHTML = ((curListing.City != undefined)?curListing.City + ", ":"") + ((curListing.StateOrProvinceCode != undefined)? curListing.StateOrProvinceCode + " ":"") + ((curListing.PostalCode != undefined)?curListing.PostalCode:"");
	Spry.$("lnkListingDetail").href = "/public" + curListing.ListingURL;	
	Spry.$('listingimg').src = getListingImageSrc(curListing);	
}

function getListingImageSrc(curListing) {
    var src = ''
    if (curListing && curListing.PhotoAvailableCd == 1 && curListing.ImageFilename)
        src = _listingImagePath  + curListing.ImageFilename;	
    else
        src = _recoImagePath + "PhotoNotAvailable_Small.gif";
    return src;
}

function go(next) {
    var data = _dsFeatListings.getData();
    var curRowID = getCurRowID(data);
    var curListing = data[curRowID];
    
    updateListingDisplay(curListing);

    if (next) curRowID++;
    else curRowID--;
	_dsFeatListings.setCurrentRow(curRowID);			
}

function addCommas(nStr) {
    nStr += '';
    x = nStr.split('.');
    x1 = x[0];
    x2 = x.length > 1 ? '.' + x[1] : '';
    var rgx = /(\d+)(\d{3})/;
    while (rgx.test(x1)) {
        x1 = x1.replace(rgx, '$1' + ',' + '$2');
    }
    return x1 + x2;
}

function ShowFeaturedProperyFormat1 () {
    var sbHtml = '';

    sbHtml += '<div id="FeatListings_DIV" spry:region="_dsFeatListings" style="display: none; padding: 10px;">';
    sbHtml += '    <table cellpadding="5" cellspacing="0" border="0" width="100px" ID="Table1" class="widget-bg" spry:state="ready">';
    sbHtml += '        <tr>';
    sbHtml += '            <td colspan="3" align="center"><h2 class="widget-header">Featured Listings</h2></td>';
    sbHtml += '        </tr>';
    sbHtml += '        <tr>';
    sbHtml += '            <td><img alt="" src="' + _commonImagePath + 'feat-list-arrow-prev.png" onclick="javascript:go(false);" style="cursor: pointer; cursor: hand;"/></td>';
    sbHtml += '            <td><a href="/public{_dsFeatListings::ListingURL}" id="lnkListingDetail"><img id="listingimg" alt="Listing Image" width="130" height="96" src="' + _recoImagePath + 'Fill.gif"/></a></td>';
    sbHtml += '            <td><img alt="" src="' + _commonImagePath + 'feat-list-arrow-next.png" onclick="javascript:go(true);" style="cursor: pointer; cursor: hand;"/></td>';
    sbHtml += '        </tr>';
    sbHtml += '        <tr>';
    sbHtml += '            <td  colspan="3" class="cssPrice" align="center" valign="middle"><div id="price" class="widget-text-bold">${_dsFeatListings::ListPriceHigh}</div></td>';
    sbHtml += '        </tr>';
    sbHtml += '        <tr>';
    sbHtml += '            <td colspan="3" class="cssStreet" align="center" valign="middle"><div id="street"  class="widget-text-bold">{_dsFeatListings::UnstructuredAddress}</div></td>';
    sbHtml += '        </tr>';
    sbHtml += '        <tr>';
    sbHtml += '            <td  colspan="3" class="cssStreet" align="center" valign="middle" nowrap="nowrap"><div id="citystatezip"  class="widget-text-bold">{_dsFeatListings::City}, {_dsFeatListings::StateOrProvinceCode} {_dsFeatListings::PostalCode}</div></td>';
    sbHtml += '        </tr>';
    sbHtml += '    </table>';
    sbHtml += '</div>';

    var d = document.getElementById('_FeaturedPropertyDiv_');
    if(d)
        d.innerHTML = sbHtml;
    else
        alert('You\'ve tried to use the Featured Property widget without the <span id="_FeaturedPropertyDiv_"></span>. Please add it.')

}

function InitializeRECoImagePath(inRECoID, inImagePath) {

    if (inRECoID != undefined && inRECoID > 0 && inImagePath != undefined && inImagePath.length > 0) {
        // Make sure there's a leading http://
        if (inImagePath.substring(0, 6) != 'http://')
            inImagePath = 'http://' + inImagePath;

        // Make sure there's an ending slash
        if (inImagePath.charAt(inImagePath.length - 1) != '/')
            inImagePath = inImagePath + '/';

        _recoImagePath = inImagePath + 'images/RECos/' + inRECoID + '/';
    }
    else
        _recoImagePath = _commonImagePath;  // We don't have a context so just use the common image

}

function ShowFeaturedProperty(inFormat, inRECoID, inImagePath) {
    $(document).ready(function() {

    InitializeRECoImagePath(inRECoID, inImagePath);

    if(inFormat == undefined)
        inFormat = 1;

    // Make sure the requested format is one that we support, if not set to 1
    if(inFormat < 1 || inFormat > 1)
        inFormat = 1;

    switch(inFormat) {
        case 1:
        default:
            ShowFeaturedProperyFormat1();
            break;
    }
        // Set up the data set
        _dsFeatListings = new Spry.Data.XMLDataSet(_urlFeaturedListingDataSource, "Root/Listings/Listing");		
        _dsFeatListings.addObserver(displayOnLoad);
    });

}


// BEGIN: Global Variables

var _protocol = location.protocol
var _host = location.host;

// Load some external scripts we need
$.include(_protocol + '//' + _host + '/Public/Include/Js/Spry/Xpath.js');
$.include(_protocol + '//' + _host + '/Public/Include/Js/Spry/SpryData.js');

var _urlFeaturedListingDataSource = _protocol + '//' + _host + '/Public/Include/Controls/Viewport/Listing/FeaturedListingXML.aspx';

var _listingImagePath = 'http://images.fnistools.com/';
var _commonImagePath = _listingImagePath + 'images/common/';
var _recoImagePath; // Value is set in InitializeRECoImagePath();

// The Spry dataset. Needs to be defined globally so that it can be by the methods herein and also the ones in the Spry framework.
var _dsFeatListings;

// END  : Global Variables


/* How to use this widget:

<script src="/Public/Include/Js/Widgets/FeaturedListings.js"></script>
<script>
    $(document).ready(function() {
        var _fpRECoID = -1; // Used to load RECo specific images. Defaults to common images if set to -1.
        var _fpFormat = 1; // Defaults to 1 if not provided.
        ShowFeaturedProperty(_fpFormat, _fpRECoID);
    });
</script>

*/