﻿function ListingDetail(wsUrl){
    this.http = new WDCHttp(wsUrl);
    this.popups = {};
    this.currentPopup = null;
}

ListingDetail.prototype.getCompactListing = function(listingId, callback){
    var params = String.format('mode=compactlisting&p={0}', listingId.toString());
    this.http.get(this,params,callback);
};

ListingDetail.prototype.getResultListing = function(listingId, searchqs, callback){
    var params = String.format('mode=resultlisting&p={0}&{1}', listingId.toString(),searchqs);
    this.http.get(this,params,callback);
};

ListingDetail.prototype.attachListingPopup = function(elemId, listingId){
    var context = this;
    var elem = document.getElementById(elemId);
    elem.onmouseover = function(){
        var popup = context.popups[listingId];
        if(context.currentPopup!=null&&context.currentPopup!=popup){
            context.currentPopup.style.visibility = 'hidden';
            context.currentPopup = null;
        }
        var pos = findPos(elem);
        var top = (pos.top+elem.offsetHeight) + 'px';
        var left = pos.left + 'px';
        if(popup!=null){
            popup.hide=false;
            popup.style.top = top;
            popup.style.left = left;
            popup.style.visibility = 'visible';
            context.currentPopup = popup;
        } else {
            context.createListingPopup(elem,listingId,top,left);
        }
    };
    elem.onmouseout = function(){
        var popup = context.popups[listingId];
        if(popup!=null){
            popup.hide=true;
            setTimeout(function(){if(popup.hide){popup.style.visibility = 'hidden';}},250);
        }
    };
};
ListingDetail.prototype.createListingPopup = function(elem,listingId,top,left){
    this.getCompactListing(listingId,function(context,result){
        var div = context.createListingPopupDiv(result);
        div.className = 'listingPopup';
        div.style.top = top;
        div.style.left = left;
        document.body.appendChild(div);
        context.popups[listingId] = div;
        context.currentPopup = div;
        div.onclick = function(){if(elem.click){elem.click();}};
        div.onmouseover = function(){div.hide=false;};
        div.onmouseout = function(){
            div.hide=true;
            setTimeout(function(){if(div.hide){div.style.visibility = 'hidden';}},250);
        };
        
    });
};

ListingDetail.prototype.createListingPopupDiv = function(listing){
    var div = document.createElement('div');
    var imgDiv = document.createElement('div');
    imgDiv.style.height='75px';
    var img = document.createElement('img');
    img.style.height='75px';
    img.style.width='auto';
    img.src = listing.thumburl;
    imgDiv.appendChild(img);
    div.appendChild(imgDiv);
    var detail = document.createElement('div');
    detail.style.padding = '2px';
    var html = '';
    if(listing.addr!=null){
        html += listing.addr + '<br/>';
    }
    html += listing.city + ', ' + listing.state;
    if(listing.price!=null){
        html += '<br/>$' + addCommas(listing.price);
    }
    html += '<br/><a href="' + listing.url + '">View Details</a>';
    detail.innerHTML = html;
    div.appendChild(detail);
    div.style.visibility = 'visible';
    return div;
};

function saveListing(anchor, listingId, mlsName, mlsNumber){
    _account.saveListing(listingId, mlsName, mlsNumber, function(context,result){
        if(result.status=='success'){
            anchor.innerHTML = 'Saved';
            anchor.href = _appPath+'/myweichert/savedlistings.aspx';
            anchor.onclick = function(){};
        }
    });
}

function listingMarkerClick(context,marker){
    if(marker.info.desc!=null){
        context.showMarkerPopup(context, marker);
    }
    if(marker.info.url!=null){
       window.location=url;
    }
}

function createCallout(href){
    var c = createTag('div',{'id':'contactCallout'});
    c.appendChild(createTag('span', {'class':'contactCallout'}));
    c.appendChild(createTag('img', {'src':_appPath+'/images/impressions.gif?impid=ldcallout&rnd='+(new Date()).getTime(),'useMap':'#calloutMap'}));
    var m = createTag('map',{'id':'calloutMap','name':'calloutMap'});
    var area = createTag('area', {'shape':'POLY','href':'javascript:void(0);','coords':'128,6, 172,6, 172,34, 128,34','title':'Close'});
    area.onclick = function(){return hideCallout(c);};
    m.appendChild(area);
    area = createTag('area', {'shape':'POLY','href':href,'coords':'112, 86, 170,86, 170,116, 112,116','title':'Ask us'})
    area.onclick = function(){window.open(href,'contactCallout','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=no,width=600,height=380,top=40,left=40');return false;};
    m.appendChild(area);
    c.appendChild(m);
    setupFadeIn(c,750,1000);
    var main;
    if(main = document.getElementById('main')){main.appendChild(c);}
}
function hideCallout(elem){
    elem.style.display='none';
    setCookie('contactCalloutShown','1',null,'/');
    return false;
}


