var pixelsToMove = 6;
var speed = 20;
var infoList = new Array();
function infoDetail (ele,cutaway,ind) {
    this.ele = ele;
    this.cutawayEle = cutaway;
    this.cutawayBorder = false;
    this.height = ele.offsetHeight;
    this.currHeight = 0;
    this.ind = ind;
    this.grower = null;
    this.shrinker = null;
    this.open = false;
    var thisObj = this;
    this.grow = function() {
        if (thisObj.shrinker) clearTimeout(thisObj.shrinker);
        if (!thisObj.cutawayBorder) {
            thisObj.cutawayEle.style.borderTop = '1px solid #ccc';
            thisObj.cutawayBorder = true;
        }
        if (thisObj.currHeight < thisObj.height) {
            if (thisObj.currHeight + pixelsToMove > thisObj.height) {
                thisObj.currHeight = thisObj.height;
                thisObj.ele.style.height = thisObj.height + 'px';
                if (thisObj.grower) clearTimeout(thisObj.grower);
            } else {
                thisObj.currHeight = thisObj.currHeight + pixelsToMove;
                thisObj.ele.style.height = thisObj.currHeight + 'px';
                thisObj.grower = setTimeout('infoList[' + thisObj.ind + '].grow()',speed);
            }
        }
    }
    this.shrink = function() {
        if (thisObj.grower) clearTimeout(thisObj.grower);
        if (thisObj.currHeight > 0) {
            if (thisObj.currHeight - pixelsToMove < 0) {
                thisObj.currHeight = 0;
                thisObj.ele.style.height = '0px';
                thisObj.cutawayEle.style.borderTop = 'none';
                thisObj.cutawayBorder = false;
                if (thisObj.shrinker) clearTimeout(thisObj.shrinker);
            } else {
                thisObj.currHeight = thisObj.currHeight - pixelsToMove;
                thisObj.ele.style.height = thisObj.currHeight + 'px';
                thisObj.shrinker = setTimeout('infoList[' + thisObj.ind + '].shrink()',speed);
            }
        } else if (thisObj.cutawayBorder) {
            thisObj.cutawayEle.style.borderTop = 'none';
            thisObj.cutawayBorder = false;
        }
    }
}
function addInfo(ind) {
    var temp = document.getElementById('more_' + ind);
    infoList[ind] = new infoDetail(temp);
}
function toggleShow(ind) {
    var showEle = document.getElementById('show_services_' + ind);
    var hideEle = document.getElementById('hide_services_' + ind);
    if (infoList[ind].open) {
        showEle.style.display = 'inline';
        hideEle.style.display = 'none';
        infoList[ind].open = false;
        checkForHideAll();
    } else {
        showEle.style.display = 'none';
        hideEle.style.display = 'inline';
        infoList[ind].open = true;
        checkForExpandAll();
    }
}
function checkForExpandAll() {
    var allOpen = true;
    for (var i=0; i<infoList.length; i++) {
        if (infoList[i]) {
            if (!infoList[i].open) {
                allOpen = false;
                break;
            }
        }
    }

    if (allOpen) {
        expandAll();
    }
}
function checkForHideAll() {
    var allClosed = true;
    for (var i=0; i<infoList.length; i++) {
        if (infoList[i]) {
            if (infoList[i].open) {
                allClosed = false;
                break;
            }
        }
    }

    if (allClosed) {
        hideAll();
    }
}
function expandAll() {
    var showLayer = document.getElementById('show_all');
    var hideLayer = document.getElementById('hide_all');
    showLayer.style.display = 'none';
    hideLayer.style.display = 'block';
    for (var i=0; i<infoList.length; i++) {
        if (infoList[i]) {
            infoList[i].grow();
            if (!infoList[i].open) {
                toggleShow(i);
            }
        }
    }
}
function hideAll() {
    var showLayer = document.getElementById('show_all');
    var hideLayer = document.getElementById('hide_all');
    hideLayer.style.display = 'none';
    showLayer.style.display = 'block';
    for (var i=0; i<infoList.length; i++) {
        if (infoList[i]) {
            infoList[i].shrink();
            if (infoList[i].open) {
                toggleShow(i);
            }
        }
    }
}
var popwin = '';
function closePopWin() {
    if (popwin && popwin.open && !popwin.closed) popwin.close();
}
function openPopWin(winURL, scrollBar, resizable, winWidth, winHeight) {
    //Safari 3.1.1 fix: def 16888  - if safari and winHeight is auto, make it an integer or the window will 'collapse.'  I used window.innerHeight to grab the height of the viewport, since we know Safari supports this property.
    if ( /safari/i.test(navigator.userAgent) && /auto/i.test(winHeight) ) {
        //now check that innerWidth is still defined for Safari... currently it works in safari 3.1.1 for both Win & Mac.
        typeof window.innerWidth != 'undefined' ? winHeight =  window.innerHeight : winHeight=500;
    }
    closePopWin();
    var winOptions = 'menubar=no,toolbar=no,location=no,directories=no,status=no,scrollbars=' + scrollBar + ',resizable=' + resizable + ',width=' + winWidth + ',height=' + winHeight;
    popwin = window.open(winURL, 'popwin', winOptions);
}

