// ----------------------------------------------------------------------------------------------
// ----------------------------------- GLOBAL FUNCTIONS -----------------------------------------
// ----------------------------------------------------------------------------------------------

// invoke the ajax web method - return the arraylist of 'plangalleryitem' objects for the plan module
function loadInventoryHome() {

    var fromPage = document.getElementById('fromPage').value;   
    if (fromPage == 'Community') {
		var siteID = document.getElementById('siteID').value; 
        var communityID = document.getElementById('communityID').value;     
        // call webservice - return a flag to indicate if the "homes available now" tab should be shown
        Pulte08.AjaxWebServices.InventoryHomeService.GetInventoryHomeForCommunity(siteID, communityID, onSuccessLoadHomeAvailableNow);
    }
    else {
        var siteID = document.getElementById('siteID').value; 
        var planID = document.getElementById('planID').value;   
        // call webservice - homes available now for plan
        Pulte08.AjaxWebServices.InventoryHomeService.GetInventoryHomeForPlan(siteID, planID, onSuccess);
    }
}

// call back function for InventoryHomeService.GetInventoryHomeForPlan
function onSuccess(result) {
    $get('homesAvailableNowForPlan').innerHTML = buildHTML(result);
    // hide/show drop down for showing 'home available now'
    if (result != null) {
		if (getCurrSite()=='Pulte') {
			Rounded('pTabHdr','div','top','transparent','#D3B77E');
			Rounded('pTabFtr','div','bottom','transparent','#BF9A46');
			Rounded('selectedPlanCorners','div','top','transparent','#FFF','border #D6BB85');
			Rounded('selectedPlanCorners','div','bottom','transparent','#FCF8EF','border #D6BB85');
		}
		if (getCurrSite()=='DelWebb') {
			Rounded('pTabHdr','div','top','transparent','#CAD14B','border #AFB720');
			Rounded('pTabFtr','div','bottom','transparent','#AFB720','border #AFB720');
			Rounded('selectedPlanCorners','div','top','transparent','#FFF','border #CBD24C');
			Rounded('selectedPlanCorners','div','bottom','transparent','#F9F9ED','border #CBD24C');
		}
		if (getCurrSite()=='DiVosta') {
			Rounded('pTabHdr','div','top','transparent','#A6CCE8');
			Rounded('pTabFtr','div','bottom','transparent','#68A8DC');
			Rounded('selectedPlanCorners','div','top','transparent','#FFF','border #9EC7E6');
			Rounded('selectedPlanCorners','div','bottom','transparent','#EDF4FA','border #9EC7E6');
		}
		customShowHide('homesAvailableNowForPlan');
	}
}

// build the html for 'home available now' for inventory home
function buildHTML(result) {
	display = '';
    communityPageURL = '';

    if (result != null && result.length !=0) {
 
        
        var display = '<div class="bdy toggle"><div class="coolBtn"><div class="pTabHdr"></div><h3><a class="toggleTrigger toggleBtn" title="Toggle Dropdown"><img src="/images/Pulte/icon-plus-plan.gif" alt="Activate Menu" /><span>';
        //display += '<img src="/assets/images/icon-MoveInSmall.gif" alt="" style="position:absolute; left:2px; top:.8em;" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
        var labelText = 'Quick Move-In Home';
        if (result.length == 1) {labelText = labelText + ' (1)'}
        else {labelText = labelText + 's (' + result.length + ')'}       
        
        display += labelText +
        '</span></a></h3><div class="pTabFtr"></div></div>' +
        '<div class="selectedPlanCorners toggleTarget hide">';

        for(var i=0; i < result.length && i<4; i++) {
			  var html =       '<ul>' +
							 '<li><a href="' + result[i].PageURL + '">' + result[i].Street + '</a></li>' +
                             '<li>' + "- <i>" +  result[i].Overview + '</i></li>' +
                             '<li class="tabRight"></li>' +
                             '</ul>';

            if (result[i].PageURL.lastIndexOf != null) {
                var lastIndex = result[i].PageURL.lastIndexOf('/homes/');
                communityPageURL = result[i].PageURL.substring(0,lastIndex);
                communityPageURL = communityPageURL + '/index.aspx?selected=homes-available-now'
            }

            if (i < (result.length - 1)) html = html + '<div class="hr"><hr /></div>';
            display = display + html;               
        }

        display = display + '</div></div>';
        if (result.length >= 3) display = display + '<a href="' + communityPageURL + '">more >></a>';
    }
    return display;
}

