var saveCompare_function_handler = '';
var saveCompare_function_data = {};
/***
 Save Page - Start
 ***/
jQuery('.saved-remove').click(function (e) {
    vehicle = jQuery(e.target).parents('.saved-vehicle-content');
    jQuery(vehicle).slideUp();
    jQuery(vehicle).siblings('.removed-saved-vehicle-content').slideDown();
    // Remove Cookie
    Cookies.remove(jQuery(e.target).attr('data-key'));
})

jQuery('.restore-saved-vehicle').click(function (e) {
    info = {};
    vehicle = jQuery(e.target).parents('.removed-vehicle-headline');
    jQuery(vehicle).children('span').each(function () {
        info[jQuery(this).attr('data-name')] = jQuery(this).text();
    })
    jQuery(e.target).parents('.removed-saved-vehicle-content').slideUp();
    jQuery(e.target).parents('.removed-saved-vehicle-content').siblings('.saved-vehicle-content').slideDown();
    // Re-Set Cookie
    Cookies.set(jQuery(e.target).attr('data-key'), info, {expires: 30});
})

jQuery('.saved-form-action').click(function (e) {
    jQuery('#form-active').remove();
    saveCompare_function_handler = 'saveForm';
    saveCompare_function_data['key'] = jQuery(e.target).attr('data-key');
    saveCompare_function_data['form'] = jQuery('#saved-gform-id').attr('data-form');
    var form_data = {};
    jQuery('#' + saveCompare_function_data['key'] + ' .form-value').each(function () {
        form_data[jQuery(this).attr('data-name')] = jQuery(this).text();
    });
    saveCompare_function_data['hooks'] = form_data;
    saveCompare_ajax_handler();
    jQuery('html, body').animate({scrollTop: jQuery('#' + jQuery(e.target).attr('data-key')).offset().top}, 1000);
    saveCompare_function_data = {};
});

jQuery('#saved-vehicle-container').on('click', '.cancel-form', function (e) {
    jQuery(e.target).parents('.saved-vehicle-form-holder').slideUp();
    jQuery(e.target).parents('.saved-vehicle-form').remove();
});

jQuery('.inventory-vehicle-compare').click(function (e) {
    parentS = jQuery(e.target).parents('.saved-vehicle-item');
    vID = jQuery(parentS).attr('id');
    compareCount = parseInt(jQuery('#compare-vehicle-count').text());
    compareBox = jQuery(parentS).find('.inventory-vehicle-compare');
    info = {};

    if (compareCount < 5) {
        jQuery(parentS).find('.removed-vehicle-headline span').each(function () { // Compare saves same data as Save Cookie
            info[jQuery(this).attr('data-name')] = jQuery(this).text();
        });

        if (jQuery(compareBox).hasClass('checked')) {
            jQuery(compareBox).removeClass('checked');
            jQuery(compareBox).find('.compare-text').text('Compare Vehicle');
            jQuery(compareBox).find('.compare-checkbox').text('');
            Cookies.remove('CDPvehicleCompare_' + vID);
            compareCount--;
            jQuery('.compare-' + vID).addClass('empty').removeClass('.compare-' + vID).children().remove(); // Remove Compare Image
        } else {
            jQuery(compareBox).addClass('checked');
            jQuery(compareBox).find('.compare-text').text('Remove Compare');
            jQuery(compareBox).find('.compare-checkbox').text('X');
            Cookies.set('CDPvehicleCompare_' + vID, info, {expires: 1});
            compareCount++;
            console.dir(info);
            outPut = '<div class="compare-main"><img src="' + info['image'] + '" /></div>';
            outPut += '<div class="compare-sub" style="display: none;">';
            outPut += '<span class="compare-saleclass">' + info['saleclass'] + '</span>';
            outPut += '<span class="compare-year">' + info['year'] + '</span>';
            outPut += '</br><span class="compare-make">' + info['make'] + '</span>';
            outPut += '<span class="compare-model">' + info['model'] + '</span>';
            outPut += '</br><span class="compare-trim">' + info['trim'] + '</span>';
            outPut += '</div>';
            outPut += '<div class="compare-remove" data-key="' + vID + '">Remove [X]</div>';
            jQuery('#compare-vehicle-container .compare-item.empty').first().removeClass('empty').addClass('item-' + compareCount + ' compare-' + vID).append(outPut);
        }

        if (compareCount == 0) {
            jQuery('#compare-vehicle-container').addClass('empty').removeClass('visible');
        } else {
            jQuery('#compare-vehicle-container').addClass('visible').removeClass('empty');
        }
        jQuery('#compare-vehicle-count').text(compareCount);
    } else {
        jQuery('#compare-over-count').slideDown().delay(2000).slideUp();
    }
});

jQuery(document).ready(function () {
    if (jQuery('#compare-button').length) {

        jQuery('.compare-item').hover(function () {
            jQuery(this).find('.compare-sub').slideDown();
        }, function () {
            jQuery(this).find('.compare-sub').slideUp();
        });
    }
})

