// ----------------------------------------------------------------------------------------------
// --------------------------------- PLAN GALLERY FUNCTIONS -------------------------------------
// ----------------------------------------------------------------------------------------------

// invoke the ajax web method - return the arraylist of 'plangalleryitem' objects for the plan module
function loadPlanGallery() {
    var siteID = document.getElementById('siteID').value; 
    var communityID = document.getElementById('communityID').value;  
    Pulte08.AjaxWebServices.PlanGalleryService.GetSeriesForCommunity(siteID, communityID, onSuccess2);   
}


// call back function for plangalleryservice.getplansforcommunity
function onSuccess2(result) {
    if (result != null && result.length > 0) {
        $get('seriesPlans').innerHTML = buildSeriesPlansHTML(result);
        customShowHide('seriesPlans'); // hide and show drop down
		redrawNavHeight(); // redraw page height if left nav is > main content
    }
    else {$get('seriesPlans').innerHTML = 'There are no plans available at this time'}
}

// invoke the ajax web method - return the arraylist of 'plangalleryitem' objects for fast pass
function loadPlanGalleryForPassFast() {
    var siteID = document.getElementById('siteID').value; 
    var communityID = document.getElementById('communityID').value; 
    Pulte08.AjaxWebServices.PlanGalleryService.GetSeriesForCommunity(siteID, communityID, onSuccess3);
}

// call back function for plangalleryservice.getplansforcommunityonfastpast
function onSuccess3(result) {
    if (result != null && result.length > 0) {
        var html = buildSeriesPlansHTML(result);
        html =  '<h2>Home models in this neighborhood</h2>' +
                '<div class="hr"><hr /></div>' + html;
        $get('seriesPlansFastPass').innerHTML = html;
        customShowHide('seriesPlansFastPass'); // hide and show drop down
    }
    else {$get('seriesPlansFastPass').innerHTML = ''}
}
    
function buildAvailablityHTML(availablity) {
    var html='';
    if (availablity == true) html = '<img src="/images/Pulte/button-status-availableNow.gif" alt="" /></a></dt>';
    return html;
}

function buildMatchSearchCriteriaHTML(isMatch) {
    var html="";
    if (isMatch) html = '<dd class="check"><img src="/images/Pulte/icon-check.gif" class="checkmark" alt="Matches Search Criteria" /></dd>';
    return html;
}