// callback - GetInventoryForCommunity
function onSuccessLoadHomeAvailableNow(result) {
    if (result && result.length && result.length > 0) $get('homeAvailableNowForCommunity').innerHTML = buildHomeAvailableNowForCommunityHTML(result);
}


// build the html for 'home available now' tab for community page
function buildHomeAvailableNowForCommunityHTML(result) {
    var communityID = document.getElementById('communityID').value; 
    var communityName = document.getElementById('communityName').value; 
    var communityCityStateZip = document.getElementById('communityCityStateZip').value;
    var phone = document.getElementById('phone').value; 
    var fax = document.getElementById('fax').value; 
    var hours = document.getElementById('hours').value; 

    var display='';
    if (result != null) {

        for(var i=0;i<result.length;i++) {

            var bathRoom = result[i].Bathrooms;
            if (result[i].HalfBaths == 1) 
                bathRoom = bathRoom + '.5';
            else if(result[i].HalfBaths > 1)
                 bathRoom = bathRoom + '.5+';
                 
            var html =          
                        '<table summary="Homes Available Now">' +
							'<tbody><tr>' +
							'<td class="img"><img src="' + result[i].ImagePath + '" alt="" width="68" /></td>' +
							'<td class="plan">' +
							'<dl>' +
							'<dt><a href="' + result[i].PageURL + '">' + result[i].PlanName + '</a></dt>' +
							'<dd>$' + addCommas(result[i].Price) + '</dd>' +
							'<dd>' + result[i].Bedrooms + ' Bedrooms / ' + bathRoom + ' Baths</dd>' +
							'<dd>' + result[i].SqFoot + ' Square Feet</dd>' +
                            '<dd><a href="javascript:void(0);" onclick="NotebookUtils.saveInventoryHomeToNotebook('+ result[i].InventoryHomeID + ',this,\'pt-savedYes\');" class="pt-savedNo addtoNotebookTrigger">Save to My Notebook</a></dd>' +
							'</dl></td>' +
							'<td rowspan="2" class="desc">' + result[i].Overview +
							'<p><a href="' + result[i].PageURL + '">Full Details</a></p>' +
							'</td>' +
							'<td rowspan="2" class="address">' +
							'<dl>' +
							'<dt>' + communityName + '</dt>' +
							'<dd>' + result[i].Street + '</dd>' +
							'<dd>' + communityCityStateZip + '</dd>' +
							'<dd><span>' + phone + '</span><strong>Phone:</strong></dd>' +
							'<dd><span>' + fax + '</span><strong>Fax:</strong></dd>' +
							'<dd><span>' + hours + '</span><strong>Hours:</strong></dd>' +
							'</dl></td></tr>' +
							'<tr>' +
							'<td colspan="2" class="button"><div class="coolBtn"><div style="padding-top:0;" class="cHdr"><b style="background-color:transparent;" class="rtop"><b style="background-color:rgb(86, 145, 190);" class="r1"></b><b style="background-color:rgb(86, 145, 190);" class="r2"></b><b style="background-color:rgb(86, 145, 190);" class="r3"></b><b style="background-color: rgb(86, 145, 190);" class="r4"></b></b></div>' +
							'<h3><a href="' + urlSiteBase + '/customer-service/contact-us.aspx?pagetitle=' + communityName + '&communityid=' + communityID + '"><span>Contact Us</span></a></h3><div style="padding-bottom:0" class="cFtr"><b style="background-color:transparent;" class="rtop"><b style="background-color:rgb(14, 64, 115);" class="r4"></b><b style="background-color:rgb(14, 64, 115);" class="r3"></b><b style="background-color:rgb(14, 64, 115);" class="r2"></b><b style="background-color:rgb(14, 64, 115);" class="r1"></b></b></div></div></td>' +
							'</tr></tbody>' +                           
                       '</table>' ;
            display = display + html;                                            
        }
    }
    return display;
}

// ----------------------------------------------------------------------------------------------