jQuery('#compare-button').click(function () {
    compareCount = parseInt(jQuery('#compare-vehicle-count').text());
    if (compareCount > 1) {
        window.open('/inventory/compare-vehicles', '_blank');
    } else {
        jQuery('#compare-under-count').slideDown().delay(2000).slideUp();
    }
})

jQuery('#compare-vehicle-container').on('click', '.compare-remove', function (e) {
    vID = jQuery(e.target).attr('data-key');
    compareCount = parseInt(jQuery('#compare-vehicle-count').text());
    Cookies.remove('CDPvehicleCompare_' + vID);
    jQuery('#' + vID).find('.compare-text').text('Compare Vehicle');
    jQuery('#' + vID).find('.compare-checkbox').text('');
    jQuery('#' + vID).find('.inventory-vehicle-compare').removeClass('checked');
    jQuery('.compare-' + vID).removeClass('compare-' + vID).addClass('empty').children().remove();
    compareCount--;
    if (compareCount == 0) {
        jQuery('#compare-vehicle-container').addClass('empty').removeClass('visible');
    } else {
        jQuery('#compare-vehicle-container').addClass('visible').removeClass('empty');
    }
    jQuery('#compare-vehicle-count').text(compareCount);
});
/***
 Save Page - End
 ***/


/***
 Compare Page - Start
 ***/
jQuery('#saved-vehicle-container').click(function () {
    saveCount = parseInt(jQuery('#saved-vehicle-count').text());
    if (saveCount > 0) {
        window.open('/inventory/saved-vehicles', '_blank');
    }
})

jQuery('.inventory-vehicle-save').click(function (e) {
    parentS = jQuery(e.target).parents('.compare-vehicle-item');
    vID = jQuery(parentS).attr('id');
    saveCount = parseInt(jQuery('#saved-vehicle-count').text());
    saveBox = jQuery(parentS).find('.inventory-vehicle-save');
    info = {};

    jQuery(parentS).find('.compare-saved-vehicle-content span').each(function () {
        info[jQuery(this).attr('data-name')] = jQuery(this).text();
    });

    if (jQuery(saveBox).hasClass('checked')) {
        jQuery(saveBox).removeClass('checked');
        jQuery(saveBox).find('.save-text').text('Save Vehicle');
        jQuery(saveBox).find('.save-checkbox').text('');
        Cookies.remove('CDPvehicleSave_' + vID);
        saveCount--;
    } else {
        jQuery(saveBox).addClass('checked');
        jQuery(saveBox).find('.save-text').text('Vehicle Saved');
        jQuery(saveBox).find('.save-checkbox').text('X');
        Cookies.set('CDPvehicleSave_' + vID, info, {expires: 30});
        saveCount++;
    }

    if (saveCount == 0) {
        jQuery('#saved-vehicle-count-container').addClass('empty').removeClass('saved');
    } else {
        jQuery('#saved-vehicle-count-container').addClass('saved').removeClass('empty');
    }
    jQuery('#saved-vehicle-count').text(saveCount);
});

/***
 Compare Page - End
 ***/

/***
 Scroll Handler - Start
 ***/
window.onscroll = saveCompare_scroll_Handler;

function saveCompare_scroll_Handler() {
    if (jQuery('#compare-vehicle-container.visible').length) {
        var mainContainer = document.getElementById('saved-vehicle-inner-container');
        var mainHeight = mainContainer.offsetHeight + 185;
        var container = document.getElementById('compare-vehicle-container');
        var contentHeight = container.offsetHeight + 15;
        var yOffset = window.pageYOffset;
        var windowY = yOffset + window.innerHeight;
        var bottomOffset = windowY - (contentHeight * 2);

        if (windowY > mainHeight) {
            jQuery(container).css({'bottom': 'inherit', 'position': 'relative'});
        } else {
            jQuery(container).css({'bottom': '0px', 'position': 'fixed', 'left': '50%', 'margin-left': '-460px'});
        }

        jQuery('#compare-button').position({
            my: 'center',
            at: 'center',
            of: '#compare-vehicle-count-container'
        });
    }
}

/***
 Scroll Handler - Start
 ***/

/***
 Ajax Handler - Start
 ***/
function saveCompare_ajax_handler() {
    return jQuery.ajax({
        url: ajax_object.url,
        data: {
            'action': 'saved_ajax_handler',
            'handler': saveCompare_function_handler,
            'params': saveCompare_function_data,
            'nonce' : ajax_object.nonce
        },
        dataType: 'json',
        beforeSend: function () {
        },
        success: function (data) {
            if (data['handler'] == 'saveForm') {
                jQuery('#' + data['id'] + ' .saved-vehicle-form-holder').append(data['content']);
                jQuery('#' + data['id'] + ' .saved-vehicle-form-holder').slideDown();
            }
        },
        complete: function () {
        },
        error: function (xhr, status, error) {
            //alert('Ajax call failed.');
            //alert(error);
        }
    });
}

/***
 Ajax Handler - End
 ***/