﻿var C21RESTServiceUrl = "/DesktopModules/TR-MyProperties/Service.aspx";
var C21DisabledSaveButtons = new Array();

function C21ShowSavedProperties()
{
    C21ActivateTab('SavedProperties');
}
function C21ShowSearchResults()
{
    C21ActivateTab('SearchResults');
}

function C21SidebarDefault()
{
    if( jQuery("#SavedPropertiesPane").attr("display") != "none" && jQuery("#SearchResultsPane").attr("display") != "none" )
    {
        // both panes are shown, just show saved properties by default
        C21ShowSavedProperties();
    }  
}

function C21SetSidebarProperty(mlnum)
{
    var sidebarDivId = "#sidebar" + String(mlnum);
    if(jQuery("#SearchResultsPane").css("display") != "none")
    {
        // pane is visible, lets scroll
        var $scrollDiv = jQuery('#SearchResultsPaneContent');
        var $sidebarDiv = jQuery(sidebarDivId);
        if($sidebarDiv != null && $scrollDiv != null)
        {
            // highlight the current property
            $sidebarDiv.addClass("CurrentPropertyDetail");
            
            // scroll the sidebar to the current property
            if($scrollDiv[0] != null)
            {
                $scrollDiv[0].scrollTo( $sidebarDiv.offset().top - ($scrollDiv.offset().top + $sidebarDiv.height()) );
            }
            
            // set the URL for the "Next Listing" button on the Property Detail page
            var nextPropertyUrl = $sidebarDiv.next().next().find("a").attr("href");
            if(nextPropertyUrl != "" && nextPropertyUrl != null)
            {
                jQuery('#propDetailNextListingBtn').attr("href",nextPropertyUrl);
                jQuery('#propDetailNextListingBtn').show();
            }
            else
            {
                jQuery('#propDetailNextListingBtn').hide();
            }
        }
    }
}

function C21ActivateTab(tab)
{    
    // change the tab button style
    jQuery(".Tab").removeClass("Active");
    jQuery("#" + tab + "Tab").addClass("Active"); 
       
    // show/hide the correct <div>'s
    if(tab == "SearchResults")
    {
        jQuery("#SavedPropertiesPane").hide();
        jQuery("#SearchResultsPane").show();
    }
    else if(tab == "SavedProperties")
    {
        jQuery("#SavedPropertiesPane").show();
        jQuery("#SearchResultsPane").hide();    
        //jQuery('#SavedPropertiesPaneContent').jScrollPane({showArrows:true,scrollbarWidth:17,scrollbarMargin:2});
    }
}

function C21AddUserSavedProperty(userId, mlnum)
{
    jQuery.post(C21RESTServiceUrl,
        { a: "AddUserProperty", u: userId, mlnum: mlnum },
        function(data){           
            var response = ParseRESTRespone(data);
            C21UpdateSavedPropertiesDiv(response[0], response[1], true);
        }
    );    
}

function C21RemoveUserSavedProperty(userId, mlnum)
{
    jQuery.post(C21RESTServiceUrl,
        { a: "RemoveUserProperty", u: userId, mlnum: mlnum },
        function(data){
            //alert("Data Loaded: " + data);            
            var response = ParseRESTRespone(data);
            C21UpdateSavedPropertiesDiv(response[0], response[1], true);
            RevertImage(mlnum);
        }
    );    
}

function C21UpdateSavedPropertiesDiv(count, htmlContent, doFade)
{
    var prevCount = jQuery("#SavedPropertyCount").text();
    jQuery("#SavedPropertyCount").text( count );
    if(doFade && parseInt(prevCount) != parseInt(count))
    {
        jQuery("#SavedPropertyCountMsg").highlightFade( {color:'#FBE733',speed: 2000} );     
    }
    
    jQuery("#SavedPropertiesPaneContent").html( htmlContent );
    // have to re-initialize the jScrollPane since the content changed
    //jQuery('#SavedPropertiesPaneContent').jScrollPane({showArrows:true,scrollbarWidth:17,scrollbarMargin:2});
}

function C21GetUserSavedProperties(userId)
{
    jQuery.post(C21RESTServiceUrl,
        { a: "GetUserSavedProperties", u: userId },
        function(data){      
            var response = ParseRESTRespone(data);
            C21UpdateSavedPropertiesDiv(response[0], response[1], false);
        }
    );    
}

function ReplaceImage(elm, newImageSrc, mlnum)
{
    if(elm != null)
    {
        if(elm.src)
        {
            elm.src = newImageSrc;
            C21DisabledSaveButtons[mlnum] = elm;        
        }
    }
}
function RevertImage(mlnum)
{
    if(C21DisabledSaveButtons[mlnum] != null)
    {
        var elm = C21DisabledSaveButtons[mlnum];
        elm.src = elm.src.replace("_off","");
    }
}
function GetChildImageElm(elm)
{
    for(var i = 0; i < elm.childNodes.length; i++)
    {
        if(elm.childNodes[i].nodeType == 1)
        {
            if(elm.childNodes[i].tagName.toUpperCase() == "IMG")
            {
                return elm.childNodes[i];
            }
        }
    }
    return null;
}