/* Filter Submission */
function handleFilter() {
    var filterString = '';
    var form = document.filterForm;
    for (var i=0; i<form.length; i++) {
        if (form.elements[i].type == 'checkbox' && form.elements[i].checked) {
            // include the leading comma so we can delimit correctly
            filterString += ',' + form.elements[i].value;
        }
    }

    //continue to show map when filters are chosen if it is currently being displayed
    if (document.getElementById('mapContainer') && document.getElementById('mapContainer').style.display == "block") {
        document.filterFormSubmit.showMap.value = "true";
    }

    document.filterFormSubmit.filters.value = filterString;
    document.filterFormSubmit.submit();
}
function checkFilter(item) {
    var formItem = eval('document.filterForm.c_' + item);
    formItem.click();
}

/* ignore this for now - just playing with ideas of reducing code bloat sent to user */
var filterMap = new Array();
filterMap['f1'] = 'Special Rate Financing';
filterMap['f2'] = 'Low Credit Score Financing';
filterMap['f3'] = 'Financing for Credit-Challenged';

filterMap['s1'] = 'Service Center';
filterMap['s2'] = 'Factory Authorized Service Center';
filterMap['s3'] = 'Body Shop';
filterMap['s4'] = 'Customization';
filterMap['s5'] = 'Detailing';
filterMap['s6'] = 'Parts & Accessories';
filterMap['s7'] = 'Concierge';
filterMap['s8'] = 'Delivery Options';
filterMap['s9'] = 'Courtesy Shuttle';
filterMap['s10'] = 'Loaner Cars';
filterMap['s11'] = 'Rental Cars';

filterMap['v1'] = 'New';
filterMap['v2'] = 'Used';
filterMap['v3'] = 'Certified';
filterMap['v4'] = 'Classic/Collector';
filterMap['v5'] = 'Conversion Vans';
filterMap['v6'] = 'Handicap-Equipped';
filterMap['v7'] = 'Hybrid';
filterMap['v8'] = 'Commercial/Fleet';

filterMap['l1'] = 'American Sign Language';
filterMap['l2'] = 'Chinese';
filterMap['l3'] = 'French';
filterMap['l4'] = 'German';
filterMap['l5'] = 'Italian';
filterMap['l6'] = 'Korean';
filterMap['l7'] = 'Polish';
filterMap['l8'] = 'Russian';
filterMap['l9'] = 'Spanish';
filterMap['l10'] = 'Tagalog';
filterMap['l11'] = 'Vietnamese';

filterMap['a1'] = 'Business Center';
filterMap['a2'] = 'Children\'s Play Area';
filterMap['a3'] = 'Comfortable Waiting Area';
filterMap['a4'] = 'Handicapped Accessible';
filterMap['a5'] = 'Internet Access';

for (var i in filterMap) {
    //alert(filterMap[i]);
}