// build the html for showing series and plans for the plan gallery
function buildSeriesPlansHTML(result) {
    var fromPage = document.getElementById('fromPage').value;
    if (fromPage == 'Plan') var planID = document.getElementById('planID').value;
    var display='';
    var planInSeries = 0;
    var planExists = false;
    if (result != null) {
        for(var i=0;i<result.length;i++) {
            var html1='';
            planInSeries = 0;
            planExists = false;
            // do not show the series dropdown bar
            if (result[i].SeriesName != ' ' && (fromPage == 'Community' || fromPage == 'Plan' || (result[i].length > 1 || (planID != result[i].PlanGalleryItemList[0].PlanId)))){
                    html1 = '<div class="toggle">' +
                            '<div class="coolBtn series"><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 class="toggleTrigger toggleBtn" title="Toggle Dropdown"><img src="/images/Pulte/icon-plus-community.gif" alt="Activate Menu" /><span>'+ result[i].SeriesName +'</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>' +
                            '<dl class="series">' +
                            '<dd>' + result[i].SeriesDescription + '</dd>' +
                            '</dl>' ;
            }

            //check if current plan exists in this series   
            for(var k=0; k < result[i].PlanGalleryItemList.length; k++) {
                 if(planID == result[i].PlanGalleryItemList[k].PlanId){  
                    planExists = true;
                    break;
                }
            }
            
            if(planExists == true){
                if(result[i].PlanGalleryItemList.length == 1){            
                    planInSeries = 1; //current plan is the only plan in the series
                }
                else{
                    planInSeries = 2; //current plan is one among many plans in the series
                }                
            }
            
            if (result[i].SeriesName != ' '){
                if(planInSeries == 0){ 
                    var html2 = '<div class="toggleTarget hide">' +
                                '<div class="hr"><hr /></div>';
                }
                else{
                    //if current plan exists in this series
                   html1 = '<div class="toggle">' +
                        '<div class="coolBtn series"><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 class="toggleTrigger toggleBtn" title="Toggle Dropdown"><img src="/images/Pulte/icon-minus-community.gif" alt="Deactivate Menu" /><span>'+ result[i].SeriesName +'</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>' +
                        '<dl class="series">' +
                        '<dd>' + result[i].SeriesDescription + '</dd>' +
                        '</dl><div class="toggleTarget show"><div class="hr"><hr /></div>';
                        
                   html2 = '';
                }
            }            
            else {var html2 = '<div class="toggleTarget show">'}

            var isMatch = false;
          
            for(var j=0; j < result[i].PlanGalleryItemList.length; j++) {
           
                if (fromPage == 'Community' || fromPage == 'Plan'){
                    if(planID != result[i].PlanGalleryItemList[j].PlanId)
                    {
                        var bathRoom = result[i].PlanGalleryItemList[j].NumFullBathrooms;
                        //if (result[i].PlanGalleryItemList[j].NumHalfBathrooms >= 1) bathRoom = bathRoom + '.5';
                        
                        if (result[i].PlanGalleryItemList[j].NumHalfBathrooms == 1) 
                            bathRoom = bathRoom + '.5';
                        else if(result[i].PlanGalleryItemList[j].NumHalfBathrooms > 1)
                             bathRoom = bathRoom + '.5+';
                        
                        if (result[i].PlanGalleryItemList[j].Price > 0)
                        {
                            var priceInfo = '$' + addCommas(result[i].PlanGalleryItemList[j].Price) ;
                        }
                        else
                        {
                            var priceInfo = 'TBD';
                        }                    

                        html2 = html2 +
                        '<dl>' +
                        '<dt><a href="' + result[i].PlanGalleryItemList[j].PageURL + '"><img src="' + result[i].PlanGalleryItemList[j].ImagePath + '" alt="" width="68" height="44"/>'+ 
                        '</a></dt>' +
                        buildMatchSearchCriteriaHTML(result[i].PlanGalleryItemList[j].IsMatchSearchCriteria) +              
                        '<dd><a href="' + result[i].PlanGalleryItemList[j].PageURL + '">'+ result[i].PlanGalleryItemList[j].Name + '</a><br />' +
                        'Starting from '+ priceInfo +'<br />'+ 
                        result[i].PlanGalleryItemList[j].SqftFormatted + ' Square Feet<br />' +
                        result[i].PlanGalleryItemList[j].NumBedrooms +' Bedrooms / '+ bathRoom+' Bathrooms<br />' +
                        buildAvailablityHTML(result[i].PlanGalleryItemList[j].HomeAvailable) + '</dd>' +
                        '</dl>' +
                        '<div class="hr"><hr /></div>';

                        if (result[i].PlanGalleryItemList[j].IsMatchSearchCriteria) isMatch = true;
                    }
                    else
                    {
                        //current plan is the only one in the series
                        if(planInSeries == 1)                       
                            html2 = '<dl class="series"><dd>No other homes available in this series.</dd></dl><div class="hr"><hr /></div>';
                    }
                }
                
            }
            html1 = html1 + html2;
            if (isMatch) {
                html1 = html1 + 
                '<p class="criteria"><img src="/images/Pulte/icon-check.gif" class="checkmark" alt="Matches Search Criteria" />Matches your search criteria</p>' +
                '<div class="hr"><hr /></div>' ;
            }

            if (result[i].SeriesName != " " && (fromPage == 'Community' || (result[i].length > 1 || (planID != result[i].PlanGalleryItemList[0].PlanId)))) html1 = html1 + '</div>';
            html1 = html1 +    
            '</div>' +
            '<div class="clear"></div>';

            display = display + html1;                                           
        }
    }
    return display;
}  

// ----------------------------------------------------------------------------------------------