function createFilterElements() {
    var container = document.getElementById('filters');
    var formEle = document.createElement('form');
    formEle.name = 'filterForm';
    var tableEle = document.createElement('table');
    var tableBodyEle = document.createElement('tbody');
    for (var i in filterMap) {
        var trEle = document.createElement('tr');
        var tdCheckEle = document.createElement('td');
        tdCheckEle.className = 'cb';
        var inputEle = document.createElement('input');
        inputEle.type = 'checkbox';
        inputEle.name = i;
        inputEle.value = filterMap[i];
        inputEle.onclick = 'handleFilter()';

        tdCheckEle.appendChild(inputEle);
        trEle.appendChild(tdCheckEle);

        var tdLabelEle = document.createElement('td');
        tdLabelEle.innerHTML = filterMap[i];

        trEle.appendChild(tdLabelEle);
        tableBodyEle.appendChild(trEle);
    }
    tableEle.appendChild(tableBodyEle);
    formEle.appendChild(tableEle);
    container.appendChild(formEle);
}

/**
 * Refresh the map with the page showing
 */
function refreshMapView() {
    var newUrl = location.href.replace(/showMap=false/,"showMap=true");
    newUrl = newUrl.replace(/#dealermap/, "");
    newUrl = newUrl.replace(/showTips=true/,"showTips=false");
    if (newUrl.indexOf("showMap") == -1) {
        newUrl += "&showMap=true";
    }
     //need to refresh the BI page instance ID
         if (window.location.href.indexOf('ref_inst') >= 0) {
              var replaceRefInst  = window.location.href.toQueryParams();
             if(typeof BIRF != "undefined"){newUrl = newUrl.replace(replaceRefInst.ref_inst, BIRF.shared.pageInstanceID)};
         }
    window.location = newUrl;
}

/**
 * Change to Tips View added for Credit Center
 */
function refreshTipsView() {
    var newUrl = location.href.replace(/showTips=false/,"showTips=true");
    newUrl = newUrl.replace(/showMap=true/,"showMap=false");
    if (newUrl.indexOf("showTips") == -1) {
        newUrl += "&showTips=true";
    }
    //need to refresh the BI page instance ID
        if (window.location.href.indexOf('ref_inst') >= 0) {
             var replaceRefInst  = window.location.href.toQueryParams();
             if(typeof BIRF != "undefined"){newUrl = newUrl.replace(replaceRefInst.ref_inst, BIRF.shared.pageInstanceID)};
        }

    window.location = newUrl;
}

/**
 *  Refresh the page without the map showing
 */
function refreshNoMapView() {
    var newUrl = location.href.replace(/&showMap=true/,"");
    newUrl = newUrl.replace(/&showTips=true/,"");
    //need to refresh the BI page instance ID
        if (window.location.href.indexOf('ref_inst') >= 0) {
             var replaceRefInst  = window.location.href.toQueryParams();
            if(typeof BIRF != "undefined"){newUrl = newUrl.replace(replaceRefInst.ref_inst, BIRF.shared.pageInstanceID)};
        }
     window.location = newUrl;
    
}

/**
 *  Change the number of records to page against and ensure map visibility remains the same
 */
function submitNumRecordsForm() {
    //continue to show map when filters are chosen if it is currently being displayed
    if (document.getElementById('mapContainer') && document.getElementById('mapContainer').style.display == "block") {
        document.numRecords.showMap.value = "true";
    }

    document.numRecords.submit();
}

/**
 *  Change the number of records to page against and ensure map visibility remains the same
 */
function submitNumRecordsForm2() {
    //continue to show map when filters are chosen if it is currently being displayed
    if (document.getElementById('mapContainer') && document.getElementById('mapContainer').style.display == "block") {
        document.numRecords2.showMap.value = "true";
    }

    document.numRecords2.submit();
}

function showNoMapToolTip() {
    $('noMapToolTipTrigger').prototip.show();
}

function showLayer(layer) {
    if(layer != null && document.getElementById){
        document.getElementById(layer).style.visibility = 'visible';
    }else if(layer != null && document.all){
        document.all[layer].style.visibility = 'visible';
    }else{
        openPopWin();
    }
}

function hideLayer(layer) {
    if (layer != null && document.getElementById) {
        document.getElementById(layer).style.visibility = 'hidden';
    }  else if (layer != null && document.all) {
        document.all[layer].style.visibility = 'hidden';
    } else {
        closePopWin();
    }
}
